var timeOut = 15;    // Initial time to Close window after __ number of seconds?
var timeRemain = 0;  // Remaining time to Close window after __ number of seconds?
var refreshRate = 1; // Check to close window every __ number of seconds?
var popupOpen = new Object();
var boxHeight = 0;
var boxWidth = 0;
var theDiv = new Array();
var doRefresh = false; //if set to true page will refresh when auto
var closeMsg = 'box will close in ';
var blanketOn = new Object();
var countDownCounter = 0;
var countDownId = new Object();

function ResetTimer(w) {
	timeRemain = timeOut;
	document.getElementById('count_down' + countDownId[w]).innerHTML = closeMsg +  timeRemain + ' seconds'; 
}

function CheckClose(w) {
	timeRemain -= (refreshRate);
	if (timeRemain > 0) {
		if ( popupOpen[w] == true ) {
			document.getElementById('count_down' + countDownId[w]).innerHTML = closeMsg +  timeRemain + ' seconds'; 
			setTimeout("CheckClose('" + w + "');",refreshRate * 1000);
		}
	} else {
		if ( popupOpen[w] == true ) {
			doToggle(w);
			if ( doRefresh == true ) {
				location.reload(true);
			}
		}
	}
	
}

function doToggle(w, remove) {
	var el = document.getElementById(w);
	if (el != null) {
		toggleOff(w);
		theDiv.pop(w);
		if ( blanketOn[w] == true ) {
			toggleOff('blanket');
			blanketOn[w] = false;
		}
		if (remove) {
			el.parentNode.removeChild(el);
		}
	}
}

