var subWind,yPos;

function activate(){ 
	getScroll();
	prepareIE(window,'100%', 'hidden');
	if (document.all){
		//alert("IE");
		//setScroll(window,0,0);
		//hideSelects(window,'hidden');
	}
}
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
function prepareIE(objWin,height, overflow){
	bod = objWin.document.getElementsByTagName('body')[0];
	bod.style.height = height;
	bod.style.overflow = overflow;
	if (document.all){
		htm = objWin.document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
		htm.style.overflowX = 'visible';
	}
}
	
	// In IE, select elements hover on top of the lightbox
function hideSelects(objWin,visibility){
	selects = objWin.document.getElementsByTagName('select');
	for(i = 0; i < selects.length; i++) {
		selects[i].style.visibility = visibility;
	}
}	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
function getScroll(){
		if (self.pageYOffset) {
			yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			yPos = document.body.scrollTop;
		}
	}
	
function setScroll(objWin,x, y){
		objWin.scrollTo(x, y); 
}
	
function addLightboxMarkup() {
	var bod	= document.getElementsByTagName('body')[0];
	var overlay	= document.createElement('div');
	overlay.id = 'overlay';
	overlay.style.display = 'none';
	overlay.style.position = 'absolute';
	overlay.style.top = 0;
	overlay.style.left = 0;
	overlay.style.width = '120%';
	overlay.style.height = '100%';
	overlay.style.zIndex = 1000;
	//overlay.style.backgroundColor = '#C0C0C0';
	//overlay.style.-moz-opacity = 0.8;
	//overlay.style.opacity = .80;
	//overlay.style.filter = 'alpha(opacity=80)';
	bod.appendChild(overlay);
}

function winOpen(width,height,url,windowFeatures)  {
	activate();
	var oOverlay = document.getElementById('overlay');
	oOverlay.style.display = "block";
	oOverlay.onclick=overlayClick;
	oOverlay.ondblclick=overlayClick;
	openCenteredWindow(width,height,url,windowFeatures);
}

function overlayClick(){
	//alert('click');
	if (!(!subWind || subWind.closed)) {
		subWind.focus();
	}
}

function openCenteredWindow(width,height,url,windowFeatures) {
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    //windowFeatures = "width=" + width + ",height=" + height + 
    //    ",status,resizable,left=" + left + ",top=" + top + 
    //    ",screenX=" + left + ",screenY=" + top;
		var tempFeatures = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top
										 + ",screenX=" + left + ",screenY=" + top 
										 + ",titlebar=0" ;
    if (windowFeatures != "")
			tempFeatures = tempFeatures + "," + windowFeatures;
		//alert(tempFeatures);
    subWind = window.open(url, "subWind", tempFeatures);
}

function windClose(objWin){
	var oOverlay = objWin.opener.document.getElementById('overlay');
	oOverlay.style.display = "none";
	if (document.all){
		//hideSelects(objWin.opener,"visible");
	}
	setScroll(objWin.opener,0,objWin.opener.yPos);
	prepareIE(objWin.opener,"auto", "auto");
	window.close();
}

