$(function() {
	/* schedule */
	$(".place").click(function() {
		if ($(this).hasClass("sel")) {
			$(this).removeClass("sel");
			$("#cities").remove();
		} else {
			$(".place").removeClass("sel");
			$("#cities").remove();
		
			var html = '<ul id="cities"><li><a href="/help/">Все школы</a><br/><br/></li>';
			for (var i=0; i<regions.length; i++) {
				html += '<li><a href="?city='+regions[i][0]+'">'+regions[i][1]+'</a></li>';
			}
			html += '</ul>';
			$(this).addClass("sel");
			$("#root").append(html).find("#cities").css({left:$(this).offset().left-($("#cities").width()+33-$(this).width()), top:$(this).offset().top+35});
		}
	});
	
	if ($(".anim").length > 0) {
		$(".anim").click(function() {
			var col = $(this).parent().parent();
			if (col.hasClass("col6")) {
				$(".col7").animate({width:"30%"}).find(".content").animate({opacity:".07"}).removeClass("active_a");
				col.animate({width:"70%"}).find(".content").animate({opacity:"1.0"});
				$(".col7").find("h1 a").removeClass("sel");
				$(".paging4").hide();
				$(".paging3").show();
				location.hash = '#read';
			} else {
				$(".col6").animate({width:"30%"}).find(".content").animate({opacity:".07"});
				col.animate({width:"70%"}).find(".content").animate({opacity:"1.0"}).addClass("active_a");
				col.find("h1").removeClass("sel");
				$(".col6").find("h1 a").removeClass("sel");
				$(".paging3").hide();
				$(".paging4").show();
				location.hash = '#view';
			}
			$(this).addClass("sel");
			return false;
		});
		if (location.hash == '#view') {
			$(".anim").eq(1).click();
		}
	}

	/* read-view ajax */
	function paging3() {
		$(".paging3 a").click(function() {
			$(".col6 .content").load($(this).attr("href"), function() {
				paging3();
			});
			/*$(".paging3 a").removeClass("sel");
			$(this).addClass("sel");*/
			$(window).scrollTop(280);
			return false;
		});
	}
	paging3();
	
	function paging4() {
		$(".paging4 a").click(function() {
			$(".col7 .content").load($(this).attr("href"), function() {
				paging4();
				$(".paging4").show();
			});
			/*$(".paging4 a").removeClass("sel");
			$(this).addClass("sel");*/
			$(window).scrollTop(280);
			return false;
		});
	}
	paging4();

	if ($(".calendar_content").length > 0) {
		$(".calendar_content li:has('.schedule') .line, .calendar_content li:has('.article') .line").hide();
		$(".calendar_content li:has('.schedule')").prev().find(".line").hide();
		$(".calendar_content li:has('.article')").prev().find(".line").hide();
		$(".calendar_content li:has('.article')").prev().prev().find(".line").hide();
	}


	if ($(".f_ask").length > 0) {
		$(".f_ask span").click(function() {
			$(".p_f_ask").show();
			$(".p_f_ask .p_close").click(function() {
				$(this).parent().hide();
			});
		});
	}
	
	if ($(".f_ans").length > 0) {
		$(".f_ans span").click(function() {
			$(".p_f_ans").show();
			$(".p_f_ans .p_close").click(function() {
				$(this).parent().hide();
			});
		});
		$(".example a").click(function() {
			$(this).parent().parent().find("form input:text").val($(this).text());
			
			
			return false;
		});
	}
	
	date_place();
});
 $(document).ready(function(){
 $("dd:not(:first)").hide();
 $("dt a").click(function(){
 $("dd:visible").slideUp("slow");
 $("dt a").removeClass("sel")
 $(this).addClass("sel");
 $(this).parent().next().slideDown("slow");
 return false;
 });

	$(".country a").click(function() {
                $.get($(this).attr("href"), function(resp){ $(".content").html(resp); });
        $(".country a").removeClass("sel");
		$(this).addClass("sel");
		date_place();
		return false;
	});
}); 

 $(document).ready(function(){
 $("sdd:not(:first)").hide();
 $("li.dates a.sel").click();
 $("sdt a").click(function(){
 $("sdd:visible").slideUp("slow");
 $("sdt a").removeClass("sel")
 $(this).addClass("sel");
 $(this).parent().next().slideDown("slow");
 return false;
 });

	$(".dates a").click(function() {
                $.get($(this).attr("href"), function(resp){ $(".content").html(resp); });
        $(".dates a").removeClass("sel");
		$(this).addClass("sel");
		date_place();
		return false;
	});
});
 
function date_place() {
	if ($(".places2").length > 0) {
		if ($(".places2 li.dates a.sel").length > 0) {
			var year = $(".places2 li.dates a.sel").attr("href").split("year=")[1].split("&")[0];
			var month = $(".places2 li.dates a.sel").attr("href").split("month=")[1].split("&")[0];
			$(".places2 li.country a").each(function() {
				var newurl = $(this).attr("href").replace(/month=\d*/g, "month=" + month).replace(/year=\d*/g, "year=" + year);
				$(this).attr("href", newurl);
			});
		} 
		if ($(".places2 li.country a.sel").length > 0) {
			var city = $(".places2 li.country a.sel").attr("href").split("cityid=")[1].split("&")[0];
			$(".places2 li.dates a").each(function() {
				var newurl = $(this).attr("href").replace(/cityid=\d*/g, "cityid=" + city);
				$(this).attr("href", newurl);
			});
		}
	}
}