function fakePopUp(windowId,content,bgColor,fontColor,h,w,loc,blanket,timed,timeUp, title) {
	blanketOn[windowId] = blanket;
	var bod = document.getElementsByTagName('body')[0];
	if ( !document.getElementById('blanket') ) {
		var bla = document.createElement("div");
		bla.id = 'blanket';
		if ( blanket == true ) {
			bla.style.backgroundColor='#808080'; //grey
		} else {
			bla.style.backgroundColor='';
		}
		bla.style.display = 'none';
		bla.style.height = '787px';
		if ( document.all) {
			bla.style.filter = "alpha(opacity=30)";
		} else {
			bla.style.opacity = '0.3';
		}
		bla.style.top = '0px';
		bla.style.width = '100%';
		bla.style.zIndex = 9001;
		bla.style.position = 'absolute';
		bod.appendChild(bla);
	} else {
		var tempBlanket = document.getElementById('blanket');
		if ( blanket == true ) {
			tempBlanket.style.backgroundColor='#808080'; //grey
		} else {
			tempBlanket.style.backgroundColor='';
		}
	}
	
	if ( !document.getElementById(windowId) ) {
		countDownId[windowId] = ++countDownCounter;
		var bod = document.getElementsByTagName('body')[0];
		
		var popBox = document.createElement('div');
		popBox.id = windowId;
		popBox.style.backgroundColor = bgColor;
		//popBox.style.height = h + 'px'; //NOTE: the height gets applied to the contentHolder
		boxHeight = h;
		popBox.style.width = w +'px';
		popBox.style.zIndex = '9002';
		popBox.style.display = 'none';
		popBox.style.position = 'absolute';
		
		var rTop = document.createElement('b');
		rTop.id = 'rTop' + countDownId[windowId];
		rTop.style.display = 'block';
		if ( blanket == true ) {
			rTop.style.backgroundColor = '#DDDDDD';
		} else {
			rTop.style.backgroundColor = 'white';
		}
		
		var top1 = document.createElement('b');
		top1.style.height = '1px';
		top1.style.overflow = 'hidden';
		top1.style.backgroundColor = bgColor;
		top1.style.margin = '0 5px';
		top1.style.display = 'block';
		
		var top2 = document.createElement('b');
		top2.style.height = '1px';
		top2.style.overflow = 'hidden';
		top2.style.backgroundColor = bgColor;
		top2.style.margin = '0 3px';
		top2.style.display = 'block';
		
		var top3 = document.createElement('b');
		top3.style.height = '1px';
		top3.style.overflow = 'hidden';
		top3.style.backgroundColor = bgColor;
		top3.style.margin = '0 2px';
		top3.style.display = 'block';
		
		var top4 = document.createElement('b');
		top4.style.height = '2px';
		top4.style.overflow = 'hidden';
		top4.style.backgroundColor = bgColor;
		top4.style.margin = '0 1px';
		top4.style.display = 'block';
		
		var rBot = document.createElement('b');
		rBot.id = 'rBot' + countDownId[windowId];
		rBot.style.display = 'block';
		if ( blanket == true ) {
			rBot.style.backgroundColor = '#DDDDDD';
		} else {
			rBot.style.backgroundColor = 'white';
		}
		
		var bot1 = document.createElement('b');
		bot1.style.height = '1px';
		bot1.style.overflow = 'hidden';
		bot1.style.backgroundColor = bgColor;
		bot1.style.margin = '0 5px';
		bot1.style.display = 'block';
		
		var bot2 = document.createElement('b');
		bot2.style.height = '1px';
		bot2.style.overflow = 'hidden';
		bot2.style.backgroundColor = bgColor;
		bot2.style.margin = '0 3px';
		bot2.style.display = 'block';
		
		var bot3 = document.createElement('b');
		bot3.style.height = '1px';
		bot3.style.overflow = 'hidden';
		bot3.style.backgroundColor = bgColor;
		bot3.style.margin = '0 2px';
		bot3.style.display = 'block';
		
		var bot4 = document.createElement('b');
		bot4.style.height = '2px';
		bot4.style.overflow = 'hidden';
		bot4.style.backgroundColor = bgColor;
		bot4.style.margin = '0 1px';
		bot4.style.display = 'block';
		
		rTop.appendChild(top1);
		rTop.appendChild(top2);
		rTop.appendChild(top3);
		rTop.appendChild(top4);
		//note that backwards order for the bottom, as they round the other way
		rBot.appendChild(bot4);
		rBot.appendChild(bot3);
		rBot.appendChild(bot2);
		rBot.appendChild(bot1);
		
		var topWrapper = document.createElement("div");
		topWrapper.id = 'top_wrapper';
		topWrapper.onmousedown =  function() { dragStart(event,windowId); };
		topWrapper.setAttribute('ONMOUSEDOWN',"dragStart(event,'" + windowId + "');");
		
		
		topWrapper.style.cursor = 'move';
		topWrapper.style.clear = "both";
		topWrapper.style.width = "100%";
		topWrapper.style.height = "20px";
		
		var titleElm = document.createElement("div");
		if (title != null)
			titleElm.innerHTML = title;
		titleElm.setAttribute("style", "float: left;");
		titleElm.style.styleFloat = "left";
		titleElm.style.textAlign = "left";
		titleElm.style.padding = "0px 10px 0px 10px";
		titleElm.style.color = fontColor;
		titleElm.style.fontWeight = "bold";
		titleElm.style.fontSize = "1.2em";
		//titleElm.onmousedown = function() { dragStart(event,windowId); }
		
		var buttonHolder = document.createElement("div");
		buttonHolder.setAttribute("style", "float: right;");
		buttonHolder.style.styleFloat = "right";
		buttonHolder.style.textAlign = "right"
		buttonHolder.style.marginRight = '5px';
		buttonHolder.style.width = "30px";
		
		var countHolder = document.createElement("div");
		countHolder.style.textAlign = 'Center';
		
		var countBox = document.createElement('span');
		countBox.id = 'count_down' + countDownId[windowId];
		
		countBox.style.fontSize = "80%";
		countBox.style.color = fontColor;
		
		if ( timed == true ) {
			countBox.style.display = 'block';
		} else {
			countBox.style.display = 'none';
		}
		
		var xButton = document.createElement('input');
		xButton.id = 'xboxout';
		xButton.type = 'button';
		xButton.onclick = function() { doToggle(windowId, true); }
		xButton.style.border = 'thin solid black';
		xButton.value = "x"; 
		xButton.style.width = "30px";
		buttonHolder.appendChild(xButton);
		countHolder.appendChild(countBox);
		
		var contentHolder = document.createElement("div");	
		contentHolder.id = windowId + '_contentholder';
		contentHolder.innerHTML = content;
		contentHolder.style.textAlign = "left";
		contentHolder.style.padding = '0px 10px 0px 10px';
		contentHolder.style.marginTop = '4px';
		if (h != null) {
			contentHolder.style.height = h + 'px';
		} else {
			contentHolder.style.maxHeight = '250px';
		}
		contentHolder.style.color = fontColor;
		try { contentHolder.style.overflowX = "none"; } catch(ex) { contentHolder.style.overflowx = "none"; }
		try { contentHolder.style.overflowY = "auto"; } catch(ex) { contentHolder.style.overflowy = "auto"; }
		contentHolder.style.width = "95%";
		
		popBox.appendChild(rTop)
		topWrapper.appendChild(titleElm);
		topWrapper.appendChild(buttonHolder);
		
		//topWrapper
		
		popBox.appendChild(topWrapper);
		
		popBox.appendChild(contentHolder);
		popBox.appendChild(countHolder);
		popBox.appendChild(rBot);
		
		bod.appendChild(popBox);
	}

	boxHeight = h;
	boxWidth = w;
	theDiv.push(windowId);
	
	if ( loc == 'corner' ) {
		windowPosCorner(windowId);
	} else if ( loc == 'mouse' ) {
		windowPosMouse(windowId);
	} else if ( loc == 'deadcenter' ) {
		windowPosDeadCenter(windowId);
	} else if ( loc == 'centerfixed' ) {
		windowPosCenterFixed(windowId);
	} else {
		windowPosCenter(windowId);
	}
	
	if ( blanket == true ) {
		blanketSize(windowId);
		toggleOn('blanket');
	}
	toggleOn(windowId);	
	
	if ( popupOpen[windowId] == true ) {
		if ( timed == true ) {
			timeOut = timeUp;
			ResetTimer(windowId);
			if (timeOut && refreshRate) {
				setTimeout("CheckClose('" + windowId + "');",refreshRate * 1000);
			}
		} else {
			document.getElementById('count_down' + countDownId[windowId]).innerHTML = '';
		}
	}
}

