/* simp styles */
$(document).ready(function(){
	$("#lm li[!.cm] > ul").addClass("d_none");

	$("#lm > li").each(function (i){
		if($(this).has("ul").html()){
			if($(this).hasClass("cm")){
				$(this).html('<span>-</span>'+$(this).html());
			} else {
				$(this).html('<span>\+</span>'+$(this).html());
			}
		}
	});

//	$.get("/system/main.p3", function(data){
//		$('#maintext').html(data);
//		/* set cafedras hblock */
//		$(".cafedral").css("height",$(".cafedral > .caf-info > .open").height());
//	});

	$(".video a img").parent().prepend("<dfn>\&nbsp\;</dfn>");
	$(".video dfn").css("opacity","0.6");
	$(".video dfn").mouseover(function(){	$(this).css("opacity","1");});
	$(".video dfn").mouseout(function(){	$(this).css("opacity","0.6");});

//	$.get("/system/maingal.p3", function(data){
//		$("#galcont").html(data);
//		setIM();
//	});
	
	$(window).resize(function(){
		setIM();
	});

	setColor();

	$(".caf-list ul li").click(function(){
		$(".caf-list h5.cur").removeClass("cur");
		if(!$(this).hasClass("open")){
			$(".caf-list .open").removeClass("open").removeClass("first");
			$(this).addClass("open");
			var cin = Number($(this).index()+1);
			$(".caf-info div").removeClass("open").slideUp(0);
			$(".caf-info div").eq(cin).slideDown(600);
		}
		return false;
	});
	$(".caf-list h5.cur").click(function(){
		if(!$(this).hasClass("open")){
			$(".caf-list ul li").removeClass("open").removeClass("first");
			$(this).addClass("open");
			var cin = Number(0);
			$(".caf-info div").removeClass("open").slideUp(0);
			$(".caf-info div").eq(cin).slideDown(600);
		}
		return false;
	});

	if($("a[rel^='pp']").html()){
		$("a[rel^='pp']").prettyPhoto();
	}

	$("#lm span").click(function(){
		var li = $(this).parent();
		li.parent().find("li.cm").removeClass("cm");

		if($(this).html()!='-'){
			li.parent().find("ul").slideUp(600);
			li.parent().find("span").html("+");
			li.find("ul:first").slideDown(600);
			$(this).html("-");
		} else {
			$(this).parent().find("ul").slideUp("slow");
			$(this).parent().find("span").html("+");
			$(this).html("+");
		}
	});
	// load default schedule
	if($("div.mainmonth").html()){
		//_sched();
		$(".month > ul > li").click(function(){
			if(!$(this).hasClass("cur")){
				scheduleMon($(this));
			}
		});
	}
	// search
	$("#search input[type='text']").click(function(){
		if($(this).val()=='Поиск'){
			$(this).val("");
		}
	});

	// show\hide metro
	$(".metrospan").mouseover(function(){
		$("#d"+this.id).show();
	});
	$(".metrospan").mouseout(function(){
		$("#d"+this.id).hide();
	});

	// icq flower
	geticq();

	// hid/show sched
	$(".oksched").slideUp(0);
	$(".sho .gobtn").click(function(){
		var ths = $(this);
		var shb = ths.parent().find(".oksched");
		if(shb.css("display")=='table'){
			ths.find("a").text("Расписание курсов");
			shb.slideUp("slow");
		} else {
			ths.find("a").text("Скрыть расписание");
			shb.slideDown("slow");
		}

//		alert(ths.index());
		return false;
	});

	/* prints */
	prints();
});

/* account prints */
function prints(){
	$(".forjurebtn").click(function(){
		if($(".forjure").css("display")=="none"){
			$(".forjure").slideDown(600);
			//$("#send input").val("счёт для безналичного расчёта");
			$("#pri").val("beznal");
			$(".print-btn, .or").slideUp(0);
		} else {
			//$("#send input").val("оформить заявку");
			$(".forjure").slideUp(600);
			$("#pri").val();
			$(".print-btn, .or").slideDown(0);
		}
	});
	$(".print-btn input").click(function(){
//		var off = $(".iagree");
//		if(off.attr("checked")){
//			var postdata;
//			var name = $("#name").val();
//			var id = $("#eventid").val();
			var mode = $(this).attr("class");
//			var postdata = "/subscribe/print.p3?name="+name+"&id="+id+"&mode="+mode
//			var postdata = unescape(postdata);
			$("#pri").val(mode);
			$("form[name='orderlessons']").submit();
//		}else{
//			alert('Необходимо принять условия договора-оферты (отметить "галочку")');
//			return false;
//		}
	});
}

function setIM(){
	if($("#image-list > ul").html()!=''){
		$('#image-list > ul').css("margin","0 0 0 0");
		ilist = $("#image-list");
		lineimg = Math.floor(ilist.width()/160);
		icnt = ilist.find("img").length;
		line = Math.ceil(icnt/lineimg);
		$("#i-control .dinamic").html("");

		for (var i = 0; i < line; i++){
			var stc = (i*lineimg)+1;
			var enc = (i+1)*lineimg;
			if(enc>icnt){var enc=icnt;}
			var ipl = i+1;
			if($("#i-control .dinamic li").filter(".cur") && i==0){
				lili = "<li title='"+ipl+"' class='cur'>"+stc+"-"+enc+"</li>"
			} else {
				lili = "<li title='"+ipl+"'>"+stc+"-"+enc+"</li>";
			}
			$("#i-control .dinamic").append(lili);
		}
		$("#i-control .dinamic li").click(function(){
			var pp = $("#i-control .dinamic li.cur");
			var ppos = pp.attr("title");
			$("#i-control .dinamic li").removeClass("cur");

			$(this).addClass("cur");
			var npos = $(this).attr("title");

			var smes = (ppos - npos)*140;
			var rem = /([\d\-]+)(?:.{2})?/;
			var osmes = $("#image-list > ul").css("margin-top");
			var osmes = Number(osmes.replace(rem, "$1"));
			var smes = smes+osmes;
			$('#image-list > ul').css("margin",smes+"px 0 0 0");
		});
	}
}

