var mover1, mover2;

$(window).load(function(){
	init_ymaps();
	$(".winBg").height($("#main").height());
	$(".addLink, .public").click(function(){
		$(".addLink").parent().css("zIndex", 3);
		$(this).parent().css("zIndex", 5);
		$(this).toggleClass("active");
		$(this).next().slideToggle("fast");
		return false;						 
	});
	$(".abort", ".public").click(function(){
		$(this).parents(".addBox:first").prev().click();	
		return false;						 
	});
	$(".item1 a", ".faqList.public").click(function(){
		act=($(this).parents("li:first").hasClass("active"))?1:0;
			$("li.active",".faqList.public").next().slideToggle();
			$("li.active",".faqList.public").toggleClass("active");
		if(act) return false;
		$(this).parents("li:first").toggleClass("active");
		$(this).parents("li:first").next().slideToggle();
		return false;
	});
	$(".mainMenu").children("li").each(function(){
		$(this).mouseover(function(){
			clearTimeout($(this).attr("timer"));
			$(".submenu",".mainMenu li:not(."+$(this).attr("class")+")").hide();
			$(".submenu",this).show();									
		});
		$(this).mouseout(function(){
			obj=this;
			$(this).attr("timer",setTimeout(function(){
				$(".submenu",obj).hide();																	   
			}, 200));
			
		});								   
	});
	
	$(".moverL").mousedown(function(){
		mover1=$(this);
		return false;
	});
	$(".moverR").mousedown(function(){
		mover2=$(this);
		return false;
	});
	$(".cc",".mover").width($(".mover").width()-6);
	maxi=parseInt($(".moverR span").text())/($(".mover").width()+parseInt($(".mover").css("left")))*120;
	$(".mover").attr("max", maxi);
	$("#main").mousemove(function(e){
		if(mover1){
			x=Math.round(e.pageX-$(".pricePanel").offset().left);
			l=parseInt($(".mover").css("left"));
			w=parseInt($(".mover").css("width"));	
			x=(x<0)?0:x;
			x=(x>100)?100:x;
			x=(x>(l+w-20))?(l+w-20):x;
			$(".mover").css({"left": x+"px", "width": (w-x+l)+"px"});
			$(".cc",".mover").css("width",(w-x+l-6)+"px");
			val=l/120*$(".mover").attr("max");
			$(".pricePanel").next().val(val);
			$(".moverL span").text(Math.round(val/10)*10+' P');
		}
		if(mover2){
			x=Math.round(e.pageX-$(".pricePanel").offset().left);
			l=parseInt($(".mover").css("left"));
			x=(x<20)?20:x;
			x=(x>120)?120:x;
			x=(x<(l+20))?(l+20):x;
			$(".mover").css("width",(x-l)+"px");
			$(".cc",".mover").css("width",(x-l-6)+"px");
			val=x/120*$(".mover").attr("max");
			$(".pricePanel").next().next().val(val);
			$(".moverR span").text(Math.round(val/10)*10+' P');
		}
		return false;
	});
	$("#main").mouseup(function(){
		if(mover1 || mover2){
			$("#ajaxModel").submit();	
			mover1=null;
			mover2=null;
			return false;
		}
	});
	
	
	$(".stars2").live("mousemove",function(e){
		if($(this).next() && $(this).next().attr("type")=='hidden'){
			x=e.pageX;
			l=$(this).offset().left;					  
			mark=Math.round((x-l)/21);
			$("div",this).width(mark*21);
		}
	});	
	$(".stars2").live("mouseout",function(e){
		if($(this).next() && $(this).next().attr("type")=='hidden')
			$("div",this).width($(this).next().val()*21);
	});	
	$(".stars2").live("click",function(e){
		if($(this).next() && $(this).next().attr("type")=='hidden'){
			x=e.pageX;
			l=$(this).offset().left;					  
			mark=Math.round((x-l)/21);
			$("div",this).width(mark*21);
			$(this).next().val(mark);
		}
	});	
	$(".stars").live("mousemove",function(e){
		if($(this).next() && $(this).next().attr("type")=='hidden'){
			x=e.pageX;
			l=$(this).offset().left;					  
			mark=Math.round((x-l)/25);
			$("div",this).width(mark*25);
		}
	});	
	$(".stars").live("mouseout",function(e){
		if($(this).next() && $(this).next().attr("type")=='hidden')
			$("div",this).width($(this).next().val()*25);
	});	
	$(".stars").live("click",function(e){
		if($(this).next() && $(this).next().attr("type")=='hidden'){
			x=e.pageX;
			l=$(this).offset().left;					  
			mark=Math.round((x-l)/25);
			$("div",this).width(mark*25);
			$(this).next().val(mark);
		}
	});	
	
	$("select","#ajaxModel").change(function(){
		$("#ajaxModel").submit();										 
	});
	$("input","#ajaxModel").change(function(){
		$("#ajaxModel").submit();										 
	});
	$("#ajaxModel").ajaxForm(function(data){
		data=data.split('|');
		$("p",".results").remove();
		if(parseInt(data[0])){
			data1=data[0].split('_');	
			if($("li:first",".selfSelectBox .imgs").html()){
				$("li:last",".selfSelectBox .imgs").html('<a href="/price/'+data1[1]+'/"><img src="/upload/series/'+data1[0]+'.jpg" alt="" /></a>');
				$(".selfSelectBox .imgs").animate({left:"-200px"}, 'normal', function(){
					$("li:first",".selfSelectBox .imgs").html('');															
				});
			}else{
				$("li:first",".selfSelectBox .imgs").html('<a href="/price/'+data1[1]+'/"><img src="/upload/series/'+data1[0]+'.jpg" alt="" /></a>');
				$(".selfSelectBox .imgs").animate({left:"0"}, 'normal', function(){
					$("li:last",".selfSelectBox .imgs").html('');															
				});
			}
		}
		$(".results").prepend(data[1]);
	});
	$(".stars").click(function(){
		$("#ajaxModel").submit();					   
	});
	
	$(".map a").click(function(){
		$(".winBg").fadeIn("fast", function(){ $(this).fadeTo("fast",0.7); });					   
		$("#"+$(this).attr("class")).fadeIn();
		return false;
	});
	$(".winBg").click(function(){
		$(this).fadeOut();
		$(".winBox").fadeOut();
	});
	$(".winBox .close").click(function(){
		$(".winBg").fadeOut();
		$(".winBox").fadeOut();
		return false;							   
	});	
	$(".pointL", ".img").click(function(){
		list=$(".list",".img");
		i=$("li",".img").index($("li.active",".img"))
		if(i){
			list.animate({left: "+=158px"}, "fast");
			act=$("li.active",".img");
			act.prev().addClass("active");
			act.removeClass("active");
		}
		return false;
	});
	$(".pointR", ".img").click(function(){
		list=$(".list",".img");
		i=$("li",".img").index($("li.active",".img"))
		if(i<($("li",".img").length-1)){
			list.animate({left: "-=158px"}, "fast");
			act=$("li.active",".img");
			act.next().addClass("active");
			act.removeClass("active");
		}
		return false;
	});
	$(".winBox").hide();
	$(".winBox").css("top","145px");
	initCalendar();
	if($("#message").length){
		$(".winBg").show();
		$(".winBg").css("opacity", "0.7");
		$("#message").show();
		timer=setTimeout(function(){
			$("#message").fadeOut(1000);						  
			$(".winBg").fadeOut(1000);
		},2000);
	}
	$("#opinionForm").each(function(){
		this.reset();
		$(this).validate();
	});
	$("#feedbackForm").each(function(){
		this.reset();
		$(this).validate();
	});
		$("#faqForm").each(function(){
		this.reset();
		$(this).validate();
	});
	
});

