$(document).ready(function(){

	$("td.td_img_description a").bind("click", function(){
		_open($(this));
		return false;
	});
});

function _open($el) {

		var code, width, height, rejim, size, lnk = '', i = 1, neg, size;
	
		_build();
		imgPreloader = new Image();
		imgPreloader.src = $el.attr("href");
		imgPreloader.onload = function(){
			
			size = _fitToViewport(imgPreloader.width, imgPreloader.height);
			
			width = size['contentWidth'];
			height = size['contentHeight'];
			
			$("div.link_foto a").each(function(){
				if ($(this).attr("href") == $el.attr("href")) neg = i;
				lnk += '<a href="' + $(this).attr("href") + '">' + i + '</a>';
				i++;
			});
			
			code = '<div class="margin"><div class="clear"><a href="#" class="close"><img src="img/prettyPhoto/btnClose.gif"/></a><a href="#" class="btnexpand">&nbsp;</a><a href="#" class="btncontract">&nbsp;</a></div><a href="#" class="mnfoto"><img id="main_img" src="' + $el.attr("href") + '" /></a><div class="navig_info">' + lnk + '</div></div>';

			$("#info").html(code);
			
			$("#info img#main_img").css({width: size["width"], height: size["height"]});
			
			$("a.btnexpand").bind("mouseover", function(){$(this).css("background-position", "0px -21px");});
			$("a.btnexpand").bind("mouseout", function(){$(this).css("background-position", "0px 0px");});
			$("a.btncontract").bind("mouseover", function(){$(this).css("background-position", "0px -21px");});
			$("a.btncontract").bind("mouseout", function(){$(this).css("background-position", "0px 0px");});
			
			if (size['resized']){
				
				$("a.btnexpand").css("display", "inline");
			
				$("a.btnexpand").bind("click", function(){
					$("#main_img").width(imgPreloader.width).height(imgPreloader.height);
					$("#info").css("display", "none");
					_show(imgPreloader.width + 20, imgPreloader.height + 70, 1);
					$("a.btnexpand").css("display", "none");
					$("a.btncontract").css("display", "inline");
					return false;
				});
				
				$("a.btncontract").bind("click", function(){
					$("#main_img").width(size['width']).height(size['height']);
					$("#info").css("display", "none");
					_show(size['contentWidth'], size['contentHeight'], 1);
					$("a.btncontract").css("display", "none");
					$("a.btnexpand").css("display", "inline");
					return false;
				});
			}
			
			$("div.navig_info a").eq(neg - 1).addClass("negative");
			
			window.setTimeout( function(){
				_show(width, height, 1);
				$("#info div.navig_info").fadeIn("normal");		
				$("#info div.navig_info a").bind("click", function(){
					re_open($(this));
					return false;
				});					
			}, 1000);

			neg = 0;

			$("a.mnfoto").click(function(){
				if ($("a.negative").next().length) re_open($("a.negative").next());
				else _close();
				return false
			});
			
			$('a.close').bind('click', function(){ _close(); return false; });
		}

	return false;
}

