
var loading;
var cur = 0;

$(document).ready(function(){
	if(window.location.hash != ""){
		var pages = ['fd_news', 'fd_about', 'fd_timing', 'fd_gallery', 'fd_blog', 'fd_contacts'];
		var hash = window.location.hash.replace("#", "");
		if (Array.prototype.indexOf){
			if(Array.indexOf(pages, hash) == -1){
				window.location.href = base_url + "/"+ hash;
			}
		}
		else{
			var found = false;
			for(var i = 0; i < pages.length; i++){
				if(pages[i] == hash){
					found = true;
					break;
				}
			}
			if(!found){
				window.location.href = base_url + "/"+ hash;
			}
		}
	}
	setInterval("scroll_gal()", 2500);
	$('.fd_to_home').live("click", function(){
		$.scrollTo('.head', 800);
	});
	$('.fd_to_news').live("click", function(){
		$.scrollTo('.news_sm', 800, {offset:{top: -120}});
	});
	$('.fd_goto').live("click", function(){
		if($(this).attr("rel") != "home"){
			switch($(this).attr("rel")){
				case "about": var id = "fd_about"; break;
				case "timing": var id = "fd_timing"; break;
				case "gallery": var id = "fd_gallery"; break;
				case "blog":  var id = "fd_blog"; break;
				case "request": var id = "fd_request"; break;
				case "contacts": var id = "fd_contacts"; break;
				case "news": var id = "fd_news"; break;
				case "hime": var id = "fd_news"; break;
			}
			if($("#"+id).length == 0) {
				load_view($(this).attr("rel"));
			}
			$.scrollTo('#'+id, 800);
		}
		else{
			$.scrollTo(0, 800);
		}
		return false;
	});
	$(".big_dk, .close").live("click", function(){
		$(".big_dk, .pop_window").fadeOut(200, function(){
			$(".pop_window").remove();
		});
		adress("");
	});
	/*$("#fd_gallery").mouseenter(function(){
		$(this).find(".gal_buttons, .gal_dk").stop(1,1).fadeIn(400);
	});
	$("#fd_gallery").mouseleave(function(){
		$(this).find(".gal_buttons, .gal_dk").stop(1,1).fadeOut(400);
	});*/
	
	// NEWS
	
	$(".fd_all_news").live("click", function(){
		get_pu("news", "", "#news");
		adress("news/list");
		return false;
	});
	$(".fd_news_post").live("click", function(){
		var id = $(this).attr("id").replace("npost_", "");
		get_pu("news_post", "&id="+id, "#news");
		adress("news/post/"+id);
	});
	$(".fd_archive").live("click", function(){
		var month = $(this).attr("rel");
		get_pu("news", "&month="+month, "#news");
		adress("news/list/"+month);
	});
	$(".fd_news_paging").live("click", function(){
		if($(this).hasClass("fd_next")){
			var cur = parseInt($("#news_cur_pg").val());
			cur++;
		}
		if($(this).hasClass("fd_prev")){
			var cur = parseInt($("#news_cur_pg").val());
			cur++;
		}
		if($(this).hasClass("fd_page")){
			var cur = $(this).attr("rel");
		}
		var month = $("#month").val();
		if(month == ""){
			var data = "&pg="+cur;
		}
		else{
			var data = "&pg="+cur+"&month="+month;
		}
		$("#news_cur_pg").val(cur);
		get_pu("news", data, "#news");
		adress("news/list/"+month);
	});
	
	// BLOG
	
	$(".fd_full_blog").live("click", function(){
		get_pu("blog_list", "", "#blog_list");
		adress("blog/list");
		return false;
	});
	$(".fd_blog_post").live("click", function(){
		var id = $(this).attr("rel");
		get_pu("blog_post", "&id="+id, "#blog_list");
		adress("blog/post/"+id);
	});
	$(".fd_prev_blog").live("click", function(){
		var pg = parseInt($("#fd_cur_blog_pg").val());
		if($(".fd_next_blog").length == 0){
			$("<a class='arr_rgt fd_next_blog'><span>дальше</span></a>").appendTo($(this).parent());
		}
		pg--;
		if(pg == 0){
			$(this).remove();
		}
		$("#fd_cur_blog_pg").val(pg);
		get_view_prt("blog", "&pg="+pg);
	});
	$(".fd_next_blog").live("click", function(){
		var pg = parseInt($("#fd_cur_blog_pg").val());
		if($(".fd_prev_blog").length == 0){
			$("<a class='arr_lft fd_prev_blog' style='float: left;'><span>назад</span></a>").prependTo($(this).parent());
		}
		pg++;
		if(pg == (parseInt($("#fd_blog_num").val())-1)){
			$(this).remove();
		}
		$("#fd_cur_blog_pg").val(pg);
		get_view_prt("blog", "&pg="+pg);
	});
	$(".fd_block_auth").live("click", function(){
		var id = $(this).attr("rel");
		get_pu("blog_list", "&auth="+id, "#blog_list");
		adress("blog/list/"+id);
	});
	$(".fd_blogs_paging").live("click", function(){
		if($(this).hasClass("fd_next")){
			var cur = parseInt($("#blogs_cur_pg").val());
			cur++;
		}
		if($(this).hasClass("fd_prev")){
			var cur = parseInt($("#blogs_cur_pg").val());
			cur++;
		}
		if($(this).hasClass("fd_page")){
			var cur = $(this).attr("rel");
		}
		$("#blogs_cur_pg").val(cur);
		var auth = $("#auth").val();
		if(auth == 0){
			var data = "&pg="+cur;
		}
		else{
			var data = "&pg="+cur+"&auth="+auth;
		}
		get_pu("blog_list", data, "#blog_list");
	});
	$(".fd_pr_nxt_blog").live("click", function(){
		var id = $(this).attr("rel");
		get_pu("blog_post", "&id="+id, "#blog_list");
		adress("blog/post/"+id);
	});
	
	// TIMITNG
	
	$(".fd_timing").live("click", function(){
		var id = $(this).attr("rel");
		get_pu("timing", "&area="+id, "#timing");
		adress("timing/"+id);
		return false;
	});
	
	// GALLERY 
	
	$(".fd_gallery").live("click", function(){
		get_pu("gallery", "", "#gallery");
		adress("gallery/list");
	});
	$(".fd_album").live("click", function(){
		var id = $(this).attr("rel");
		if(id != 0){
			var pu = "gallery_album";
		}
		else{
			var pu = "gallery";
		}
		get_pu(pu, "&id="+id, "#gallery");
		if(id != 0){
			adress("gallery/album/"+id);
		}
		else{
			adress("gallery/list");
		}
	});
	$(".fd_video_album").live("click", function(){
		var id = $(this).attr("rel");
		if(id != 0){
			var pu = "video_album";
		}
		get_pu(pu, "&id="+id, "#gallery");
		if(id != 0){
			adress("gallery/video_album/"+id);
		}
	});
	$(".fd_photo").live("click", function(){
		var id = $(this).attr("rel");
		get_pu("gallery_photo", "&id="+id, "#gallery");
		adress("gallery/photo/"+id);
	});
	$(".fd_get_video").live("click", function(){
		get_pu("gallery_vid_list", "", "#gallery");
		adress("gallery/video_list/");
	});
	$(".fd_video").live("click", function(){
		var id = $(this).attr("rel");
		get_pu("view_video", "&id="+id, "#gallery");
		adress("gallery/video_post/"+id);
	});
	$(".fd_gal_paging").live("click", function(){
		if($(this).hasClass("fd_next")){
			var cur = parseInt($("#news_cur_pg").val());
			cur++;
		}
		if($(this).hasClass("fd_prev")){
			var cur = parseInt($("#news_cur_pg").val());
			cur++;
		}
		if($(this).hasClass("fd_page")){
			var cur = $(this).attr("rel");
		}
		$("#gallery_cur_pg").val(cur);
		get_pu("gallery", "&pg="+cur, "#gallery");
	});
	$(".fd_ph_paging").live("click", function(){
		var alb = $("#alb_id").val();
		if($(this).hasClass("fd_next")){
			var cur = parseInt($("#news_cur_pg").val());
			cur++;
		}
		if($(this).hasClass("fd_prev")){
			var cur = parseInt($("#news_cur_pg").val());
			cur++;
		}
		if($(this).hasClass("fd_page")){
			var cur = $(this).attr("rel");
		}
		$("#gallery_cur_pg").val(cur);
		get_pu("gallery_album", "&pg="+cur+"&id="+alb, "#gallery");
	});
	$(".fd_gal_vid_paging").live("click", function(){
		var alb = $("#alb_id").val();
		if($(this).hasClass("fd_next")){
			var cur = parseInt($("#news_cur_pg").val());
			cur++;
		}
		if($(this).hasClass("fd_prev")){
			var cur = parseInt($("#news_cur_pg").val());
			cur++;
		}
		if($(this).hasClass("fd_page")){
			var cur = $(this).attr("rel");
		}
		$("#gallery_cur_pg").val(cur);
		get_pu("gallery_vid_list", "&pg="+cur, "#gallery");
	});
	$(".fd_pr_nxt_ph").live("click", function(){
		var id = $(this).attr("rel");
		get_pu("gallery_photo", "&id="+id, "#gallery");
		adress("gallery/photo/"+id);
	});
	$(".fd_pr_nxt_vid").live("click", function(){
		var id = $(this).attr("rel");
		get_pu("view_video", "&id="+id, "#gallery");
		adress("gallery/video_post/"+id);
	});
	
	// CONTACTS
	
	$(".fd_change_cont").live("click", function(){
		var id = $(this).attr("rel").replace("cont_", "");
		$.ajax({
			type: "POST",
			asynca: false,
			data: "id="+id,
			url: base_url+"main/get_contacts",
			success: function(data){
				$("#fd_cont_box").fadeOut(400, function(){
					$(this).html(data).fadeIn(400);
					coords(id);
				});
			}
		});
	});
	
	// ABOUT US
	
	$(".fd_about").live("click", function(){
		var id = $(this).attr("rel");
		get_pu("about", "&id="+id, "#about");
		adress("about/"+id);
		return false;
	});
	
	$(".fd_about_sub").live("click", function(){
		var id = $(this).attr("rel");
		get_pu("about_sub", "&id="+id, "#about");
		adress("about/sub/"+id);
		return false;
	});
	
});

