var hash = window.location.hash.replace("#","");
var imageList = new Array();

$(function(){
	if(typeof(imageList) != 'undefined') imageList.pop();
	// centering the image
	//$(".photo a").each(function(i,e){ $(e).addClass("center").css({"marginLeft":-$(e).find("img").width()/2,"marginTop":-$(e).find("img").height()/2}) });

	// if photo page - process the hash
	if($(".photo").length){
		hashProcess();
	}

	// lightbox
	$(".photo img").live("click",function(e){ return loadBig(e); });
	
	// #nav .prev & #nav .next
	$("#nav .prev").click(function(){ return navTo(-1) });
	$("#nav .next").click(function(){ return navTo(1) });
	
	$(".thumbs a").click(function(e){ return thumbClick(e) });
	
	$(".thumbs a.prev").live("click",function(){ return thumbPage(-1); });
	$(".thumbs a.next").live("click",function(){ return thumbPage(1); });
	
	prepareThumbs();
	
	redrawNavi();
	
	// Внимательно слушаем клавишу Esc
	$(document).keydown(function(e) {
		if(e.which==27) closeBig();
	});
	
	$(".descr_text").fancybox({
			'autoDimensions'	: false,
			'width'				: 700,
			'height'			: 'auto'
		});
	
});

function pos(a){
	var href = a.find("img").attr("src");
	for(i=0;i<imageList.length;i++){
		if(imageList[i]==href){
			return i;
		}
	}
	return -1;
}

function displayImg(src, num){
	img = $("<img>").attr({"src":src});
//	a = $("<a>").attr({"href":src.replace(/-normal\./,"-large.")});
	a = $("<a>").attr({"href":imageListLarge[num]});
	var visible = $(".photo");
	visible.append(a.append(img));
}

function hashProcess(){
	if(!hash){
		window.location.hash = "#1";
	} else {
		navTo(hash-1);
	}
}

function setHash(e){
	var index = pos($(".photo a"));
	window.location.hash = "#"+(index+1);
}

function thumbClick(e){
	var visible = $(".photo a");
	var index = pos($(".photo a"));
	
	target = $(".thumbs a").not(".nav").index( $(e.target).parent() );
	navTo(target - index);
	return false;
}

var perpage = 10;

function prepareThumbs(){
	reset = false;
	
	var prev = $("<a>").addClass("nav prev");
	var next = $("<a>").addClass("nav next");
	
	var temp = 0;
	
	$(".thumbs a").each(function(i,e){
		if(i!=0){
			if((i+temp)%(perpage-1)==0){
				$(e).before(next.clone());
				$(e).before(prev.clone());
				temp++;
			}
		}
	});
}

var visible;
var index;
var total;
var page;

function redrawNavi(){
	index = pos($(".photo a"));
	total = imageList.length;
	
	prev = $("#nav .prev");
	next = $("#nav .next");
	
	if(index==0) prev.hide(); else prev.show();
	if(index==total-1) next.hide(); else next.show();
	
	$(".thumbs a").removeClass("active");
	$( $(".thumbs a").not(".nav")[index] ).addClass("active");
	
	thumbPage(0);
}

function thumbPage(direction){
	if(direction==1 || direction==-1){
		page = page + direction;
	} else {
		page = Math.floor((index) / perpage);
		page = Math.floor((index+((page)*2)+1) / perpage);
	}
	
	$(".thumbs a").each(function(i,e){
		var temp = Math.floor((i) / perpage);
		if( temp != page ){
			$(e).hide();
		} else {
			$(e).show();
		}
	});
	return false;
}

function navTo(direction){
	visible = $(".photo a");
	index = pos($(".photo a"));
	
	target = imageList[index+direction];
	
	visible.remove();
	
	displayImg(target, index+direction);
	
	redrawNavi();
	
	setHash(target);
	
	return false;
}

/* lightbox */

	var href;
	var fog;
	var loading;

	function loadBig(e){
		target = $(e.target);
		href = $(target).parent().attr("href");

		fog = $("<div>").addClass("fog");
		loading = $("<div>").addClass("loading");		
		$("body").append(fog.append(loading));
		fog.click(function(){ closeBig(); });
		
		var tmpImg = new Image();
		tmpImg.src = href;
		tmpImg.onload = bigPicLoaded;

		return false;
	}

	function bigPicLoaded(){
		w = this.width;
		h = this.height;
		r = w/h;

		fw = $(fog).width();
		fh = $(fog).height();
		fr = fw/fh;
		
		w = (w>fw)?fw-40:w;
		h = (h>fh)?fh-40:h;

		if(r<fr){
			w = h*r;
		} else {
			h = w/r;
		}
		
		var container = $("<div>").addClass("lightbox").css({"width":w,"height":h,"marginLeft":-(w+20)/2,"marginTop":-(h+20)/2});
		container.click(function(){ closeBig(); });
		var img = $("<img>").attr({"src":href,"width":w,"height":h});
		container.append(img);
		
		
		$("body").append(container);
	}

	function closeBig(){
		$(".lightbox").remove();
		$(".fog").remove();
	}

/* lightbox */