function initCalendar(){
	$("a",".calendarBox .table").click(function(){
		$.get($(this).attr("href"), function(data){
			$(".winBg").fadeIn("fast", function(){ $(this).fadeTo("fast",0.7); });					   
			$("#order").fadeIn();
			$("#order .cont").html(data);								
			$("#orderForm").validate();
			$("#selectCenter").change(function(){
				$.post("/times/", {
					center:$(this).val(),
					day:$("#day").val(),
					month:$("#month").val(),
					year:$("#year").val()
				}, function(data){
					$("#times").html(data);											   
					$("a", "#times").click(function(){
						$("a.active", "#times").removeClass("active");
						$(this).addClass("active");
						$("#timeInp").val($(this).text());
						return false;							
					});
				});									  
			});
		});
		return false;										
	});
	
	$("#pointL", ".month").click(function(){
		if(!$(this).hasClass("hold"))
			$.post("/prev_month/", {
				month:$("div",".month").attr("month"),
				year:$("div",".month").attr("year")
			}, function(data){
				$(".calendarBox .cont").html(data);
				initCalendar();
			});
		return false;								  
	});
	$("#pointR", ".month").click(function(){
		if(!$(this).hasClass("hold"))
			$.post("/next_month/", {
				month:$("div",".month").attr("month"),
				year:$("div",".month").attr("year")
			}, function(data){
				$(".calendarBox .cont").html(data);
				initCalendar();
			});
		return false;								  
	});
	
	
}