function re_open ($el){
	
	var size;

	$("#info div.navig_info a").unbind("click");
	imgPreloader2 = new Image();
	imgPreloader2.src = $el.attr("href");
	$("div.contentHolder .loaderIcon").css("display", "block");
	$("div.contentHolder .loaderIcon").css("left", $("div.contentHolder").width()/2-50).css("top", $("div.contentHolder").height()/2-50);
	$("#info").css("display", "none");
	imgPreloader2.onload = function(){

		size = _fitToViewport(imgPreloader2.width, imgPreloader2.height);
			
		width = size['contentWidth'];
		height = size['contentHeight'];
		
		$("#info img#main_img").css({width: size["width"], height: size["height"]});		
		
		_show(width, height, 1);
		
			$("a.btnexpand").hide().bind("mouseover", function(){$(this).css("background-position", "0px -21px");});
			$("a.btnexpand").bind("mouseout", function(){$(this).css("background-position", "0px 0px");});
			$("a.btncontract").hide().bind("mouseover", function(){$(this).css("background-position", "0px -21px");});
			$("a.btncontract").bind("mouseout", function(){$(this).css("background-position", "0px 0px");});
			
			if (size['resized']){
				
				$("a.btnexpand").css("display", "inline");
			
				$("a.btnexpand").bind("click", function(){
					$("#main_img").width(imgPreloader2.width).height(imgPreloader2.height);
					$("#info").css("display", "none");
					_show(imgPreloader2.width + 20, imgPreloader2.height + 70, 1);
					$("a.btnexpand").css("display", "none");
					$("a.btncontract").css("display", "inline");
					return false;
				});
				
				$("a.btncontract").bind("click", function(){
					$("#main_img").width(size['width']).height(size['height']);
					$("#info").css("display", "none");
					_show(size['contentWidth'], size['contentHeight'], 1);
					$("a.btncontract").css("display", "none");
					$("a.btnexpand").css("display", "inline");
					return false;
				});
			}
			else $("a.btnexpand, a.btncontract").hide();
		

		$(".negative").removeClass("negative");
		$el.addClass("negative");
		$("#info img#main_img").attr("src", $el.attr("href"));
		window.setTimeout( function(){$("#info").fadeIn("normal");}, 1000);
		$("#info div.navig_info a").bind("click", function(){
			re_open($(this));
			return false;
		});		
		$('a.close').bind('click', function(){ _close(); return false; });
		$('a#negative').bind("click", function (){ return false});
	}
}

function _show(width, height, rejim){
	
	var pos = _getCenterPos(width, height);
	
	if (!rejim) $("div.contentHolder #info").css("display", "none");
	else $("div.contentHolder .loaderIcon").css("display", "none");

	$('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").fadeIn("normal");
			});
}

function _build(){
	
	if($.browser.msie && $.browser.version == 6){
		$('select').css('visibility','hidden');
	};

	backgroundDiv = "<div class='backgroundDiv'></div>";
	$('body').append(backgroundDiv);
	$('div.backgroundDiv').css('height',$(document).height());		
	contentHolder = '<div class="contentHolder"><div class="loaderIcon"></div><div id="info"></div></div>';
		
	$('body').append(contentHolder);

	$('.contentHolder').css({'opacity': 0});

	var pos = _getCenterPos($("div.contentHolder").width(), $("div.contentHolder").height());
	
	$('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();
			$("select").css("display", "inline");
	});
	
};
		
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;
		if (projectedTop + h > $(document).height()) projectedTop = $(document).height() - h - 50;		
		projectedLeft = (windowWidth/2) + scrollPos['scrollLeft'] - w/2;
		
		return {top: projectedTop, left: projectedLeft};
};

$(window).resize(function(){ if($(".contentHolder").size() > 0) _reshow() });
$(window).scroll(function(){ if ($("div.contentHolder").height() < $(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());
	$("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};
};

function _fitToViewport(width,height){
	hasBeenResized = false;
		
	contentHeight = height + 70;
	contentWidth = width + 20;
	
	imageWidth = width;
	imageHeight = height;

	if($.browser.opera) {
		windowHeight = window.innerHeight;
		windowWidth = window.innerWidth;
	}else{
		windowHeight = $(window).height();
		windowWidth = $(window).width();
	};
		
	if ((contentWidth > windowWidth) || (contentHeight > windowHeight)) {
		hasBeenResized = true;

		var xscale = (contentWidth + 100) / windowWidth;
		var yscale = (contentHeight + 100) / windowHeight;

		if (yscale>xscale){
			imageWidth = Math.round(width * (1/yscale));
			imageHeight = Math.round(height * (1/yscale));
		} else {
			imageWidth = Math.round(width * (1/xscale));
			imageHeight = Math.round(height * (1/xscale));
		};
		
		contentHeight = imageHeight + 67;
		contentWidth = imageWidth + 20;
	};

	return {
		width:imageWidth,
		height:imageHeight,
		contentHeight:contentHeight,
		contentWidth:contentWidth,
		resized:hasBeenResized
	};
};