var popupState = 'closed';
var popupWindow = new Object();
var popupActive = '';
var currentTotalHeight = 0;
var currentTotalWidth = 0;

popupWindow.Start = function(popupId) { 
  var popupId = arguments[0];
  /* $$('object').each(function (s) { s.hide(); }); // hide the flash!! */
	popupWindow.Open(popupId);
	currentTotalHeight = getTotalHeight();
	currentTotalWidth = getTotalWidth();
	Event.observe(window, 'resize', function () { popupWindow.Change(); }); 
};

popupWindow.contentPos = function() { 
  $(popupActive).show();
	var windowHeightHalf = (getWindowHeight() / 2);
	var contentHeightHalf = (getOffsetHeight($('popup_content')) / 2);
	var windowWidthHalf = (getWindowWidth() / 2);
	var contentWidthHalf = (getOffsetWidth($('popup_content')) / 2);
	$('popup_content').style.top = (windowHeightHalf - contentHeightHalf + getScrollTop()) + 'px';
	$('popup_content').style.left = (windowWidthHalf - contentWidthHalf) + 'px';
};

popupWindow.Open = function(popupId) { 
  if (popupState == 'closed') {
	  popupActive = popupId;
		$('popup_area').show();
		$('popup_area').style.width = '100%';
		$('popup_area').style.height = getTotalHeight() + 'px';
		new Effect.Appear('popup_bg', { 
		  beforeStart: function() { 
			  $('popup_bg').style.width = '100%'; 
				$('popup_bg').style.height = getTotalHeight() + 'px';	
		  }, duration: 0.5, from: 0, to: 0.40, 
			afterFinish: function() { new Effect.Appear('popup_content', { 
			  beforeStart: function() { setTimeout('popupWindow.contentPos()', 50) }, duration: 0.5, from: 0, to: 1, 
				afterFinish: function() { Event.observe($('popup_bg'), 'click', popupWindow.Close ) } });
			}});
		popupState = 'open';
  } else {
	  new Effect.Fade('popup_content', { duration: 0.5, from: 1, to: 0, afterFinish: function() { 
		  $(popupActive).hide();
			new Effect.Fade('popup_bg', { duration: 0.5, from: 0.4, to: 0,
			  afterFinish: function() { $('popup_area').hide(); popupActive = ''; popupState = 'closed'}
			});
		}});
  } 
};

popupWindow.Close = function(e) { 
  var theElement = Event.element(e);
	if (theElement.id == 'popup_bg') { popupWindow.Open(); }
};

popupWindow.Change = function() { setTimeout('popupWindow.ChangeHeight()', 50); };

popupWindow.ChangeHeight = function() { 
  if (currentTotalHeight>getWindowHeight()) { 
	  $('popup_bg').style.height = getTotalHeight() + 'px';
		$('popup_area').style.height = getTotalHeight() + 'px'
  } else { 
	  $('popup_bg').style.height = getWindowHeight() + 'px';
		$('popup_area').style.height = getWindowHeight() + 'px';
  }
	setTimeout('popupWindow.ChangeWidth()', 100);
};

popupWindow.ChangeWidth = function() { 
  $('popup_bg').style.width = '100%'; 
	$('popup_area').style.width = '100%'; 
	popupWindow.contentPos(); 
};

function getWindowWidth() { return window.offsetWidth || document.documentElement.clientWidth; }
function getWindowHeight() { return window.offsetHeight || document.documentElement.clientHeight; }
function getTotalWidth() { return (window.innerWidth+window.scrollMaxX) || document.body.clientWidth; }
function getTotalHeight() { return (window.innerHeight+window.scrollMaxY) || document.body.clientHeight; }
function getScrollWidth() { return getTotalWidth() - getWindowWidth(); }
function getScrollHeight() { return getTotalHeight() - getWindowHeight(); }
function getScrollTop() { return document.documentElement.scrollTop || document.body.scrollTop; }
function getScrollLeft() { return document.documentElement.scrollLeft || document.body.scrollLeft; }
function getOffsetWidth(obj) { return obj.offsetWidth; }
function getOffsetHeight(obj) { return obj.offsetHeight; }