var $onePhotoList, mover1, mover2, $imgsBox, $scrollBox;
var mover=null, speed=4;
var ch=new Array();
var timer1;
var animban=null;

$(window).load(function(){
	$("#shadow").height(($("#main").height()));
	$("#shadow").click(function(){
		$(this).fadeOut("normal");
		$("#window").fadeOut("normal");
	});
	$("#window .close").click(function(){
		$("#shadow").click();
		return false;
	});
	$("#callback").click(function(){
		$(".askForm1").show();
		$(".askForm1").next().hide();					 
		$("#window").fadeIn("normal");						
		return false;
	});
	
});

$(document).ready(function(){

	if($("#order_form").length)
		$("#order_form").validate();


	$("#list").attr("lw",$("#list").width());
	$scrollBox=$(".scrollBox");
	mini=parseInt($("#pricemin").val());
	maxi=parseInt($("#pricemax").val());
	
	$("#feedsub").click(function(){
		$form=$(".askForm");
		$form.ajaxForm(function(data){
			if(data==1){
				$(".askForm").hide();
				$(".askForm").next().show();				
			}
		});
		$form.submit();
		return false;
	});
	var pageact=$(".pagesBox .active").html();
	pageact=eval(pageact);
	var	n=$(".pagesBox td").length;
	if(pageact>10 && n>20 ){
		w=$("#pagesWithParams .list").width()-$("#pagesWithParams").width();
		if(pageact<(n-10))
			x=-(pageact-9)*35;	
		else
			x=-(n-20)*35;				
		$("#pagesWithParams .list").animate({left: x+"px"}, "normal");
		x=-($("#pagesWithParams").width()-70)*x/w;
		$("#mover").animate({left: x+"px"}, "normal");
	}	
	$("input:checkbox:checked").each(function(){
		$(this).prev().addClass("a");								 
	});

	$("input:checkbox").click(function(){
		if($(this).attr("checked"))
			$(this).prev().addClass("a");
		else
			$(this).prev().removeClass("a");
	});	
	
	
	$(".forClear").each(function(){
		$(this).attr("val",$(this).val());
		$(this).focus(function(){
			if($(this).val()==$(this).attr("val"))
				$(this).val('');
		});
		$(this).blur(function(){
			if(!$(this).val())
				$(this).val($(this).attr("val"));
		});
	});
	
	$(".checkbox").hover(
      function () {
        $(this).next().fadeIn("normal");
      },
      function () {
       $(this).next().fadeOut("normal");
      }
    );
	$(".select").each(function(){
		$this=$(this);
		$select=$("select", this);
		html='<ul>';
		$("option", $select).each(function(){
			html+='<li id="'+$(this).attr("value")+'">'+$(this).text()+'</li>';							   
		});
		html+='</ul>';
		$select.after(html);
		if($("option:selected",$select).text())
			$("span", this).text($("option:selected",$select).text());
		else
			$("span", this).text($("option:first",$select).text()); 
		$this.click(function(){
			$("ul", this).slideToggle("fast");
		});
		$this.hover(
			function(){},
			function(){ $("ul", this).slideUp("fast"); },
			function(){ $("#main").slideUp("fast"); }
		);
		$("li", $this).hover(
			function(){ $(this).addClass("a")},
			function(){ $(this).removeClass("a")}
		);
		$("li", $this).click(function(){
			$select=$(this).parents(".select:first");
			val=$(this).attr("id");
			text=$(this).text();
			$("select",$select).val(val);
			$("select",$select).change();
			$("span",$select).text(text);
		});
	});	   
	$("a","#tabs").click(function(){							
  		$("li.active","#tabs").removeClass("active");
		$(this).parents("li:first").addClass("active");
		i=$("a","#tabs").index(this);
		$(".item.active",".topCenterBox .selectedTabs").removeClass("active");
  		$(".item:eq("+i+")",".topCenterBox .selectedTabs").addClass("active");
		$(".item").hide();
		$(".item.active").show();
		location.hash=$(this).attr("href");
	});
	$("a", ".tabs1").click(function(){
  		$("li.active",".tabs1").removeClass("active");
		$(this).parent().addClass("active");
		i=$(this).attr("href").substr(1,2);
		$(".item.active",".portfolioList, .hollidaysList").removeClass("active");
		if(i) $(".item:eq("+eval(i-1)+")",".portfolioList, .hollidaysList").addClass("active");
		else $(".item",".portfolioList, .hollidaysList").addClass("active");
		$(".item").hide();
		$(".item.active").show();
		return false;
	});
	
	$(".cancel").click(function(){
		$(".choiceBox").fadeOut("normal");
		$("#shadow1").fadeOut("normal");
	});
	$(".moverL").mousedown(function(){
		mover1=$(this);
		return false;
	});
	$(".moverR").mousedown(function(){
		mover2=$(this);
		return false;
	});
	
	$("#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>286)?286:x;
			x=(x>(l+w-14))?(l+w-14):x;
			$(".mover").css({"left": x+"px", "width": (w-x+l)+"px"});
			$(".cc",".mover").css("width",(w-x+l-4)+"px");
			val=Math.round(Math.pow(x/286,3.5)*maxi/10)*10;
			$("#pricemin").val(val);
		}
		if(mover2){
			x=Math.round(e.pageX-$(".pricePanel").offset().left);
			l=parseInt($(".mover").css("left"));
			x=(x<14)?14:x;
			x=(x>300)?300:x;
			x=(x<(l+14))?(l+14):x;
			$(".mover").css("width",(x-l)+"px");
			$(".cc",".mover").css("width",(x-l-4)+"px");
			val=Math.round(Math.pow(x/300,3.5)*maxi/10)*10;
			$("#pricemax").val(val);
		}
		return false;
	});
	$("#main").mouseup(function(){
		if(mover1 || mover2){
			//$("#ajaxModel").submit();	
			mover1=null;
			mover2=null;
			return false;
		}
	});
	$(".rPoint").click(function(){
		var list=$("#list");
		if(!$(this).attr("cl")){
			$(this).attr("cl", 1);	
			var lp=Math.round(list.position().left-131);
			var lw=eval(list.attr("lw"))+lp;
			if(lw>0){
				list.animate({"left": "-=131px"},"fast", function(){														
					$(".rPoint").attr("cl","");														 									 
				});
			}else{
				$("td:last",list).after($("td:eq(0)",list));
				lp=Math.round(list.position().left+131);
				list.css("left",lp+"px");	
				list.animate({"left": "-=131px"},"fast", function(){														
					$(".rPoint").attr("cl","");														 									 
				});
			}
		}
		return false;
	});
	$(".lPoint").click(function(){
		var list=$("#list");
		if(!$(this).attr("cl")){
			$(this).attr("cl", 1);
			var lp=list.position().left;
			var lw=list.attr("lw");
			if(lp<0){
				$("#list").animate({"left": "+=131px"},"fast", function(){
					$(".lPoint").attr("cl","");														 													 
				});
			}else{
				list.css("left", "-131px");
				$("td:eq(0)",list).before($("td:last",list));
				$("#list").animate({"left": "+=131px"},"fast", function(){													 
					$(".lPoint").attr("cl","");														 
				});
			}
		}
		return false;
	});
	$imgsBox=$(".imgsBox");
	$(".rPoint1").click(function(){
		$(".list1").attr("mS", $(".list1").parent().width()-$(".list1").width());
		if(!$(this).attr("cl") && parseInt($(".list1").css("left"))>$(".list1").attr("mS")){
			$(this).attr("cl", "1");
			$(".list1").animate({"left": "-=373px"},"normal", function(){
				$(".rPoint1").attr("cl","");														 
				$(".rPoint1").attr("cl","");														 
			});
			count=$("#photoNumber").text()-(-1);
			$("#photoNumber").text(count);
		}
		
		return false;
	});
	$(".lPoint1").click(function(){
		if(!$(this).attr("cl") && parseInt($(".list1").css("left"))<0){
			$(this).attr("cl", "1");
			$(".list1").animate({"left": "+=373px"},"normal", function(){
				$(".lPoint1").attr("cl","");														 
				$(".lPoint1").attr("cl","");														 
			});
		}
		count=$("#photoNumber").text()-1;
		if(count>0)
		$("#photoNumber").text(count);
		return false;
	});
	
	$(".onePhotoBox").mouseout(function(){
		animban = setTimeout(galAnim, 5000);
	});
	$(".onePhotoBox").mouseover(function(){
		clearTimeout(animban);
		galStop($("#list", this));
	});
	if($("#list").length)
		animban = setTimeout(galAnim, 5000);
		
	function galAnim(){
	    clearTimeout(animban);
		obj=$("#list");
		var op=obj.position().left;
		var ow=obj.width();
		if(op<0){									   
			obj.animate({"opacity": 0}, 400, function(){
				obj.css("left", (op+131)+"px");
				obj.animate({"opacity": 1}, 400, function(){
					animban = setTimeout(galAnim, 5000);
				});
			});
		}else{
			obj.animate({"opacity": 0}, 400, function(){
				obj.css("left", (131-ow)+"px");
				obj.animate({"opacity": 1}, 400, function(){
					animban = setTimeout(galAnim, 5000);	
				});
			});
		}
	}	

	function galStop(obj){
		obj.stop(true);
		obj.css({"marginRight":0,"opacity": 1});
	}
	$(".list", ".rightGalleryBox").attr("lw",$(".list", ".rightGalleryBox").width());
	$(".pointL").click(function(){
		if(!$(this).attr("cl")){
			$(this).attr("cl", 1);
			var list=$(this).parent().prev();
			var lp=list.position().left;
			var lw=list.attr("lw");
			galStop(list);
			if(lp<0){
				list.animate({"left": "+=370px"}, "fast", function(){
					$("pointL").attr("cl", "");
					$("#pointR").attr("cl", "");
				});
			}else{
				list.css("left", -lw+"px");
				list.animate({"left": "+=370px"}, "fast", function(){
					$("#pointL").attr("cl", "");
					$("#pointR").attr("cl", "");
				});
			}
		}
		return false;										   
	});
	$(".pointR").click(function(){
		if(!$(this).attr("cl")){
			$(this).attr("cl", 1);
			var list=$(this).parent().prev();
			var lp=list.position().left;
			var lw=list.attr("lw");
			galStop(list);
			if(lp>(370-lw)){
				list.animate({"left": "-=370px"}, "fast", function(){
					$(".pointL").attr("cl", "");
					$(".pointR").attr("cl", "");
				});
			}else{
				list.css("left", "370px");
				list.animate({"left": "0"}, "fast", function(){
					$(".pointL").attr("cl", "");
					$(".pointR").attr("cl", "");
				});
			}
		}
		return false;										   
	});
	$("#all").click( function() {
		if($("#all").attr('checked')){
			$(".check:enabled").attr('checked', true);
		}else{
			$(".check:enabled").attr('checked', false);
		}
	});
	$(".allchbx").click( function() {
		if($(this).attr('checked')){
			$(this).parent().parent().parent().find(".check").attr('checked', true);
		}else{
			$(this).parent().parent().parent().find(".check").attr('checked', false);
		}
	});
	$(".checkboxList img").hover
		( function (){
			$(this).attr("alt", function() {
			$(this).show();
			});
		},
		function () {
			$(this).parent().next().hide();
		}
	);
	if($("#mover").length){
		$("#mover").mousedown(function(e){
			mover=$(this);
			var x=e.pageX;
			mover.attr("cX", x-$(this).offset().left);
			mover.attr("pX", $(this).parent().offset().left);
			mover.attr("cW", $(".pagesBox").width()-$(".pagesBox .list").width());
			mover.attr("maxX", $(".pagesBox").width()-mover.width());
			return false;
		});
		$("#main").mousemove(function(e){
			if(mover){
				var x=e.pageX;
				var mX=x-mover.attr("pX")-mover.attr("cX");
				if(mX<0) mX=0;
				if(mX>mover.attr("maxX")) mX=mover.attr("maxX");
				$(".pagesBox .list").css("left", mX/mover.attr("maxX")*mover.attr("cW")+"px");
				mover.css("left",mX+"px");
			}
		});
		$("#main").mouseup(function(){
			mover=null;
		});
		$(window).mouseup(function(){
			mover=null;
		});
	}
	$("#all1").click( function() {
	if($("#all1").attr('checked')){
			$(".check1:enabled").attr('checked', true);
		}else{
			$(".check1:enabled").attr('checked', false);
		}
   });
	$(".choice").click(function(){
		$(this).next().next().fadeIn("normal");
		$("#shadow1").fadeIn("fast", function(){
			$(this).fadeTo("fast", 0.5);								 
		});	
		$("#shadow1").height($("#main").height()+$(".topPanel").height()+$(".bottomBlock").height()+"px");
		return false;
	});
	$("#shadow1").click(function(){
		$(this).fadeOut("normal");
		$(".choiceBox").fadeOut("normal")
	});	
	$(".choose").live("click", function(){
		var res='';
		checks=$(".choiceList", ".selectedTabs li.active").children().children(".check");
		checkstext=$(".check").next();
		if($(".selectedTabs li.active").find("input:checked").length){
			for(var i=0; i<checks.length; i++){
						
				if(checks[i].checked){
					if(res) res+=', ';
					res+=checkstext[i].innerHTML;
					
	 				as=$(".selectedTabs li.active span").text(res);
					alert(as);
					
				}
			}
		 }
		 $(".choiceBox").fadeOut("normal");
		 $("#shadow1").fadeOut("normal");
		 return false;
	});
	$("#all2").click( function() {
	if($("#all2").attr('checked')){
			$('.check2:enabled').attr('checked', true);
		}else{
			$('.check2:enabled').attr('checked', false);
		}
   });	
	$("#all3").click( function() {
	if($('#all3').attr('checked')){
			$('.check3:enabled').attr('checked', true);
		}else{
			$('.check3:enabled').attr('checked', false);
		}
   });	
	$("#dilivery").click( function() {
		if($("#dilivery").attr('checked'))
			$(".slide").slideDown("slow");
	});
	$("#self_call").click( function() {
		if($("#self_call").attr('checked'))
			$(".slide").slideUp("slow");
	});
	
	$("#dilivery").click( function() {
		if($("#dilivery").attr('checked'))
			$(".disable").animate({opacity: 1});
			$(".disable input").attr("disabled", false);
	});
	$(".faq").click( function() {
		if($(this).next().is(":hidden")){
			$(this).next().slideDown("slow");
			$(this).prev().addClass("active");
		}else{
			$(this).next().slideUp("slow")
			$(this).prev().removeClass("active");	
		}
		return false;
	});
	$(".tPoint").click( function() {
		$this=$(this);
		val=$(this).parent().prev().val();
		val++;
		$(this).parent().prev().val(val++);
		result=$(this).parent().parent().next().text();
		//$(this).parent().parent().next().next().text((result*(val-1)));
		calc();
	});
	$(".bPoint").click( function() {
		val=$(this).parent().prev().val();
		if(val>0){
			val--;	
			$(this).parent().prev().val(val--);
			result=$(this).parent().parent().next().text();
			res=$(this).parent().parent().next().next().text();
			//$(this).parent().parent().next().next().text((res-result));
		}
		calc();
	});
	$(".article").click( function() {
		if($(".article").attr('checked')){
			val=$(this).parent().parent().next().next().next().children().val();
			resul=$(this).parent().parent().next().next().next().next().text();
			//$(this).parent().parent().next().next().next().next().next().text((resul*val));
			
		}else{
			$("#summorder").text("0 p.");
		}
		calc();
	});
	$(".col").change(function(){
		//alert("1");
		calc();
		
		
	});	

	$(".reset").click(function(){
		resetForm('search_form');
		return false;
	});

	function resetForm(id) {
		$('#'+id).each(function(){
		        this.reset();
		});
	}

	function calc(){
		var  results=0;
		$checkList=$(".article");	
		$priceList=$(".summOne");
		$kolList=$(".col");
		$summ=$(".ss");
		if($("#order").find("input:checked").length){
			for(var i=0; i<$checkList.length; i++){
				if($checkList[i].checked){
					$kol=($(".col")[i]['value'])*($(".ss")[i].innerHTML);
					//$kol=$kol.toFixed(2);
					$priceList[i].innerHTML=$kol;
					results=results+eval($priceList[i].innerHTML);
					$("#summorder").text(results+' ð.');
				}
			}
		}else{
			$("#summorder").text("0 ð.")
		}
		var str = $(".procent").text().substr(0,$(".procent").text().length-1)/100;
		$("#withProcent").text((results-results*str)+' ð.');
	}
	
	
	if(document.getElementById("YMapsID")){
		var map = new YMaps.Map(document.getElementById("YMapsID"));
		map.setCenter(new YMaps.GeoPoint(37.671354,55.734643), 16, YMaps.MapType.MAP);
		map.addControl(new YMaps.Zoom());
		map.addControl(new YMaps.ToolBar());
		map.addControl(new YMaps.TypeControl());
		var s = new YMaps.Style();
		s.iconStyle = new YMaps.IconStyle();
		s.iconStyle.offset = new YMaps.Point(-13,-40);
		s.iconStyle.href ="http://api-maps.yandex.ru/i/0.2/placemarks/pmgnl1.png";
		s.iconStyle.size = new YMaps.Point(36,41);
		YMaps.Styles.add("wizard#gnl1Point", s);
		var s = new YMaps.Style();
		s.iconStyle = new YMaps.IconStyle();
		s.iconStyle.offset = new YMaps.Point(-13,-40);
		s.iconStyle.href = "http://api-maps.yandex.ru/i/0.2/placemarks/pmgnl2.png";
		s.iconStyle.size = new YMaps.Point(36,41);
		YMaps.Styles.add("wizard#gnl2Point", s);
		var s = new YMaps.Style();
		s.iconStyle = new YMaps.IconStyle();
		s.iconStyle.offset = new YMaps.Point(-13,-40);
		s.iconStyle.href = "http://api-maps.yandex.ru/i/0.2/placemarks/pmgnl3.png";
		s.iconStyle.size = new YMaps.Point(36,41);
		YMaps.Styles.add("wizard#gnl3Point", s);
		var s = new YMaps.Style();
		s.lineStyle = new YMaps.LineStyle();
		s.lineStyle.strokeColor = "33cc00FF";
		s.lineStyle.strokeWidth = 5;
		YMaps.Styles.add("wizard#33cc00FF5Line", s);
		var placemark1 = new YMaps.Placemark(new YMaps.GeoPoint(37.66786,55.732438), {hasBalloon: 0,style: "wizard#gnl1Point", balloonOptions: {maxWidth: 300}});
		map.addOverlay(placemark1);
		var placemark2 = new YMaps.Placemark(new YMaps.GeoPoint(37.67051,55.734922), {style: "wizard#gnl2Point", balloonOptions: {maxWidth: 300}});
		map.addOverlay(placemark2);
		placemark2.setBalloonContent("2");
		var placemark3 = new YMaps.Placemark(new YMaps.GeoPoint(37.673487,55.736217), {hasBalloon: 0,style: "wizard#gnl3Point", balloonOptions: {maxWidth: 300}});
		map.addOverlay(placemark3);
		var polyline1= new YMaps.Polyline([new YMaps.GeoPoint(37.667932,55.732479),new YMaps.GeoPoint(37.670555,55.734902),new YMaps.GeoPoint(37.67139,55.73478),new YMaps.GeoPoint(37.673487,55.736217)], {style:"wizard#33cc00FF5Line"});
		map.addOverlay(polyline1);
		polyline1.setBalloonContent("<div></div>");
	}

	
});
