/*
ALL SCRIPTING IN THIS DOCUMENT WAS WRITTEN BY NICK ANNIS FOR THE ENGINES AND ENERGY CONVERSION LAB (CSU)
	-last revised: 2/26/09
	
	-requires additional files:
		tools.js

AVAILABLE FUNCTIONS:
-constructor				: creates an absolutely positioned box to place over the body of the webpage,
							  wraps body in a div and appends to the end of the body. If an overlay element already exists for the
							  page, returns that overlay element
	
	reSize					: resizes the width of the overlay element
	rePosition				: repositions the y-coordinate of the overlay element so that the overlay sits
							  in the center of the screen
	show					: resizes, repositions and reveals the overlay element
	hide					: hides the overlay element
	setOverlayHTML			: takes an html string as an argument to display on the overlay
								-args:	str			-STRING value, valid html to insert inside the popup object
	setOverlayElement		: takes an html element as an argument to display on the overlay
								-args:  element		-HTML ELEMENT, valid element to insert inside the popup object
	setWidth				: manually sets the width of the overlay element, if reSize function isnt working
								-args:	width		-INT value, new width of the object in pixels
	runCloseEvals			: runs string statements from array closeEvalSequence in order on popup close
	addCloseEval			: adds a string to the array closeEvalSequence to be evaluated on popup close
	removeCloseEval			: removes a string from the array closeEvalSequence, shifts other array elements back to
							  compensate for the gap
								-args:	remove		-INT index of location to remove from the array
	runOpenEvals			: runs string statements from array openEvalSequence in order when the popup appears
	addOpenEval				: adds a string to the array openEvalSequence to be evaluated when the popup apprears
								-args:	evalString	-STRING value, valid javascript statement to evaluate with eval();
	removeOpenEval			: removes a string from the array openEvalSequence, shifts other array elements back to 
							  compensate for the gap
								-args:	remove		-INT index off location to remove from the array
	mOver					: when mousing over the "close" button, changes text and background color for the button
	mOut					: when mousing out of the "close" button, changes text and background color back to default
	isVisible				: returns a boolean value indicating whether or not the popup is currently visible
	getFrame				: returns the "frame" element containing the overlay
	
*/

