/*************************************************************************
	does not current center, but uses fixed TOP governed by gPopupTopDown
	because height is indeterminant on first call, and known on subsequent
	causing some movement on first scroll
*/
var gPopupContainerId;			
var gPopupIsShown = false;			/* popup currently being shown */
var gPopupTopDown = 0;//250;			/* top down in containing containing div */


var gPopupTop = -100;
var gPopupLeft = -65;				// for fixed width containers

addEvent(window, "resize", centerPopWin);
addEvent(window, "scroll", centerPopWin);

function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent){	/* IE 5, 6 */
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 

	return window.undefined; 
}
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function showPopup( id, width, height ) {
	var sameContainer = ( id == gPopupContainerId );
	if( gPopupIsShown ) hidePopup();
	if( sameContainer ) return; // e.g., clicking on the original link to close

	gPopupIsShown = true;
	gPopupContainerId = id;
	centerPopWin( width, height );
	Effect.Appear( id );
}

function hidePopup() {
	gPopupIsShown = false;
	Effect.Fade( gPopupContainerId );
	gPopupContainerId = null;
}

function centerPopWin( width, height ) {
	if( gPopupIsShown ) {
		if( width == null || isNaN( width ) || width == 0 ) {
			width = gPopupContainerId.clientWidth;
			if( width == 0 ) width = 400;
		}
		if( height == null || isNaN( height ) || height == 0 ) {
			height = gPopupContainerId.clientHeight;
			if( height == 0 ) height = 200;
		}
		
		var aScrollOffsets = getScrollXY();
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		
		gPopupContainerId.style.top = gPopupTop + ( aScrollOffsets[1] + (( fullHeight /*- height*/ ) / 2 ) - gPopupTopDown )+ "px";
		gPopupContainerId.style.left = gPopupLeft + ( aScrollOffsets[0] + (( fullWidth - width ) / 2 )) + "px";
/**
		alert( 
				'element h/w: ' + height + ':' + width 
				+ '\nfull h/w: ' + fullHeight + ':' + fullWidth  
				+ '\nscrolltop/left: ' + aScrollOffsets[1] + ':' + aScrollOffsets[0]
				+ '\ntop /left: ' + gPopupContainerId.style.top + ':' + gPopupContainerId.style.left  
		);
/**/
	}
}
