
var bWidth=0;
var intLeft=0;
var intRight=0;
var minTMain=0;
var maxTMain=0;
var show="none";
var MX=0;
var MY=0;

$(document).ready(function(){	
	
	$(document).mousemove(function(e){
      MX=e.pageX;
	  MY=e.pageY;
								   
	  
	  $(".wrapper_img_bath[nogong!='aga']").each(function(){
	 				 if($(this).find("img").attr("show")!="true")
						return false;
					var topI=$(this).position().top;
					var pos=$(this).position();
					$(this).attr("nogong","aga");
					//alert("1");
					//alert(pos.top + " TOP \r\n" + pos.left + " LEFT\r\n" + (pos.top+$(this).height()) + " BOTTOM\r\n" + (pos.left+$(this).width()) + " RIGHT\r\n" + MY + " MY\r\n" + MX + " MX\r\n" );
					if(parseInt(pos.top)<=parseInt(MY) && parseInt(pos.top+$(this).height())>=parseInt(MY) && parseInt(pos.left)<=parseInt(MX) && parseInt(pos.left+$(this).width())>=parseInt(MX))
					
					{
						//alert("INwe");
						$(this).removeAttr("nogong");
					}else{
						//alert("2");
						if($(this).find("img").attr("typea")=="declare")
						{
							$($(this).find("img")).animate({left:'170px'},300,function(){
											$(this).parent().remove();
										});
						}else{
							$(this).animate({left:(pos.left-170)+'px'},300,function(){
										$($(this).find("img")).animate({left:'170px'},300,function(){
											$(this).parent().remove();
										});
									});
						}
					}				
				});
   }); 
	
	setInterval(updateInfo,500);
	
	$("#slider-range").slider({
			range: true,
			min: min_val_pay,
			max: max_val_pay,
			values: [min_catalog_pay, max_catalog_pay],
			change: function(event, ui) {
				//$("#amount").val('$' + ui.values[0] + ' - $' + ui.values[1]);
				var left=$("#slider-range .ui-slider-range ").position().left;
				var w=$("#slider-range .ui-slider-range ").width();
				$(".infoLeft").text(ui.values[0]);
				$(".infoRight").text(ui.values[1]);
				var leftP=left-parseInt($(".infoLeft").width()/2);
				var rightP=left+w-parseInt($(".infoRight").width()/2);
				if(rightP-leftP<=$(".infoLeft").width()/2+$(".infoRight").width()/2+10)
				{
					
					if($(".infoRight").attr("hide")!="hide")
					{
						if(show=="left")
						   $(".infoRight").fadeOut(200);
					    else
					  	   $(".infoLeft").fadeOut(200);
						   
						 $(".infoRight").attr("hide","hide");
					}
				
					//leftP+=(rightP-leftP)/2;
					
				}else{
					if($(".infoRight").attr("hide")=="hide")
					{
					 $(".infoRight").fadeIn(200).attr("hide","show");
					  $(".infoLeft").fadeIn(200);
					}
				}
				$(".infoLeft").css("left",leftP+"px");
				$(".infoRight").css("left",rightP+"px");
				$("input[name=start]").val(ui.values[0]);
				$("input[name=end]").val(ui.values[1]);
			//	
			},slide: function(event, ui) {
				//$("#amount").val('$' + ui.values[0] + ' - $' + ui.values[1]);
				var left=$("#slider-range .ui-slider-range ").position().left;
				var w=$("#slider-range .ui-slider-range ").width();
				$(".infoLeft").text(ui.values[0]);
				$(".infoRight").text(ui.values[1]);
				var leftP=left-parseInt($(".infoLeft").width()/2);
				var rightP=left+w-parseInt($(".infoRight").width()/2);
				if(rightP-leftP<=$(".infoLeft").width()/2+$(".infoRight").width()/2+10)
				{
					
					if($(".infoRight").attr("hide")!="hide")
					{
					   if(show=="left")
						   $(".infoRight").fadeOut(200);
					    else
					  	   $(".infoLeft").fadeOut(200);
						   
						 $(".infoRight").attr("hide","hide");
					}
					//if(show=="left")
					//	$(".infoLeft").text(ui.values[0]);
					//else 
					//	$(".infoLeft").text(ui.values[1]);
					//leftP+=(rightP-leftP)/2;
				}else{
					if($(".infoRight").attr("hide")=="hide")
					{
					 $(".infoRight").fadeIn(200).attr("hide","show");
					  $(".infoLeft").fadeIn(200);
					}
				}
				$(".infoLeft").css("left",leftP+"px");
				$(".infoRight").css("left",rightP+"px");
				$("input[name=start]").val(ui.values[0]);
				$("input[name=end]").val(ui.values[1]);
			//	
			}
		});
		$("#slider-range .ui-slider-handle:first").css("background","url('/design/standart/style/left_s.gif') no-repeat").mousedown(function(){show="left"});
		$("#slider-range .ui-slider-handle:last").css("background","url('/design/standart/style/right_s.gif') no-repeat right top").mousedown(function(){show="right"});
		$(".infoLeft").show();
		$(".infoRight").show();
		var left=$("#slider-range .ui-slider-range ").position();
		left=left.left;
		var w=$("#slider-range .ui-slider-range ").width();
		$(".infoLeft").text(min_catalog_pay).css("left",(left-parseInt($(".infoLeft").width()/2))+"px");
		$(".infoRight").text(max_catalog_pay).css("left",(left+w-parseInt($(".infoRight").width()/2))+"px");
		$("input[name=start]").change(function(){
			$(this).val(parseInt($(this).val()));
		    $("#slider-range").slider( "values" , 0 , $(this).val());
		});
		$("input[name=end]").change(function(){
			$(this).val(parseInt($(this).val()));
		    $("#slider-range").slider( "values" , 1 , $(this).val());
		});
		//$("#amount").val('$' + $("#slider-range").slider("values", 0) + ' - $' + $("#slider-range").slider("values", 1));
		
		//$("input[name=widthT]").attr("disabled","disabled");
		//$("input[name=heightT]").attr("disabled","disabled");

		$("input[type=radio]").click(function(){
		
			if($(this).attr("name")=="height")
			{
				if(parseInt($(this).val()))
					$("input[name=heightT]").removeAttr("disabled");
				else
					$("input[name=heightT]").attr("disabled","disabled");
			}else{
				
				if(parseInt($(this).val()))
					$("input[name=widthT]").removeAttr("disabled");
				else
					$("input[name=widthT]").attr("disabled","disabled");
			}
		
		});
		
		$("#makeFun").click(function(){
			if(typeof $(this).attr("funed")==="undefined")
			{
				$(".ss-item").height("400px").width("120px").find(".vp-buttons").hide();
				$(this).attr("funed","true");
				$(".ss-item .vp-ablock").width("160px");
				$(".ss-item .add_margin").css("margin-left","10px");
				//160px
			}else{
				$(".ss-item").height("370px").width("340px").find(".vp-buttons").show();
				$(this).removeAttr("funed");
				$(".ss-item .vp-ablock").width("auto");
				$(".ss-item .add_margin").css("margin-left","auto");
			}
			return false;
		});
		
		 $(window).scroll(function(){
				$(".wrapper_img_bath[nogong!='aga']").each(function(){
					if($(this).find("img").attr("show")!="true")
						return false;
					var topI=$(this).position().top;
					var pos=$(this).position();
					$(this).attr("nogong","aga");
					//alert("1");
					//alert(pos.top + " TOP \r\n" + pos.left + " LEFT\r\n" + (pos.top+$(this).height()) + " BOTTOM\r\n" + (pos.left+$(this).width()) + " RIGHT\r\n" + MY + " MY\r\n" + MX + " MX\r\n" );
					if(parseInt(pos.top)<=parseInt(MY) && parseInt(pos.top+$(this).height())>=parseInt(MY) && parseInt(pos.left)<=parseInt(MX) && parseInt(pos.left+$(this).width())>=parseInt(MX))
					
					{
						//alert("INwe");
						$(this).removeAttr("nogong");
					}else{
						//alert("2");
						if($(this).find("img").attr("typea")=="declare")
						{
							$($(this).find("img")).animate({left:'170px'},300,function(){
											$(this).parent().remove();
										});
						}else{
							$(this).animate({left:(pos.left-170)+'px'},300,function(){
										$($(this).find("img")).animate({left:'170px'},300,function(){
											$(this).parent().remove();
										});
									});
						}
					}				
				});
													
		});
		$(".ss-item .add_margin").hover(function(){
			if(typeof $("#makeFun").attr("funed")!=="undefined")
			{
				var src=$(this).find("img").attr("src");
				var pos=$(this).find("img").position();
				var top=pos.top;
				var left=pos.left;	
												
				$("body").append('<div class="wrapper_img_bath" ><img src="' + src + '" class="img_clean" /></div>');
				$(".wrapper_img_bath:last img").attr("show","false");	
				//$(".wrapper_img_bath:last").css("top",(top-$(window).scrollTop())+"px");
				$(".wrapper_img_bath:last").css("top",top+"px");
				$(".wrapper_img_bath:last").css("left",(left-170)+"px");
				$(".wrapper_img_bath:last img").animate({left:'0px'},300,function(){
					if(left+320+10<parseInt($(window).width()))
					{
						$(this).attr("typea","standart");
						$($(this).parent()).animate({left:left+'px'},300,function(){
							$(this).find("img").attr("show","true");
							var p=$(this).position();
							p.width=parseInt($(this).width());
							p.height=parseInt($(this).height());
						//	alert(MX + " - " + MY);
						
						var cb=function(){
								if($(this).attr("anima")=="true")
									return false;								
								$(this).attr("anima","true");								
								$($(this).parent()).animate({left:(left-170)+'px'},300,function(){
									$($(this).find("img")).animate({left:'170px'},300,function(){
										$(this).parent().remove();
									});
								});
								
							};
							//if(MX<p.left || MY<p.top )
							//	
							//	$(this).find("img").trigger("mouseleave");
							
							$(this).find("img").mouseleave(cb);
						});
					}else{
						$(this).attr("typea","declare");
						$(this).attr("show","true");
						//if((MX<p.left || MX>p.left+p.width) || (MY<p.top || MY>p.top+p.height) )
							//	ALERT("A");$(this).trigger("mouseleave");
						$(this).mouseleave(function(){
							if($(this).attr("anima")=="true")
									return false;								
							$(this).attr("anima","true");													
							$(this).animate({left:'170px'},300,function(){
								$(this).parent().remove();
							});								
						});
					}
				});
				
				
			}
			//alert("IN");
		},function(){
			//alert("OUT");
			
		});
	
});

	function onlo()
	{
		
		$("#top_vanns #wrp .imgT").each(function(i,el){
			
			bWidth+=parseInt($(el).width())+12;
			$("#top_vanns #wrp").css("width",bWidth+"px");
		  //  
		
		});
		$(".right,.left").css("display","block");
		$(".left").mouseenter(function(){intLeft=setInterval(onLeft,30);}).mouseleave(function(){clearInterval(intLeft);});
		$(".right").mouseenter(function(){intRight=setInterval(onRight,30);}).mouseleave(function(){clearInterval(intRight);});
		$(".imgT").click(function(){
			
			var position = $(this).position();
			//alert(position.left);
			position.left+=$(this).width()/2;
			var opaset=this;
			$("#top_vanns #wrp").animate({left:-(position.left-$("body").width()/2)+"px"},400,"swing",function(){
					window.location=$(opaset).find("a").attr("href");
			});
			$("#top_vanns #wrp .imgT").css("border","none");
			$(this).filter("img").css("border","3px solid #f00");
			return false;
		
		});
		
		$(".left").click(function(){
			var position = $("#top_vanns #wrp .imgT:first").position();
			//alert(position.left);
			position.left+=$("#top_vanns #wrp .imgT:first").width()/2;
			$("#top_vanns #wrp").animate({left:-(position.left-$("body").width()/2)+"px"},400,"swing");
			return false;
		});
		
		$(".right").click(function(){
			var position = $("#top_vanns .imgT:last").position().left;
			position-=$("body").width()/2;
			position+=$("#top_vanns #wrp .imgT:last").width()/2;
			$("#top_vanns #wrp").animate({left:-(position)+"px"},400,"swing");
			return false;
		});
		//alert(bWidth+"px");
		minTMain=$("body").width()/2-$("#top_vanns #wrp img:first").width()/2;
		//maxTMain=-($("#top_vanns #wrp").width()-$("body").width()/2-$("#top_vanns #wrp img:last").width());
		maxTMain=-($("#top_vanns #wrp img:last").position().left-$("body").width()/2+$("#top_vanns #wrp img:last").width()/2);
		//alert(maxTMain);
	}
	
	function onRight()
	{
	//alert("&");
		var next=parseInt($("#top_vanns #wrp").css("left"))-5;
		if(next<=maxTMain)
			next=maxTMain;
		$("#top_vanns #wrp").css("left",next+"px");
	}
	
	function onLeft()
	{
	//alert("&");
		var next=parseInt($("#top_vanns #wrp").css("left"))+5;
		if(next>=minTMain)
			next=minTMain;
		
		$("#top_vanns #wrp").css("left",next+"px");
	}
	
	function updateInfo()
	{
		//minTMain=$("body").width()/2-$("#top_vanns #wrp img:first").width()/2;
		//maxTMain=-($("#top_vanns #wrp").width()-$("body").width()/2-$("#top_vanns #wrp img:last").width());
		minTMain=$("body").width()/2-$("#top_vanns #wrp img:first").width()/2;
		maxTMain=-($("#top_vanns #wrp img:last").position().left-$("body").width()/2+$("#top_vanns #wrp img:last").width()/2);
	}
	
	function showLigthBox(url)
	{
		$(".display_gal a").lightBox({
	imageLoading: '/images/loading.gif',
	imageBtnClose: '/images/close.gif',
	imageBtnPrev: '/images/prev.gif',
	imageBtnNext: '/images/next.gif',
	containerResizeSpeed: 350,
	txtImage: 'Фото',
	txtOf: 'из'
   });
   $(".display_gal a[href='"+url+"']").trigger("click");
	}
	
	$(document).ready(function(){
		$(".display_gal").each(function(){
			var file=$(this).attr("file_gal");
			if(!file)
				return;
			$.getJSON(file, function(data) {
			  var i;
			  for(i=0;i<data.length;i++)
			  {
			  	$(".display_gal").append("<a href=\""+data[i].big+"\">Gallery 1</a>");
			  }
			});
		});
	});