function get_pu(pu, adds, inner){
	if(inner == "" || $(inner).length == 0){
		pu += "&full=1";
	}
	$.ajax({
		type: "POST",
		async: false,
		data: "pu="+pu+adds,
		url: base_url+"main/get_pu",
		success: function(data){
			if(inner == "" || $(inner).length == 0){
				$(".big_dk").fadeIn(200);
				$(data).appendTo("body").fadeIn(400);
				resize(inner);
			}
			else{
				$(inner).html(data);
			}
		}
	});
}

function resize(pu){
	var resz = false;
	if($(pu).height() > $(window).height()){
		$(pu).css({"height": $(window).height()-50, marginTop: (-1)*($(window).height()/2 - 25), "overflow-y": "auto", "overflow-x": "hidden"});
		resz = true;
	}
	if($(pu).width() > $(window).width()){
		$(pu).css({"width": $(window).width()-50, marginLeft: (-1)*($(window).width()/2 - 25), "overflow-y": "auto", "overflow-x": "hidden"});
		resz = true;
	}
	if((pu == "gallery" || pu == "gallery_vid_list") && resz){
		var i = 1;
		$(".photo").each(function(){
			if(i%3 == 0){
				$(this).css({"margin-right": 0});
			}
			i++;
		});
	}
}

