$(document).ready(function(){
	$("div.image a").bind("click", function(){
		_open($(this).find("img").attr("src").split("mid_")[0] + $(this).find("img").attr("src").split("mid_")[1]);
		return false;
	});
	
	$("a.loop").click(function(){
		_open($("div.image a img").attr("src").split("mid_")[0] + $("div.image a img").attr("src").split("mid_")[1]);
		return false;
	});
	
	$("div.image-block a").click(function(){
		now = numfoto($("div.image-block a"), $(this).attr("href"));
		_open($(this).attr("href"));
		return false;
	});
});

function _open(href) {

	var code, width, width_img, height, height_img, rejim, simv = 0, simvol = 0;

	if ($("div.backgroundDiv").length == 0) _build();
	else _show(150, 150, 0);			
	
	imgPreloader = new Image();
	imgPreloader.src = href;
	imgPreloader.onload = function(){
		code = '<a class="close" href="#">&nbsp;</a><div class="margin"><div class="lefthalf">&nbsp;</div><div class="righthalf">&nbsp;</div><div class="water">&nbsp;</div><img src="' + href + '" id="main_img"/></div><div class="btm"><a class="prev" href="#">&nbsp;</a><a class="next" href="#">&nbsp;</a></div>';
		width = imgPreloader.width + 16;
		height_img = imgPreloader.height;
		height = imgPreloader.height + 36;
		$("#info").html(code);
		window.setTimeout( function(){
			_show(width, height, 1);
			naves(width - 16, height_img);
		}, 1000);
	}
	
return false;
};

function __open(href) {
	
	href += '?fill=1';
	
	var code, width, width_img, height, height_img, rejim, simv = 0, simvol = 0;

	if ($("div.backgroundDiv").length == 0) _build();
	else _show(150, 150, 0);			
	
	$.get(href, function(data){

		code = '<a class="close" href="#">&nbsp;</a><div class="margin">' + data + '</div>';
		width = /*imgPreloader.width + 16*/ 670;
		height_img = /*imgPreloader.height*/ 380;
		height = /*imgPreloader.height + 36*/ 478;
		$("#info").html(code);
		window.setTimeout( function(){
			__show(width, height);
			naves(width - 16, height_img);
		}, 1000);

	 });
	
};

function __show(width, height){

	var pos = _getCenterPos(width, height);
	
	$("div.contentHolder .loaderIcon").hide();
	$("div.contentHolder").css("background-color", "#2A1307").css("border", "2px solid #A08161");
	$("div.contentHolder div.margin").css("margin", "12px");	
	$("div.contentHolder a.close").css("margin", "10px 10px 0px 0px");

	$('div.contentHolder').animate({
				'width': width,
				'height': height,
				'top': pos['top'],
				'left': pos['left']
			}, "normal", function(){
				$("div.contentHolder #info").fadeIn("normal", function(){
					sIFR.replaceElement(named({
						sSelector:"div.contentHolder h2",
						sFlashSrc:"img/aquarelle.swf",
						sColor:"#c8ad91",
						sLinkColor:"#c8ad91",
						sBgColor:"#c8ad91",
						sHoverColor:"#c8ad91",
						nPaddingTop:0,
						nPaddingBottom:0,
						sWmode:"transparent",
						sFlashVars:"textalign=left&offsetTop=0"
					}));
				});
			});
}


function _show(width, height, rejim){

	var pos = _getCenterPos(width, height + 36);
	
	if (!rejim) $("div.contentHolder #info").hide();
	else $("div.contentHolder .loaderIcon").hide();

	$('div.contentHolder').animate({
				'width': width,
				'height': height,
				'top': pos['top'],
				'left': pos['left']
			}, "normal", function(){
				if (rejim) $("div.contentHolder #info").fadeIn("normal");
				else $("div.contentHolder .loaderIcon").show();
			});
}

