/*
 * ----------------------------------------------------------------------------
 * $Id: shields.js,v 1.8 2009/03/01 08:58:55 stephan Exp $
 * ----------------------------------------------------------------------------
 * $Log: shields.js,v $
 * Revision 1.8  2009/03/01 08:58:55  stephan
 * completely jQuery based
 * calculate all on css offsets and sizes
 *
 * Revision 1.7  2009/02/25 17:13:43  stephan
 * use jquery keydown
 *
 * Revision 1.6  2009/02/20 19:03:24  stephan
 * almost all non jquery code removed. os() needs to be removed later.
 *
 * Revision 1.5  2009/02/18 19:36:59  stephan
 * better positioning. Remember top and left in css!
 *
 * Revision 1.4  2009/02/17 20:25:21  stephan
 * height and width from css
 *
 * Revision 1.3  2009/02/17 19:20:04  stephan
 * only call shieldsSavedKeydown if it is a function
 *
 * Revision 1.2  2009/02/17 19:06:08  stephan
 * control recursion.
 *
 * Revision 1.1  2008/07/19 14:46:04  stephan
 * Initial revision
 *
 * ----------------------------------------------------------------------------
 */

var shieldLevel = 0;

function showPopbox(callback) 
{
	var samesize = 0;

	if(shieldLevel)
		return;

	shieldLevel++;

	if(typeof document.body.style.maxHeight === "undefined")
		$('html').css('overflow', 'hidden');

	$('body').append('<div id="overlay"></div>');
	$('body').append('<div id="popbox"></div>');

	if($('#popbox').height() >= $('#overlay').height())
		samesize = 1;
		
	$('#overlay').css('height', $(document).height()+'px')
		.addClass('overlay').click(hidePopbox);

	if(samesize)
		$('#popbox').css('height', $('#overlay').height()+'px');

	$(window).keydown(function(e)  
	{
		keycode = (e == null) ? event.keyCode : keycode = e.which;
		if (keycode == 27) 
			hidePopbox();
	});

	var left = ($('#overlay').width() - $("#popbox").width()) / 2;
	var top = (($('#overlay').height() - $("#popbox").height()) / 2) + (!samesize * $(document).scrollTop());

	top = (top < 0) ? 0 : top;

	$('#popbox').css('left', (left < 0 ? 0 : left) + 'px');
	$('#popbox').css('top', (top < 0 ? 0 : top) + 'px');

        $('#overlay').css('opacity', 0).fadeTo('slow', 0.75, function () {
                        $('#popbox').fadeIn('fast', function () {
				if(typeof callback === "function")
					callback('#popbox');
                        });
                });
}

function hidePopbox() 
{
	if(shieldLevel)
		shieldLevel--;

	$('#popbox').fadeOut('fast');
	$('#overlay').fadeOut('fast', function () {
		$('#popbox,#hideselect,#overlay').remove();
		$('html').css('overflow', '');
	});
}
