
var status = false;
var direct = 2;
var Top = 0;
var heightList
var heightBox




function Drive(){
	if(status){
		Top = Top + direct;
		
		if(Top < heightBox - heightList)
			Top = heightBox - heightList
		if(Top > 0)
			Top = 0
		$('.list','.blockScroll').css('top', Top+'px')
		window.setTimeout(Drive, 10)
	}
}


//------------- прокрутка фоток
$(function(){
	var L = $("li",".listFoto").length;
	$("ul",".listFoto").width(L*130)
	
	$("a", $("li",".listFoto")).click(function(){
		$('.ContainerLoad').attr("src", $(this).attr("href"))
		$('.bigFotoNameImage').html($(this).attr("title"))
		Index = $('li',$(this).parent().parent()).index($(this).parent());
		if(L > 5)
			DriveList(Index)
		
		return false;
	});
	
	$(".next", ".listFoto").click(function(){
		if(L > 5)
			DriveList(Index+1)
		return false;
	});
	$(".back", ".listFoto").click(function(){
		if(L > 5)
			DriveList(Index-1)
		return false;
	});
	if(L < 6){
		$(".next", ".listFoto").css("display","none")
		$(".back", ".listFoto").css("display","none")
	}
});
var Index = 2;
var pos = 2;
function DriveList(In){

	if(In < 2)
		In = 2
	if(In > $("li",".listFoto").length-3)
		In = $("li",".listFoto").length-3
	Index = In
	n = pos-In
	if(n != 0){
		if(n == 1)
			delta = '+=130';
		if(n == -1)
			delta = '-=130';
		if(n == 2)
			delta = '+=260';
		if(n == -2)
			delta = '-=260';
		$("ul",".listFoto").animate({
		left: delta
		}, 250, 'swing',  function() {
			pos = Index
		});
	}
	
}
/**************** новая фотогалерея ******************/

$(function(){
	var item_w = 126;
	var curent_i = 0;
	$('.foto-gallery-3').find("ul").width($('.foto-gallery-3').find("li").length*item_w);
	$('.foto-gallery-3').find("ul").find("a").click(function(){
		var i = $(this).parent().parent().find("li").index($(this).parent());
		openFoto(i);
		return false;
	});
	$('.foto-gallery-3').find('.big-img').find("a.next").click(function(){
		var i = $('.foto-gallery-3').find(".big-img").attr('i');
		openFoto(i*1+1);
		return false;
	});
	$('.foto-gallery-3').find('.big-img').find("a.back").click(function(){
		var i = $('.foto-gallery-3').find(".big-img").attr('i');
		openFoto(i*1-1);
		return false;
	});
	$('.foto-gallery-3').find('.list-image').find("a.next").click(function(){
		var i = $('.foto-gallery-3').find(".big-img").attr('i');
		openFoto(i*1+4);
		return false;
	});
	$('.foto-gallery-3').find('.list-image').find("a.back").click(function(){
		var i = $('.foto-gallery-3').find(".big-img").attr('i');
		openFoto(i*1-4);
		return false;
	});
	openFoto(0);
})

function openFoto(i){
	var item_w = 126;
	var maxHeight = 480;//550;
	var maxWidth = 724;//800;
	var curent_i;
	var l = $('.foto-gallery-3').find("li").length;
	
	if(i > l-1)
		i = l-1;
	if(i < 0)
		i = 0;
	
	var drive_to = i-2;
	if(drive_to > l-5)
		drive_to = l-5;
	if(drive_to < 0)
		drive_to = 0;
	
	
	
	
	$('.foto-gallery-3').find("ul").find("li").removeClass("act");
	$('.foto-gallery-3').find("ul").find("li").eq(i).addClass("act");
	var src_img = $('.foto-gallery-3').find("ul").find("li").eq(i).find("a").attr("href");
	/*var img_width = $('.foto-gallery-3').find("ul").find("li").eq(i).find("a").attr("width");
	var img_height = $('.foto-gallery-3').find("ul").find("li").eq(i).find("a").attr("Height");
	var margin = "";
	if(img_height*1 > 550)
		margin = 'style="margin-top:-'+((img_height-550)/2)+'px;"';
	
	var params = "";
	if(img_width != "" && img_width != undefined)
		params += ' width="'+img_width+'" ';

	if(img_height != "" && img_height != undefined)
		params += ' height="'+img_height+'" ';
	else
		params += ' height="550" ';
	
	if(margin != "")
		params += margin;
	*/
	$('.foto-gallery-3').find(".button").find("a").attr("href", src_img)
	
	$('.foto-gallery-3').find("ul").animate({
		left: -drive_to*item_w}, 300, function(){
			$('.foto-gallery-3').find(".big-img").find("div").html('<img src="'+src_img+'" style="visibility:hidden;" alt=""/>');
			var $img = $('.foto-gallery-3').find(".big-img").find("div").find('img');
			 
		    // Дожидаемся загрузки изображения браузером.
		    $img.load(function(){
		    	var CurImg = $(this);
		    	CurImg.removeAttr("width").removeAttr("height").removeAttr("style");
		    	var width  = CurImg.width();
		    	var height = CurImg.height();
		    	var newHeight;
		    	var newWidth;
		    	
		    	var type = "h";
		    	var k = 1;
		    	//if (width/height > 1.4545)
		    	if (width/height > 1.5083)
		    		type = "h";
		    	else
		    		type = "w";
		    	//alert(width/height)
		    	if(type == "h"){
		    		k = maxHeight/height;
		    		newHeight = maxHeight;
			    	newWidth = parseInt(width*k);
		    	}else{
		    		k = maxWidth/width;
		    		newHeight = parseInt(height*k);
			    	newWidth = maxWidth;
		    	}
		    	
		    	var marginTop = (maxHeight - newHeight)/2;
		    	
		    	CurImg.css({ width: newWidth+"px", height: newHeight+"px", marginTop: marginTop+"px"});
		    	//attr('width', newWidth).attr('width', newHeight);
		    	//alert(newWidth+" "+newHeight)
		    });
		}
	)

	$('.foto-gallery-3').find(".big-img").attr('i', i);	
}