function naves(width, height){
	$('a.close').bind('click', function(){ _close(); return false; });
	
	$("div.lefthalf, div.righthalf").css({height: height, cursor: "pointer"});
	$("div.water").height(height);
	
	if ($("div.prev").length) var lngth = $("div.prev a").length;
	else var lngth = $("div.image-block a").length;	

	if (now == lngth - 1){
		$("a.next").attr("id", "Ldis").unbind("click").click(function(){return false});
		$('div.righthalf').css("cursor", "default");
	}
	else{
		$("a.next").removeAttr("id");
		$('div.righthalf').css("cursor", "pointer");
		$('a.next, div.righthalf').click(function(){
			if ($("div.prev").length) _open($("div.prev a").eq(now + 1).attr("href").split("mid_")[0] + $("div.prev a").eq(now + 1).attr("href").split("mid_")[1]);
			else _open($("div.image-block a").eq(now + 1).attr("href"));
			now++;
			return false;
		});		
	}
	
	if (now == 0){
		$("a.prev").attr("id", "Fdis").unbind("click").click(function(){return false});
		$('div.lefthalf').css("cursor", "default");
	}
	else{
		$("a.prev").removeAttr("id");
		$('div.lefthalf').css("cursor", "pointer");
		$('a.prev, div.lefthalf').click(function(){
			if ($("div.prev").length) _open($("div.prev a").eq(now - 1).attr("href").split("mid_")[0] + $("div.prev a").eq(now - 1).attr("href").split("mid_")[1]);
			else _open($("div.image-block a").eq(now - 1).attr("href"));			
			now--;
			return false;			
		});		
		
	}
}


function _build(){

	backgroundDiv = "<div class='backgroundDiv'></div>";
	$('body').append(backgroundDiv);
	$('div.backgroundDiv').css('height',$(document).height());		
	contentHolder = '<div class="contentHolder"><div class="loaderIcon">&nbsp;</div><div id="info"></div></div>';
		
	$('body').append(contentHolder);

	$('div.contentHolder').css({'opacity': 0});

	var pos = _getCenterPos($("div.contentHolder").width(), $("div.contentHolder").height() + 36);
	
	$('div.backgroundDiv').css('opacity',0).fadeTo('normal', 0.7, function(){
		$('div.contentHolder').css('opacity',0).fadeTo('normal', 1);
		$('div.contentHolder').css({left: pos['left'], top: pos['top']});	
	});

	window.setTimeout( function(){
		$('div.backgroundDiv').bind('click',function(){
			_close();
		});
	}, 1000);

};
	
function _close(){
	$('div.contentHolder, div.backgroundDiv').fadeOut('normal', function(){
			$('div.backgroundDiv').remove();
			$('div.contentHolder').remove();
	});
};
		
function _getCenterPos(w, h){
	
		var scrollPos = _getScroll();
			
		if($.browser.opera) {
			windowHeight = window.innerHeight;
			windowWidth = window.innerWidth;
		}else{
			windowHeight = $(window).height();
			windowWidth = $(window).width();
		};
			
		projectedTop = (windowHeight/2) + scrollPos['scrollTop'] - h/2;
		if (projectedTop < 0) projectedTop = 25;
		projectedLeft = (windowWidth/2) + scrollPos['scrollLeft'] - w/2;
		
		return {top: projectedTop, left: projectedLeft};
};

$(window).resize(function(){ if($("div.contentHolder").size() > 0) _reshow() });
$(window).scroll(function(){ if ($("div.contentHolder").height()+36 < $(window).height()) _reshow(); });

$(document).keyup(function(e){
	switch(e.keyCode){
		case 27:
			_close();
			break;
	};
});

function _reshow() {
	var pos = _getCenterPos($("div.contentHolder").width(), $("div.contentHolder").height() + 36);
	$("div.contentHolder").css("top", pos['top']).css("left", pos['left']);
}

function _getScroll(){
	scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
	scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
	return {scrollTop:scrollTop,scrollLeft:scrollLeft};
};