function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el != null && el.style.display == 'none' ) {	
		el.style.display = 'block';
	} else if (el != null){
		el.style.display = 'none';
	}
}

function toggleOff(div_id) {
	var el = document.getElementById(div_id);
	if (el != null) {
		el.style.display = 'none';
		popupOpen[div_id] = false;
		if ( div_id == 'blanket' ) {
			handleCorners(false);
		}
	}
}

function toggleOn(div_id) {
	document.getElementById(div_id).style.display = 'block';
	popupOpen[div_id] = true;
	if ( div_id == 'blanket' ) {
		handleCorners(true);
	}
}

function handleCorners(covered) {
	for (var i=0;i<theDiv.length;i++) {
		if ( popupOpen[theDiv[i]] == true ) {
			if ( covered == true ) {
				if ( document.getElementById('rTop' + (i+1)) ) {
					document.getElementById('rTop' + (i+1)).style.backgroundColor = '#DDDDDD';
					document.getElementById('rBot' + (i+1)).style.backgroundColor = '#DDDDDD';
				}
			} else {
				if ( document.getElementById('rTop' + (i+1)) ) {
					document.getElementById('rTop' + (i+1)).style.backgroundColor = 'white';
					document.getElementById('rBot' + (i+1)).style.backgroundColor = 'white';
				}
			}
		}
	}
}

function blanketSize(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
}

