$(function (){
	$(".light-box").lightBox();
})
function preview(src,text){
	if (!src) return;
	$(document.body).append('<div id="popupMask"></div><div id="popupContainer"><table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%"><tr><td align="center" style="text-align:center;vertical-align:middle"><div id="popupContent"><div id="popupHeader"><a href="" id="popupClose">Закрыть</a></div><div id="popupImgCont"><a href="" id="prev"></a><img src="'+host+'/images/ajax-loader.gif" width="32px" height="32px" id="popupImg"><a href="" id="next"></a></div></div></td></tr></table></div>');
	
	$("#popupMask")
		.css("width",$(document).width())
		.css("height",$(document).height())
		.css("opacity",0.4)
		.fadeIn("slow");
	$("#popupContainer")
		.css("left",getBodyScrollLeft())
		.css("top",getBodyScrollTop())
		.fadeIn("slow");
	$("#popupClose").click(hidePreview);
	$("#prev").click(prevImage);
	$("#next").click(nextImage);
	img = new Image();
	img.alt = text
	img.onload = imgPreviewOnLoad;
	img.src = src;
	
	$(window).bind("resize",resizeWnd)
}
function resizeWnd(){
	$("#popupMask")
		.css("width",$(document).width())
		.css("height",$(document).height())
}
function imgPreviewOnLoad(){
	imgWidth  = this.width;
	imgHeight = this.height;
	src		  = this.src;
	alt 	  = this.alt;
	wndWidth   = document.body.clientWidth;
	wndHeight  = document.body.clientHeight;
	if (imgWidth > wndWidth){
		imgHeight = parseInt(imgWidth * (wndWidth / imgWidth));
		imgWidth  = parseInt(wndWidth);
	}
	
	if (imgHeight > wndHeight){
		
		imgWidth  = parseInt(imgWidth * (wndHeight / imgHeight));
		imgHeight = wndHeight;
	}
	$("#popupFooter").html(alt)
	
	
	contWidth   = $("#popupContent").width() - 32 + imgWidth;
	contHeight  = $("#popupContent").height() - 32 + imgHeight;
	$("#popupContent")
		.animate({"width":contWidth},1500,function (){
			$(this).animate({"height":contHeight},1500,function(){
				$("#popupImg")
					.hide()
					.attr("src",src)
					.attr("alt",alt)
					.attr("title",alt)
					.width(imgWidth)
					.height(imgHeight)
					.fadeIn(1000/*,function(){
						$("#popupFooter")
							.slideDown("slow");
					}*/);
				$("#prev").width(parseInt(imgWidth/2)).height(parseInt(imgHeight)).show();
				$("#next").width(parseInt(imgWidth/2)).height(parseInt(imgHeight)).css("marginLeft",-1*(parseInt(imgWidth/2)-20)).show();
				$("#popupMask")
					.css("width",$(document).width())
					.css("height",$(document).height())
			});
		});
}
function prevImage(event){
	return false;
}
function nextImage(event){
	return false;
}
function hidePreview(){
	$("#popupContainer").fadeOut("slow",function(){
		$(this).remove();
		$("#popupMask").fadeOut("slow",function(){$(this).remove()});
	})
	return false;
}
function getBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}
