
/*------------------------------------------------------------------------------
	Overlay
------------------------------------------------------------------------------*/


var overlayLoaded = false;
var popupVisible = false;
var popupImages = new Array();
var popupWidth = 1024;
var popupHeight = 800;

var overlayPub = null;
var overlayAction = null;



function showImagePopup(src)
{
	// show overlay
	showOverlay();

	// preload image
	imgPopup = new Image();
	imgPopup.onload = function()
	{

		var imgWidth = imgPopup.width;
		var imgHeight = imgPopup.height;

		if (imgWidth > popupWidth)
		{
			imgWidth = popupWidth;
			imgHeight = imgHeight * (imgWidth / popupWidth);
		}
		if (imgHeight > popupHeight)
		{
			imgHeight = popupHeight;
			imgWidth = imgWidth * (imgHeight / popupHeight);
		}
		
		// add image to popup
		showPopup(imgWidth, imgHeight, '<img src="'+src+'" id="popupImage" width="'+imgWidth+'" height="'+imgHeight+'">');

		imgPopup.onload=null;	//	clear onLoad, IE behaves irratically with animated gifs otherwise
	}
	imgPopup.src=src;
}

function showPubPopup(pub_Id)
{
	$.ajax({
		url:	'/dynamics/getpub.php',
		data:	{pub_Id: pub_Id},
		success: function(content)
		{
			showOverlay();
			showPopup(532, 600, content);
		},
		error: function()
		{
			location.href='/publicaties/'+pub_Id;
		}

	});
}
function setForm(form_Id)
{
	$.ajax({
		type: 'POST',
		url: '/dynamics/getoverlay.php?pub_Id='+overlayPub+'&action='+overlayAction,
		data: $('form#'+form_Id+' :input').serialize(),
		success: function(content){
			showPopup(popupWidth, popupHeight, content);
			$('#overlay form').validatorEnable({ajaxHandler: setForm});
		},
		error: ajaxError
	});
	return false;
}

function ajaxError()
{
	alert('Failed to execute ajax request');
}

function showOverlay()
{
	if (!overlayLoaded)
	{
		overlayLoaded = true;

		// hide select box (ie6 bug)
		if ($.browser.msie) $('input').hide();

		// show overlay
		$('<div id="overlay"><div id="overlay_mask"></div><div id="overlay_load"></div><div id="overlay_popup"><a href="javascript:hidePopup()" id="popupClose"></a><div id="popup"></div></div></div>').appendTo('body');

		$('#overlay_mask').click(function(){ removeOverlay(); });

		// stretch to full height & width
		$('#overlay_mask')
			.height(document.documentElement.scrollHeight > $('html').height() ? document.documentElement.scrollHeight : $('html').height())
			.width(document.documentElement.scrollWidth);
		$('#overlay')
			.css('padding-top', document.documentElement.scrollTop+'px');

	}
}

function removeOverlay()
{
	if (overlayLoaded)
	{
		overlayLoaded = false;
		popupVisible = false;

		// remove overlay
		$('#overlay').remove();
		if ($.browser.msie) $('input').show();
	}
}

function showPopup(pWidth, pHeight, pContent)
{
	$('#popup').html(pContent);

	$('#overlay_load').hide();

	animatePopup(pWidth, pHeight);
}

function animatePopup(pWidth, pHeight)
{
	if (!popupVisible)
	{
		popupVisible = true;

		var marginTop = parseInt(($('html').height() - pHeight) / 2);
		//var marginTop = 180;

		// set margin top
		$('#overlay_popup').css({top: marginTop+'px'}).height(5).width(0);
		$('#popup').hide();

		// show popup
		$('#overlay_popup').animate({
			width: pWidth
		}, null, null, function(){
			$('#overlay_popup').animate({height: pHeight}, null, null, function()
			{
				if ($('#popupImage').length)
				{
					$('#popup').show();
					$('#popupImage').animate({opacity: 'toggle'});
					$('#popupClose').animate({opacity: 'toggle'});
				}
				else
				{
					$('#popup').height(pHeight).animate({opacity: 'toggle'});
					$('#popupClose').show();
				}

			});
		});
	}
	else
	{
		$('#popup').show();
	}
}

function hidePopup()
{
	var width = $('#overlay_popup').width();
	var height = $('#overlay_popup').height();

	$($('#popupImage').length ? '#popup #popupImage' : '#popup').animate({opacity: 'toggle'}, null, null, function()
	{
		$('#overlay_popup').html('').animate({height: 5}, null, null, function()
		{
			$('#overlay_popup').animate({width: 'toggle'}, null, null, function()
			{
				removeOverlay();
			});
		});
	});
}