//----- schedule month click (at main page)
function scheduleMon(obj){
	var fld = 'all'
	if(currentCaf){
		fld = currentCaf.attr('id').substr(10);
	}
	_sched(obj.attr("id").substr(1), $("#schedTypeSel").val(), fld);
	$(".month > ul > li").removeClass("sel");
	obj.addClass("sel");
}

//----- schedule type selected (at main page)
function scheduleType(obj){
	_sched($(".month > ul > li.sel").attr("id").substr(1),obj.val());
}
var currentCaf = 0;
function schedType(obj){
	if(!currentCaf){
		currentCaf = $("#schedtype_all");
	}
	var rbClassRm = 'rbon';
	//if(currentCaf.attr("id") == "schedtype_all"){
	//	rbClassRm = rbClassRm + '_r';
	//}
	currentCaf.removeClass(rbClassRm);
	var cafName = obj.id.substr(10);
	//$(".month > ul > li.sel").attr("id").substr(1)
	_sched(0,0,cafName);
	currentCaf = $("#schedtype_"+cafName);
	var rbClassAdd = 'rbon';
	//if(currentCaf.attr("id") == "schedtype_all"){
	//	rbClassAdd = rbClassAdd + '_r';
	//}
	currentCaf.addClass(rbClassAdd);
}

// schedule show: month-type-folder
function _sched(m,t,f,ch){
	var sign = "?";
	var url = "/schedule/index.php";
	if(t){ url = url + sign + "type=" + t; sign = "&"; }
	if(f && f != ''){ url = url + sign + "folder=" + f; sign = "&"; }
	if(m){ url = url + sign + "month=" + m; sign = "&"; }
	//$("#schdebug").html(url);
	//alert(url);
	$.get(url, function(data){
		if(data != ""){
			$("#schedule-container").html(data);
		}
		if(ch){
			if(!$("#schedule-container table").html()){
				//scheduleMon($(".month > ul > li").eq(1));
//				alert($(".month > ul > li").eq(1).attr("id").substr(1));
//				_sched($(".month > ul > li").eq(1).attr("id").substr(1),obj.value);
			}
		} else {
			setColor();
		}
	});
}

function setColor(){
	$("#timetable table td:even").addClass("row");
}

// course combobox on subscribe page
function setCourse(obj){
	document.getElementById('eventid').value = obj.value;
	document.getElementById('orderlessons').action = '/subscribe/'+obj.value+'.event';
	$.get('/subscribe/info.php?id='+obj.value, function(data){
		if(data != ""){
			eval(data);
		}
	});
}

function showPrices(id){
	$(".cafprice").each(function (i){
		if(this.id != id && id != 'all'){
			this.style.display = 'none';
		}else{
			this.style.display = 'block';
		}
	});
	var linkid = "lnk_"+id;
	$("#caflist > li > a").each(function (i){
		if(this.id == linkid){
			$(this).addClass("rbon");
		}else{
			$(this).removeClass("rbon");
		}
	});
}

function showTab(id){
	$("ul.tabs > li").each(function(i){
		if(this.id == "tab_"+id){
			$(this).addClass("curtab");
		}else{
			$(this).removeClass("curtab");
		}
	});
	$("div.tabcontent").each(function(i){
		if(this.id == "content_"+id){
			$(this).removeClass("dnone");
		}else{
			$(this).addClass("dnone");
		}
	});
}

function showReviews(id){
	$(".cafreview").each(function (i){
		if(this.id != id){
			this.style.display = 'none';
		}else{
			if(this.innerHTML != '.'){
				this.style.display = 'block';
			}else{
				$.get("/reviews/"+id+"/?mode=js", function(data){
					$("#"+id).html(data);
					$("#"+id).css('display','block');
				});
			}
		}
	});
	var linkid = "lnk_"+id;
	$("#caflist > li > a").each(function (i){
		if(this.id == linkid){
			$(this).addClass("rbon");
		}else{
			$(this).removeClass("rbon");
		}
	});
}

//----- запоминаем сортировку
function setSchedSort(sortName){
	setDocumentCookie(document, 'sortsched', sortName);
	var url = document.location.href;
	url = url.replace(/[^/]+$/,'');
	document.location.href = url;
	//document.location.reload();
}

function setDocumentCookie(oDoc, sName, sValue, toMs){
	if ( sName.length < 1 )
		return;
	if ( sValue.length > 0 ){
		var expDate = new Date();
		if(!toMs) var toMs = 365*24*60*60*1000;
		expDate.setTime(expDate.getTime() + toMs);
		oDoc.cookie = sName + "=" + escape(sValue) + "; expires=" + expDate.toGMTString() + "; path=/;";
	} else {
		oDoc.cookie = sName + "=";
	}
}

// подгрузка icq
function geticq(){
	for(var uin in icq){
		var icqImg = document.getElementById('icq'+icq[uin]);
		if(icqImg){
			//alert('http://online.icq.com/scripts/online.dll?icq='+icq[uin]+'&img=5');
			icqImg.src = 'http://online.icq.com/scripts/online.dll?icq='+icq[uin]+'&img=5';
		}
	}
}

