var popupStatus = 0;

//loading popup with jQuery
function loadPopup(obj,loadfunction){

	centerPopup(obj);
	
	//loads popup only if it is disabled
	if(popupStatus==0)
	{
		var ie6 =  /msie|MSIE 6/.test(navigator.userAgent);
		$("#backgroundPopup").css({"opacity": (ie6?"1":"0.7")});
		$("#backgroundPopup").fadeIn("slow",function() {$(obj).show();if (loadfunction) loadfunction()});	
		
		//$(obj).fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(fast){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#lightbox .lightboxContent").unbind();
		$("#lightbox").hide();
		$("#lightbox .lightboxContent").html("");
		
		if (fast)
		{
			$("#backgroundPopup").hide();
		}
		else
		$("#backgroundPopup").fadeOut("slow");
		
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(obj){
	var popup = obj//$("#popupContact");

	var objcenterleft = GetTopLeft(obj).Left + (obj.offsetWidth/2);
	var objcentertop = GetTopLeft(obj).Top + (obj.offsetHeight/2);

	
	
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = window.innerHeight;
	
	var pageyoffset = window.pageYOffset;
	
	var popupHeight = $(popup).height();
	var popupWidth = $(popup).width();

	
	if(windowHeight==undefined) windowHeight = getDocHeight();
	if(pageyoffset == undefined) pageyoffset= obj.offsetTop;
	if(!obj.offsetLeft) objcenterleft = (windowWidth/2);
	if(!obj.offsetTop) objcentertop = (windowHeight/2);
	

	var newx = objcenterleft - (popupWidth/2);
	var newy = objcentertop - (popupHeight/2);

	//centering
	$(popup).css({
		"position": "absolute",
		//"top": newy,
		"left": newx
	});	
	
	
	/*
	if((newy + popupHeight)>windowHeight)
	{
		//set popup bottom to windowbottom
		newy = (windowHeight - popupHeight);
		$(popup).css({"top":newy});
	}

	if(newy<pageyoffset)
	{
		//set popup top to windowtop
		newy = pageyoffset;
		$(popup).css({"top":newy});	
	}
	*/
	/*
	var newh = (windowHeight*0.8);
	$(popup).css("height", newh+"px");
	$(".lightboxContent",popup).css("height", newh+"px");
	$(".item",popup).css("height", (newh-70)+"px");
	*/
	/*
	var newm = $(".actions", popup).css("height");
	$(".item", popup).css("height", (newh - 60) + "px");
	
	var bla = getElementTop($("#lightbox:first")[0]);
	alert(bla.Top);
	
	$("#lightboxfooter").css({"top":(newh) + "px","left":newx});
	*/
	
	$("#backgroundPopup").css({
		"height": windowHeight,
		"width":windowWidth
	});
	
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function GetTopLeft(elm)

{
var x, y = 0;
x = elm.offsetLeft;
y = elm.offsetTop;
elm = elm.offsetParent;
while(elm != null)
{
x = parseInt(x) + parseInt(elm.offsetLeft);
y = parseInt(y) + parseInt(elm.offsetTop);
elm = elm.offsetParent;
}

return {Top:y, Left: x};
}

$(document).ready(function(){
	
	//Click the x event!
	$("#lightboxClose").click(function(){
		disablePopup();
	});
	
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});

function centerPopupv2(obj)
{
var height = $(obj).height();
	var docheight = getDocHeight();
	
	var width = $(obj).width();
	var docwidth = document.documentElement.clientWidth;
	
	$(obj).css({
		'top' : ((docheight - height)/2) + 'px'
		,'left' : ((docwidth - width)/2)+'px'
		});
}
