$(document).ready(function()
{
	$(".all_container").css("top","80%").css("left","0");   
      var x1=0;
  	  var y1=0;



   var lleft;

$(window).scroll(function(){
	if($.browser.msie){
		$(".modal_photo").css("top", parseInt($(window).scrollTop())+"px");	
	}
});	

$(document).resize(function(){
	if($.browser.msie){
		$(".modal_photo").css("top", parseInt($(window).scrollTop())+"px");
		$(".modal_photo").css("width", parseInt($(window).width())+"px");
		$(".modal_photo").css("height", parseInt($(window).height())+"px");
	}
});

//скролл превьюшек кнопкой вправо  
	$(".button_modal_right").mousedown(function()
	 {  
		  
	 lleft=parseInt($(window).width())*0.9-parseInt($(".photo_small").children("nobr").width());   
	 if(!$(this).hasClass("btn_unactive_right")){
	       // alert(lleft);
			ttime=Math.abs(Math.abs(parseInt($(".photo_small").css("left")))-Math.abs(lleft));
			$(".photo_small").animate({left:Math.round(lleft)},ttime*1.2,function(){
			$(".button_modal_right").addClass("btn_unactive_right");	
			});
			} 
	   	});  
	
	$(".button_modal_right").mouseup(function()
	{
      $(".photo_small").stop(); 
	   //alert(Math.round(lleft)+" ; "+parseInt($(".photo_small").css("left")));
		if(parseInt($(".photo_small").css("left"))!=0){
		  		$(".button_modal_left").removeClass("btn_unactive_left");
		  }
		if(parseInt($(".photo_small").css("left"))==Math.round(lleft)){
		  	   //alert("hey"); 
				$(".button_modal_right").addClass("btn_unactive_right");
		  }
	});      
	 
	
	
$(".foto_gallery_img").click(function(){
	$(".modal_photo").show();
	$(".modal_photo h5").html("Фотогалерея");
	if($.browser.msie){
		$(".modal_photo").css("position", "absolute");
		$(".modal_photo").css("width", parseInt($(window).width())+"px");
		$(".modal_photo").css("height", parseInt($(window).height())+"px");
		$(".modal_photo").css("top", parseInt($(window).scrollTop())+"px");
		
	}	//$(".photo_small").children("nobr").children("img").each(function(){
			//alert($(this).attr("id"));
			//$(this).html("<a class=\"iehover\">"+$(this).html()+"</a>");
			//$("</a>").after($(this));
			//$("<a href='#'>").before($(this));
			//alert($(this).html());
			//$(this).prepend("<div class=\"iehover\">").append("</div>");
		
		//});
		
	
	
	return false;
	
	
});	

$(".foto_gallery_a").click(function(){
	$(".modal_photo").show();
	$(".modal_photo h5").html("Фотогалерея");
	if($.browser.msie){
		$(".modal_photo").css("position", "absolute");
		$(".modal_photo").css("width", parseInt($(window).width())+"px");
		$(".modal_photo").css("height", parseInt($(window).height())+"px");
		$(".modal_photo").css("top", parseInt($(window).scrollTop())+"px");
	}
	return false;
});	


$(".close_modal_video").click(function(){
	$(".modal_photo").hide();
});

$(".modal-photo-close").click(function(){
	$(".modal_photo").hide();
});

	
//скролл превьюшек кнопкой влево  
     $(".button_modal_left").mousedown(function()
	 {  
         
	 	if(!$(this).hasClass("btn_unactive_left"))
		{
        	ttime=Math.abs(Math.abs(parseInt($(".photo_small").css("left"))));
			$(".photo_small").animate({left:0},ttime*1.2, function(){
			   $(".button_modal_left").addClass("btn_unactive_left");   
			});
		} 
	 });  

	$(".button_modal_left").mouseup(function()
	{
      $(".photo_small").stop();
	  if(parseInt($(".photo_small").css("left"))==0){
	  		$(".button_modal_left").addClass("btn_unactive_left");
	  }
	  
	  if(parseInt($(".photo_small").css("left"))!=lleft){
	  		$(".button_modal_right").removeClass("btn_unactive_right");
	  } 
	
	      
	});

   $(".button_modal_left").mouseout(function(){  $(".photo_small").stop(); });
   $(".button_modal_right").mouseout(function(){  $(".photo_small").stop(); }); 
//драг превьюшек
	$(".photo_small").draggable(
	{ 
            start:
			function()
			{
				$(".button_modal_right").removeClass("btn_unactive_right");	
				$(".button_modal_left").removeClass("btn_unactive_left");	
			},
			scroll: false, axis:'x',
		    stop: 
			function()
			{
		          if(parseInt($(".photo_small").children("nobr").width())>parseInt($(window).width())*0.9)
				  {
					if(parseInt($(".photo_small").css("left"))>0)
					{
						var time=(parseInt($(".photo_small").css("left"))*0.782);                      

						$(this).animate({left:'0'},time); 
						$(".button_modal_left").addClass("btn_unactive_left");
					}

					if(parseInt($(".photo_small").children("nobr").width())<(parseInt($(window).width())*0.9-parseInt($(".photo_small").css("left"))))
					{

				       var lleft=parseInt($(window).width())*0.9-parseInt($(".photo_small").children("nobr").width()); 
					   var timevar=lleft-parseInt($(".photo_small").css("left"));
					   var time=timevar*0.782; 
				       //alert(timevar);   
						$(this).animate({left:lleft},time);
						$(".button_modal_right").addClass("btn_unactive_right");
					}                       
				  }else{$(this).animate({left:'0'},500); }
			} 

	});       


     
  $(".thumb").mousedown(function(e){
	x1=e.pageX;
	y1=e.pageY;
	
  });


$(".thumb").mouseup(function(e){
	  
if(x1 == e.pageX &&  y1 == e.pageY) {
	 
	 $(".thumb").each(function(){$(this).removeClass("selected")});   
     $(this).addClass("selected");  
	 curid=parseInt($(this).attr("id"));
	
	 if(curid!=1 && curid!=$(".photo_small").children("nobr").children().size()){
	   	 $(".next_photo").show();  
			$(".prev_photo").show();
			nextbigid=(curid+1)+"big";
			prevbigid=(curid-1)+"big"; 
			$(".prev_photo").attr("id",prevbigid);  
			$(".next_photo").attr("id",nextbigid); 
		    nextid="#"+(curid+1)+"thumb";
			previd="#"+(curid-1)+"thumb";   
			//alert(nextid+" ; "+previd);
			pathnext=$(nextid).attr("src");
			pathprev=$(previd).attr("src");

				pathnext=pathnext.replace("tmb_","");
			    pathprev=pathprev.replace("tmb_","");


			pathcur=$(this).attr("src");
			pathcur=pathcur.replace("tmb_","");
			$("#center_photo").attr("src",pathcur);
			$(".prev_photo").attr("src",pathprev);  
			$(".next_photo").attr("src",pathnext); 
	                                              
	 }
	 else{
		if(curid==$(".photo_small").children("nobr").children().size()){
			
			    $(".next_photo").hide();  
				$(".prev_photo").show();
                previd="#"+(curid-1)+"thumb";   
				//alert(nextid+" ; "+previd);
			    pathprev=$(previd).attr("src");
                pathprev=pathprev.replace("tmb_","");
                pathcur=$(this).attr("src");
				pathcur=pathcur.replace("tmb_","");
				$("#center_photo").attr("src",pathcur);
				$(".prev_photo").attr("src",pathprev);  
				   
			    prevbigid=(curid-1)+"big"; 
				$(".prev_photo").attr("id",prevbigid);  
						
		   
		}
		if(curid==1){
			$(".prev_photo").hide();
			$(".next_photo").show();   
		    	nextid="#"+(curid+1)+"thumb";
				previd="#"+(curid-1)+"thumb";  
				 	   
				
				 nextbigid=(curid+1)+"big";
				 $(".next_photo").attr("id",nextbigid);
		   
				
				 
				//alert(nextid+" ; "+previd);
				pathnext=$(nextid).attr("src");
			    pathnext=pathnext.replace("tmb_","");
				pathcur=$(this).attr("src");
				pathcur=pathcur.replace("tmb_","");
				$("#center_photo").attr("src",pathcur);
			    $(".next_photo").attr("src",pathnext);
		}
		
		
	}
    try {                                   
           
			nextnextid="#"+(curid+2)+"thumb";
		    prevprevid="#"+(curid-2)+"thumb";
	   // alert(prevprevid);  
		    pathnextnext=$(nextnextid).attr("src")
		    src1=pathnextnext.replace("tmb_","");
			pathprevprev=$(prevprevid).attr("src")
		    src2=pathprevprev.replace("tmb_","");
		    
	        $("#preload1").attr("src",src1);
	        $("#preload2").attr("src",src2);
			
			} catch(err){
		    	try{
			        nextnextid="#"+(curid+2)+"thumb";
				    pathnextnext=$(nextnextid).attr("src")
				    src1=pathnextnext.replace("tmb_","");
				    $("#preload1").attr("src",src1);
				   }catch(err){
						try{
							    prevprevid="#"+(curid-2)+"thumb";  
							    pathprevprev=$(prevprevid).attr("src")
							    src2=pathprevprev.replace("tmb_","");
							    $("#preload2").attr("src",src2);  
						}catch(err){
							
						}}
	      
			}
}

  




});


  $(".next_photo").click(function(){
       
 	$(".prev_photo").show();    
	
	curid=parseInt($(this).attr("id")); 
	currentbigid="#"+(curid)+"thumb"; 
	 $(".thumb").each(function(){$(this).removeClass("selected");});   
     $(currentbigid).addClass("selected");  
   // alert(curid+" ; "); 
	if(curid==($(".photo_small").children("nobr").children().size())){
	    
	    
	
	
	    $(".next_photo").hide(); 
		prevbigid=(curid-1)+"big"; 
	
		$(".prev_photo").attr("id",prevbigid);  
        
		currentbigid="#"+(curid)+"thumb";
	    previd="#"+(curid-1)+"thumb";   
	  		
		pathprev=$(previd).attr("src");
        pathprev=pathprev.replace("tmb_","");
        pathcur=$(currentbigid).attr("src");
		pathcur=pathcur.replace("tmb_","");
  
		$("#center_photo").attr("src",pathcur);
		$(".prev_photo").attr("src",pathprev);  
	   
	
	
	
	}else{
	
	   	nextbigid=(curid+1)+"big";
		prevbigid=(curid-1)+"big"; 
	
		$(".prev_photo").attr("id",prevbigid);  
		$(".next_photo").attr("id",nextbigid);
	
		currentbigid="#"+(curid)+"thumb";
		nextid="#"+(curid+1)+"thumb";
		previd="#"+(curid-1)+"thumb";   
		//alert(nextid+" ; "+previd);
	
		pathnext=$(nextid).attr("src");
		pathprev=$(previd).attr("src");

		pathnext=pathnext.replace("tmb_","");
		pathprev=pathprev.replace("tmb_","");

      
		pathcur=$(currentbigid).attr("src");
		pathcur=pathcur.replace("tmb_","");
  
		$("#center_photo").attr("src",pathcur);
		$(".prev_photo").attr("src",pathprev);  
		$(".next_photo").attr("src",pathnext);
    
	}                                  
    try {                                   

			nextnextid="#"+(curid+2)+"thumb";
		    prevprevid="#"+(curid-2)+"thumb";
	   // alert(prevprevid);  
		    pathnextnext=$(nextnextid).attr("src")
		    src1=pathnextnext.replace("tmb_","");
			pathprevprev=$(prevprevid).attr("src")
		    src2=pathprevprev.replace("tmb_","");

	        $("#preload1").attr("src",src1);
	        $("#preload2").attr("src",src2);

			} catch(err){
		    	try{
			        nextnextid="#"+(curid+2)+"thumb";
				    pathnextnext=$(nextnextid).attr("src")
				    src1=pathnextnext.replace("tmb_","");
				    $("#preload1").attr("src",src1);
				   }catch(err){
						try{
							    prevprevid="#"+(curid-2)+"thumb";  
							    pathprevprev=$(prevprevid).attr("src")
							    src2=pathprevprev.replace("tmb_","");
							    $("#preload2").attr("src",src2);  
						}catch(err){

						}}

			}  
  }); 

 $(".prev_photo").click(function(){

 $(".next_photo").show();    

	curid=parseInt($(this).attr("id"));  
    currentbigid="#"+(curid)+"thumb"; 
	 $(".thumb").each(function(){$(this).removeClass("selected");});   
     $(currentbigid).addClass("selected");
	if(curid==1){
		$(".prev_photo").hide();
        
		nextbigid=(curid+1)+"big";
		$(".next_photo").attr("id",nextbigid);
        
		currentbigid="#"+(curid)+"thumb";
		nextid="#"+(curid+1)+"thumb";
	    //alert(nextid+" ; "+previd);
        
		pathnext=$(nextid).attr("src");
	    pathnext=pathnext.replace("tmb_","");
	    pathcur=$(currentbigid).attr("src");
		pathcur=pathcur.replace("tmb_","");

		$("#center_photo").attr("src",pathcur);
		
		$(".next_photo").attr("src",pathnext);
		
	}else{
	
	    nextbigid=(curid+1)+"big";
		prevbigid=(curid-1)+"big"; 

		$(".prev_photo").attr("id",prevbigid);  
		$(".next_photo").attr("id",nextbigid);

		currentbigid="#"+(curid)+"thumb";
		nextid="#"+(curid+1)+"thumb";
		previd="#"+(curid-1)+"thumb";   
		//alert(nextid+" ; "+previd);

		pathnext=$(nextid).attr("src");
		pathprev=$(previd).attr("src");

		pathnext=pathnext.replace("tmb_","");
		pathprev=pathprev.replace("tmb_","");

	    pathcur=$(currentbigid).attr("src");
		pathcur=pathcur.replace("tmb_","");

		$("#center_photo").attr("src",pathcur);
		$(".prev_photo").attr("src",pathprev);  
		$(".next_photo").attr("src",pathnext);	
		
	} 
    try {                                   

			nextnextid="#"+(curid+2)+"thumb";
		    prevprevid="#"+(curid-2)+"thumb";
	   // alert(prevprevid);  
		    pathnextnext=$(nextnextid).attr("src")
		    src1=pathnextnext.replace("tmb_","");
			pathprevprev=$(prevprevid).attr("src")
		    src2=pathprevprev.replace("tmb_","");

	        $("#preload1").attr("src",src1);
	        $("#preload2").attr("src",src2);

			} catch(err){
		    	try{
			        nextnextid="#"+(curid+2)+"thumb";
				    pathnextnext=$(nextnextid).attr("src")
				    src1=pathnextnext.replace("tmb_","");
				    $("#preload1").attr("src",src1);
				   }catch(err){
						try{
							    prevprevid="#"+(curid-2)+"thumb";  
							    pathprevprev=$(prevprevid).attr("src")
							    src2=pathprevprev.replace("tmb_","");
							    $("#preload2").attr("src",src2);  
						}catch(err){

						}}

			}
  });

	
	
	
	

	
	
	
});