function windowPosCorner(popUpDivVar) {
	var window_width = getWindowWidth();
	var popUpDiv = document.getElementById(popUpDivVar);
	var leftAmount = ((window_width - boxWidth) - 20) 
	popUpDiv.style.left = leftAmount + 'px';
	popUpDiv.style.top = '20px';
}

function windowPosCenter(popUpDivVar) {
	var window_width = getWindowWidth();
	var popUpDiv = document.getElementById(popUpDivVar);
	var leftAmount= (window_width*.5) - (boxWidth*.5); 
	popUpDiv.style.left = leftAmount + 'px';
	popUpDiv.style.top = '35px';
}

function windowPosCenterFixed(popUpDivVar) {
	windowPosCenter(popUpDivVar);
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv.style.position = "fixed";
}

function windowPosDeadCenter(popUpDivVar) {
	//windowPosCenter(popUpDivVar);
	var popUpDiv = document.getElementById(popUpDivVar);
	/*
	var scrollHeight = getScrollAmount();
	var windowHeight = getWindowHeight();
	var topAmount = (scrollHeight + windowHeight*.5) - (popUpDiv.offsetHeight*.5);
	*/
	////
	
	var W3C = window.addEventListener;
	var objectWidth = popUpDiv.style.width; //'200px';
	var objectHeight = '400px'; //null is being passed as the height so must explictly declare a value
	
	var moreoff;

	if(W3C) {
		height = parseInt(document.documentElement.clientHeight);
        width = parseInt(document.documentElement.clientWidth);       
		pageOffsetX = parseInt(window.pageXOffset);
		pageOffsetY = parseInt(window.pageYOffset);
		moreoff = 0;
	}
	else {          
		height = parseInt(document.body.clientHeight);
        width = parseInt(document.body.clientWidth);
        
		pageOffsetX = parseInt(document.body.scrollLeft || document.documentElement.scrollLeft);
		pageOffsetY = parseInt(document.body.scrollTop || document.documentElement.scrollTop);
		//moreoff = 1100;
	}
	

	var specLeft = (pageOffsetX + (width / 2) - (parseInt(objectWidth) / 2)) + 'px';
	var specTopTmp = (pageOffsetY + (height / 2) - (parseInt(objectHeight) / 2));
	
	var specTop = (specTopTmp /4) + 'px';
	
	popUpDiv.style.left = specLeft;
	popUpDiv.style.top = specTop;
	
	
	////
	
	//popUpDiv.style.top = topAmount + 'px';
}

function getWindowHeight() {
	var windowHeight = 0;
	try {
		windowHeight = document.body.clientHeight;
	} catch(ex) {
		windowHeight = window.innerHeight;
	}
	return windowHeight;
}

function getScrollAmount() {
	var scrollHeight = 0;
	try {
		scrollHeight = document.body.scrollTop;
	} catch(ex) {
		scrollHeight = window.pageYOffset;
	}
	return scrollHeight;
}

function getWindowWidth() {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}

	return window_width;	
}
/*
function windowPosMouse(popUpDivVar) {
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv.style.position = "absolute";
	
	if ((parseInt(popUpDiv.style.width) + mouseX) > document.body.parentNode.clientWidth) {
		mouseX = document.body.parentNode.clientWidth - parseInt(popUpDiv.style.width);
	}
	
	if ((parseInt(popUpDiv.style.height) + mouseY) > document.body.parentNode.clientHeight) {
		mouseY = document.body.parentNode.clientHeight - parseInt(popUpDiv.style.height);
	}
	
	popUpDiv.style.left = (mouseX) + 'px';
	popUpDiv.style.top = (mouseY) + 'px';
}

// Capture mouse position for pop-ups
var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var mouseX = 0
var mouseY = 0

function getMouseXY(e) {
  if (IE) {
    mouseX = event.clientX + document.body.scrollLeft;
    mouseY = event.clientY + document.body.scrollTop;
  } else {
    mouseX = e.pageX;
    mouseY = e.pageY;
  }
  mouseX = Math.max(mouseX, 0);
  mouseY = Math.max(mouseY, 0);
} */