function overlay(name,element){
	if(typeof element == 'undefined')
		element = null;
	if(typeof overlay.exists == 'undefined')
		overlay.exists = name;
	else{
		window[name] = window[overlay.exists];
		return window[overlay.exists];
	}
	
	this.name = name;
	window[this.name] = this;
	this.object = "window[\""+this.name+"\"]";
	this.divObj = element;
	this.frameName = this.name+"Frame";
	this.bCaseName = "OLBCASE";
	this.allowScroll = true;
	this.visible = false;
	
	this.toolBox = new tools("toolBox");
	
	this.closeEvalSequence = new Array();
	this.openEvalSequence = new Array();
	this.lastPosO = 0;
	this.lastPosC = 0;
	
	this.frameParentStyle = "position:absolute;width:"+this.toolBox.getScreenWidth()+"px;border:0px solid #ff0000;left:0px; display:none;z-index:1000;background:url(undefined);";
	this.frameStyle = "visibility:hidden;border:5px solid #000000;z-index:1000;background-color:#ffffff;";
	this.infoStyle = "background:#c8c8c8;text-align:center;color:#000000;height:20px;border-top:1px solid #000000;cursor:pointer;"+
					 "font-family:Verdana;font-size:12px;line-height:20px;padding:3px;font-weight:bold;display:block;";
	this.bodyCaseStyle = "display:block;width:100%;height:100%;";
	this.opaque = "zoom:1;width:100%;height:100%;opacity:0.3;"+
		"filter: alpha(opacity=30);";
	this.infoText = "Close";

	this.closeInfo = document.createElement('div');
		this.closeInfo.innerHTML = "<div onclick='"+this.object+".hide()' onmouseover='"+this.object+".mOver()' onmouseout='"+this.object+".mOut()' style='"+this.infoStyle+"'>"+this.infoText+"</div>";
		this.closeInfo = this.closeInfo.firstChild;
		
	this.frame = document.createElement('div');
		this.frame.innerHTML = "<div id='"+this.frameName+"' name='"+this.frameName+"' align='center' style='"+this.frameStyle+"'></div>";
		this.frame.setAttribute("align","center");
		this.frame.setAttribute("style",this.frameParentStyle);
		this.frame.style.cssText = this.frameParentStyle;
		
		if(this.divObj!=null){
			this.frame.firstChild.appendChild(this.divObj);
		}
		this.frame.firstChild.appendChild(this.closeInfo);
		
	if(document.getElementById(this.bCaseName) == null){
		this.bodyCase = document.createElement('div');
			this.bodyCase.setAttribute("id",this.bCaseName);
			this.bodyCase.setAttribute("name",this.bCaseName);
			while(document.body.firstChild){
				this.bodyCase.appendChild(document.body.firstChild);
			}
		this.bodyCase.setAttribute("style",this.bodyCaseStyle);
		this.bodyCase.style.cssText = this.bodyCaseStyle;
			
		document.body.appendChild(this.bodyCase);
	}
	else{
		this.bodyCase = document.getElementById(this.bCaseName);
	}
		
	document.body.appendChild(this.frame);
		this.frame.onselectstart = function () { return false; };
		this.frame.onmousedown = function () { return false; };
		this.frame = this.frame.firstChild;
		
	return true;
}

	overlay.prototype.reSize = function(){
		this.frame.style.visibility = "hidden";
		this.frame.parentNode.style.display = "block";
		
		var frameWidth = this.toolBox.getElementWidth(this.frame.firstChild);
		if(frameWidth!=0){
			this.frame.style.width = frameWidth+"px";
		}
		else{
			this.frame.style.width = "";
		}
	}

	overlay.prototype.rePosition = function(){
		this.frame.style.top = "0px";
		this.frame.style.visibility = "hidden";
		this.frame.parentNode.style.display = "block";
		
		var overlayTop = Math.floor((this.toolBox.getScreenHeight()/2)-(this.toolBox.getElementHeight(this.frame)/2)+this.toolBox.getWindowYPos()+.5);
		
		if((overlayTop+this.toolBox.getElementHeight(this.frame))>(this.toolBox.getScreenHeight()+this.toolBox.getWindowYPos())){
			overlayTop = (this.toolBox.getScreenHeight()+this.toolBox.getWindowYPos())-(this.toolBox.getElementHeight(this.frame));
		}
		if(overlayTop<0){
			overlayTop = 0;
		}
		this.frame.parentNode.style.top = overlayTop+"px";
	}

	overlay.prototype.show = function(){
		this.bodyCase.setAttribute("style",this.opaque);
		this.bodyCase.style.cssText = this.opaque;
		
		this.rePosition();
		this.reSize();
		
		this.frame.style.visibility = "visible";
		this.frame.parentNode.style.display = "block";
		
		if(!this.allowScroll){
			document.body.style.overflow = "hidden";
		}
		
		this.visible = true;
		this.runOpenEvals();
	}
	
	overlay.prototype.hide = function(){
		this.frame.parentNode.style.display = "none"
		this.frame.style.visibility = "hidden";
		this.bodyCase.setAttribute("style",this.bodyCaseStyle);
		this.bodyCase.style.cssText = this.bodyCaseStyle;
		if(!this.allowScroll){
			document.body.style.overflow = "auto";
		}
		
		this.visible = false
		this.runCloseEvals();
	}
	
	overlay.prototype.setOverlayHTML = function(str){
		if(typeof str == "string"){
			var newElem = document.createElement('span');
			newElem.innerHTML = str;
			if(newElem.firstChild == newElem.lastChild){
				newElem = newElem.firstChild;
			}
			this.setOverlayElement(newElem);
		}
	}
	
	overlay.prototype.setOverlayElement = function(element){
		if(typeof element == "object" && element != null){
			if(this.divObj!=null){
				this.frame.removeChild(this.divObj);
			}
			if(element.tagName == "IMG"){
				var func = "window['"+this.name+"'].show()";
				element.setAttribute("onload",func);
				element.onload = function(){eval(func);};
				
				if(window.opera){
					var testimage = new Image;
					testimage.src = element.src;
					if(testimage.complete){
						this.show();
					}
				}
			}
			this.divObj = element;
			this.frame.removeChild(this.closeInfo);
			this.frame.appendChild(this.divObj);
			this.frame.appendChild(this.closeInfo);
		}
	}
	
	overlay.prototype.setWidth = function(width){
		this.frame.style.width = width+"px";
	}
	
	overlay.prototype.runCloseEvals = function(){
		for(var i=0;i<this.lastPosC;i++){
			eval(this.closeEvalSequence[i]);
		}
	}
	
	overlay.prototype.addCloseEval = function(evalString){
		this.closeEvalSequence[this.lastPosC] = evalString;
		this.lastPosC++;
	}
	
	overlay.prototype.removeCloseEval = function(remove){
		var index = -1;
		if(typeof remove == "string"){
			for(var i=0;i<this.lastPosC;i++){
				if(this.closeEvalSequence[i]==remove){
					index = i;
				}
			}
		}
		else if(typeof remove == "number" && remove<this.lastPosC){
			index = remove;
		}
		if(index!=-1){
			var i;
			this.closeEvalSequence[index] = "";
			for(i = index;(i+1)<this.lastPosC;i++){
				this.closeEvalSequence[i] = this.closeEvalSequence[i+1];
			}
			this.closeEvalSequence[i+1] = null;
			this.lastPosC--;
		}
	}
	
	overlay.prototype.runOpenEvals = function(){
		for(var i=0;i<this.lastPosO;i++){
			eval(this.openEvalSequence[i]);
		}
	}
	
	overlay.prototype.addOpenEval = function(evalString){
		this.openEvalSequence[this.lastPosO] = evalString;
		this.lastPosO++;
	}
	
	overlay.prototype.removeOpenEval = function(remove){
		var index = -1;
		if(typeof remove == "string"){
			for(var i=0;i<this.lastPosO;i++){
				if(this.openEvalSequence[i]==remove){
					index = i;
				}
			}
		}
		else if(typeof remove == "number" && remove<this.lastPosO){
			index = remove;
		}
		if(index!=-1){
			var i;
			this.openEvalSequence[index] = "";
			for(i = index;(i+1)<this.lastPosO;i++){
				this.openEvalSequence[i] = this.openEvalSequence[i+1];
			}
			this.openEvalSequence[i+1] = null;
			this.lastPosO--;
		}
	}
	
	overlay.prototype.mOver = function(){
		this.closeInfo.style.background = "#00490c";
		this.closeInfo.style.color = "#ffffff";
	}
	
	overlay.prototype.mOut = function(){
		this.closeInfo.style.background = "#c8c8c8";
		this.closeInfo.style.color = "#000000";
	}
	
	overlay.prototype.isVisible = function(){
		return this.visible;
	}
	
	overlay.prototype.getFrame = function(){
		return this.frame;
	}