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();
var objCreated = 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) {
	toggleOff(w);
	theDiv.pop(w);
	if ( blanketOn[w] == true ) {
		toggleOff('blanket');
		blanketOn[w] = false;
	}
}

function fakePopUp(windowId,content,bgColor,fontColor,h,w,loc,blanket,timed,timeUp) {
	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) ) {
		var popBox = document.createElement('div');
		popBox.id = windowId;
		var preInsertedContent = '';
	} else if (objCreated[windowId] == undefined) { //if the div does exist don't bother to assign to anything unless the style has been created
		var popBox = document.getElementById(windowId);
		var preInsertedContent = popBox.innerHTML;
		popBox.innerHTML = '';
		
	}
	
	if ( objCreated[windowId] == undefined  ) {
		
		countDownId[windowId] = ++countDownCounter;
		objCreated[windowId] = true;
	
		var bod = document.getElementsByTagName('body')[0];
		
		popBox.style.backgroundColor = bgColor;
		//popBox.style.height = h + 'px'; //NOTE: the height gets applied to the contentHolder
		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 buttonHolder = document.createElement("div");
		buttonHolder.style.float = 'right';
		buttonHolder.style.textAlign = "right"
		buttonHolder.style.marginRight = '5px';
		
		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); }
		xButton.style.border = 'thin solid black';
		xButton.value = "x"; 
		buttonHolder.appendChild(xButton);
		countHolder.appendChild(countBox)
		
		var contentHolder = document.createElement("div");	
		
		if (preInsertedContent) {
			contentHolder.innerHTML = preInsertedContent
		} else {
			contentHolder.innerHTML = content;
		}
		
		contentHolder.style.padding = '10px 10px 10px 10px';
		contentHolder.style.marginTop = '10px';
		contentHolder.style.height = h + 'px';
		contentHolder.style.color = fontColor;
		
		popBox.appendChild(rTop)
		popBox.appendChild(buttonHolder);
		
		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 {
		windowPosCenter(windowId);
	}
	
	if ( blanket == true ) {
		blanketSize(windowId);
		toggleOn('blanket');
	}
	toggleOn(windowId);	
	
	if ( popupOpen[windowId] == true ) {
		if ( timed == true ) {
			document.getElementById('count_down' + countDownId[windowId]).style.display = 'block'; //new ... will it stay?
			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.style.display == 'none' ) {	
		el.style.display = 'block';
	} else {
		el.style.display = 'none';
	}
}

function toggleOff(div_id) {
	document.getElementById(div_id).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 = '250px';
}

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;	
}