function get_view_prt(view, adds){
	$.ajax({
		type: "POST",
		async: false,
		data: "view="+view+adds,
		url: base_url+"main/load_view_prt",
		success: function(data){
			switch(view){
				case "blog": 
					$("#fd_blog_list").hide(400, function(){
						$(this).html(data);
						$(this).show(400);
					});
					break;
			}
		}
	});
}

function coords(id){
	var point;
	$.ajax({
		type: "POST",
		async: false,
		dataType: "XML",
		url: base_url+"main/get_coords",
		data: "id="+id,
		success: function(data){
			$(data).find("position").each(function(){
				if($(this).index() == 0){
					map.setCenter(new GLatLng($(this).children("posx").text(), $(this).children("posy").text()), 15);
				}
				point = new GLatLng($(this).children("posx").text(), $(this).children("posy").text());
				map.addOverlay(new GMarker(point))
			});
		}
	});
}

function scroll_gal(){
	$(".pic_holder").stop().animate({marginLeft: -636*cur}, 700);
	cur++;
	if(cur == $(".slide").length){
		cur = 0;
	}
}

function adress(pg){
	if(!$.browser.msie){
		var stateObj = { foo: "bar" };
		history.pushState(stateObj, "title", base_url+"main/index/"+pg);
	}
	else{
		window.location.hash = "main/index/"+pg;
	}
}
