var currentFoto;
var visFirst;
var ifClick;
$(document).ready(init);
function init () {
	ifClick = 0;
	visFirst = 1;
	currentFoto = 1;
	var i = 0;
	$(".foto_border").each(function() {
		i=i+1;
	});
	var fotos = i;
	var divWidth = 101*fotos+1;
	$(".foto_list_all").css("width", divWidth);
	$(".foto_border").click(function () {
		if (ifClick == 0) {
			ifClick = 1;
			i = 0;
			var thisObj = this;
			$(".foto_border").each(function() {
				i=i+1;
				if (thisObj == this) return false;
			});
			if (currentFoto != i) {
				currentFoto = i;
				$(".foto_border").removeClass("sel");
				$(this).addClass("sel");
				var thisFoto = $(this).attr("href");
				$(".big_foto").animate( { opacity:0 }, 700, null, function () {
					var img = document.createElement('img');
				    img.onload = function () 
				    {
						$(".loader_gallery").hide();
						$(".big_foto").css("background-image", "url("+thisFoto+")");
						$(".big_foto").animate( { opacity:1 }, 700, null, function () {
							ifClick = 0;
						});
				    }
				    $(img).attr('src', thisFoto);
				    $(".loader_gallery").show();
				});
			} else {
				ifClick = 0;
			}
		}
		return false;
	});
}
function nextPhoto() {
	if (ifClick == 0) {
		ifClick = 1;
		var i = 0;
		$(".foto_border").each(function() {
			i=i+1;
		});
		var countFotos = i;
		if (countFotos == currentFoto) {
			leftVis = 0;
			currentFoto = 1;
		} else {
			var leftVis = parseInt($(".foto_list_all").css("left").substr(0, $(".foto_list_all").css("left").length-2));
			leftVis = leftVis - 101;
			currentFoto = currentFoto + 1;
		}
		i = 0;
		var thisFoto;
		var thisFotoObj;
		$(".foto_border").each(function() {
			i = i+1;
			thisFotoObj = $(this);
			if (i == currentFoto) return false;
		});
		thisFoto = thisFotoObj.attr("href");
		
		$(".big_foto").animate( { opacity:0 }, 700, null, function () {
			var img = document.createElement('img');
			img.onload = function ()
			{
				$(".loader_gallery").hide();
				$(".big_foto").css("background-image", "url("+thisFoto+")");
				$(".big_foto").animate( { opacity:1 }, 700, null, function () {
					ifClick = 0;
				});
			}
			$(img).attr('src', thisFoto);
			$(".loader_gallery").show();
			$(".foto_border").removeClass("sel");
			thisFotoObj.addClass("sel");
			if (countFotos - visFirst > 5 || currentFoto == 1) {
				$(".foto_list_all").animate( { left: leftVis }, 700);
				if (currentFoto == 1) {
					visFirst = 1;
				} else {
					visFirst = visFirst + 1;
				}
			}
		});
	}
}
function prevPhoto() {
	if (ifClick == 0) {
		ifClick = 1;
		var i = 0;
		$(".foto_border").each(function() {
			i=i+1;
		});
		var countFotos = i;
		if (currentFoto == 1) {
			leftVis = -101*(countFotos-6);
			currentFoto = countFotos;
		} else {
			var leftVis = parseInt($(".foto_list_all").css("left").substr(0, $(".foto_list_all").css("left").length-2));
			leftVis = leftVis + 101;
			currentFoto = currentFoto - 1;
		}
		i = 0;
		var thisFoto;
		var thisFotoObj;
		$(".foto_border").each(function() {
			i = i+1;
			thisFotoObj = $(this);
			if (i == currentFoto) return false;
		});
		thisFoto = thisFotoObj.attr("href");
		
		$(".big_foto").animate( { opacity:0 }, 700, null, function () {
			var img = document.createElement('img');
			img.onload = function ()
			{
				$(".loader_gallery").hide();
				$(".big_foto").css("background-image", "url("+thisFoto+")");
				$(".big_foto").animate( { opacity:1 }, 700, null, function () {
					ifClick = 0;
				});
			}
			$(img).attr('src', thisFoto);
			$(".loader_gallery").show();
			$(".foto_border").removeClass("sel");
			thisFotoObj.addClass("sel");
			if ((visFirst > 1 || currentFoto == countFotos) && countFotos > 6) {
				$(".foto_list_all").animate( { left: leftVis }, 700);
				if (currentFoto == countFotos) {
					visFirst = countFotos - 5;
				} else {
					visFirst = visFirst - 1;
				}
			}
		});
	}
}
