
var page = 1;
var max_page = 1;
var current_index = 0;
var max_index = 0;

function loadImage() {
	var img_height	= parseInt($(".gallery_list li:eq("+current_index+") a span.height").html());
	var img_width	= parseInt($(".gallery_list li:eq("+current_index+") a span.width").html());
	var img_page	= parseInt($(".gallery_list li:eq("+current_index+") a span.page").html());
	var img			= $(".gallery_list li:eq("+current_index+") a").attr('href');
	var img_alt		= $(".gallery_list li:eq("+current_index+") a img").attr('alt');
	
	$("#preview img").fadeTo("fast",0.01,function() {
		
		if (img_page!=page) {
			page = img_page;
			galleryListRefresh();
		}
		
		$("#preview").attr('style','height:'+(img_height+10)+'px;');
		$("#preview img").attr('src','css/img/spacer.gif');
		$("#preview img").attr('alt','');
		$("#preview_title p").html('');
		
		$.ajax({
			type: "GET",
			dataType: "text",
			url: img,
			data: "",
			complete: function() {
				$("#preview img").attr('src',img);
				$("#preview img").attr('alt',img_alt);
				$("#preview_title p").html(img_alt);
				setTimeout('$("#preview img").fadeTo("fast",1.0)',800);
			}
		});
	});
}

function galleryListRefresh() {
	$(".gallery_pages a").removeClass('current');
	$(".gallery_pages a:eq("+page+")").addClass('current');
	
	$(".gallery_list li").each(function() {
		if (!$(this).hasClass("page_"+page)) {
			$(this).slideUp("fast");
		}
		else {
			$(this).slideDown("fast");
		}
	});
	
}

$(document).ready(function() {
	//tak na prawde potrzebne do stronicowania galerii
	galleryListRefresh();
	
	$(".gallery_pages a:eq(1)").addClass('current');
	
	max_index	= $(".gallery_list li").length - 1;
	max_page	= $(".gallery_pages a").length - 2;
	
	$("#preview img").click(function() {
		if (current_index==max_index) {
			current_index = 0;
		}
		else {
			current_index++;
		}

		loadImage();
	});
	
	$(".preview_navigate a").click(function() {
		if ($(this).hasClass("prev")) {
			//prev
			if (current_index==0) {
				current_index = max_index;
			}
			else {
				current_index--;
			}
		}
		else if ($(this).hasClass("next")) {
			//next
			if (current_index==max_index) {
				current_index = 0;
			}
			else {
				current_index++;
			}
		}
		
		/*var img_height	= parseInt($(".gallery_list li:eq("+current_index+") a span.height").html());
		var img_width	= parseInt($(".gallery_list li:eq("+current_index+") a span.width").html());
		var img_page	= parseInt($(".gallery_list li:eq("+current_index+") a span.page").html());
		var img			= $(".gallery_list li:eq("+current_index+") a").attr('href');
		loadImage(img,img_height,img_width,img_page);*/
		loadImage();
	});
	
	$(".gallery_pages a").click(function() {
		
		if ($(this).hasClass("prev")) {
			//prev
			if (page==1) {
				page = max_page;
			}
			else {
				page--;
			}
		}
		else if ($(this).hasClass("next")) {
			//next
			if (page==max_page) {
				page = 1;
			}
			else {
				page++;
			}
		}
		else {
			page = parseInt($(this).html());
		}

		galleryListRefresh();
	});
	
	$(".gallery_list li a").click(function() {
		var img			= $(this).attr('href');
		
		//aktualizujemy current_index
		var counter = 0;
		$(".gallery_list li").each(function() {
			if ($(this).find("a").attr('href')==img) {
				current_index = counter;
			}
			counter++;
		});
		//max_index = counter;
		
		loadImage();
		
		return false;
	});
});
