/**************************************************************
 *
 *	LightBox Constructor
 *
 *	@param	eleID	Name to give the lightbox's html element
 *
 *	Deendent on SWFObject 1.5, com.beavercreek.util,
 *
 **************************************************************/
function LightBox() {
	//	ID of the overlay
	this.overlayID = 'lbOverlay';
	//	ID of the div tag containing content
	this.contentID = 'lbContent';
	//	SWF Object 
	this.swfObject;
	
	this.overlay;
	this.content;
	this.closeButton;
	this.displayNode;
	this.timer;
	this.type;
	this.htmlString;
	this.path;
	this.width;
	this.height;
	
	this.createUIObjects();
	this.layout();
}

/**************************************************************
 *
 *	Creates a HTML DOM Node for displaying text
 *
 *	@param	htmlString	Path to the image file.
 *
 **************************************************************/
LightBox.prototype.html = function(htmlString) {
	this.type = 'html';
	this.htmlString = htmlString;
}

/**************************************************************
 *
 *	Creates a img DOM Node for images
 *
 *	@param	path	Path to the image file.
 *
 **************************************************************/
LightBox.prototype.image = function(path) {
	this.type = 'img';
	this.path = path;
}

/**************************************************************
 *
 *	Creates a div DOM Node for flash content
 *
 *	@param	path	Path to the flash file
 *	@param	width	Width of the flash content
 *	@param	height	Heighht of the flash content
 *
 **************************************************************/
LightBox.prototype.flash = function(path, width, height) {
	this.type = 'swf';
	this.path = path;
	this.width = width;
	this.height = height;
}

/**************************************************************
 *
 *	Creates a div DOM Node for flash content
 *
 **************************************************************/
LightBox.prototype.show = function() {
	switch (this.type) {
		case 'html':
			if(this.displayNode) {
				document.removeChild(document.getElementById('lbHTML'));
			}
			
			this.displayNode = document.createElement('div');
			this.displayNode.id = 'lbHTML';
			this.displayNode.innerHTML = this.htmlString;
			this.content.appendChild(this.displayNode);
		break;
		
		case 'swf':
			this.displayNode = document.createElement('div');
			this.displayNode.id = 'flashContent';
			this.content.appendChild(this.displayNode);
			
			this.swfObject = new SWFObject(this.path, 'flashContent', this.width, this.height, '8', '#FFFFFF');
			this.swfObject.addParam('swliveconnect', 'true');
			this.swfObject.addParam('allowscriptaccess', 'always');
			this.swfObject.addParam('wmode','opaque');
			
			//	Check if there's a lesson
			if(arguments.length == 2) {
				this.swfObject.addVariable('course', arguments[0]);
				this.swfObject.addVariable('lesson', arguments[1]);
			//	Check if it's a course
			} else if(arguments.length == 1) {
				this.swfObject.addVariable('course', arguments[0]);
				this.swfObject.addVariable('lesson', '1');
			}
			
			this.swfObject.useExpressInstall('/js/expressinstall.swf');
			
			this.swfObject.write('flashContent');
		break;
		
		default:
			this.displayNode = document.createElement('div');
			var img = document.createElement('img');
			img.src = this.path;
			this.displayNode.appendChild(img);
			this.content.appendChild(this.displayNode);
		break;
	}
	this.content.style.display		= 'block';
	this.overlay.style.display		= 'block';
}

/**************************************************************
 *
 *	Destroys the display node, and hides everything else
 *
 **************************************************************/
LightBox.prototype.hide = function() {
	this.content.removeChild(this.displayNode);
	this.displayNode = null;
	this.content.style.display = 'none';
	this.overlay.style.display = 'none';
}

/**************************************************************
 *
 *	Creates the two basic divs for the lightbox.
 *
 **************************************************************/
LightBox.prototype.createUIObjects = function() {
	var lb = this;
	
	this.overlay	= document.createElement('div');
	this.overlay.id	= this.overlayID;
	document.body.appendChild(this.overlay);
	
	this.content	= document.createElement('div');
	this.content.id	= this.contentID;
	document.body.appendChild(this.content);
	
	this.closeButton = document.createElement('a');
	this.closeButton.id = 'lbClose';
	this.closeButton.href = 'javascript:void(0);';
	this.content.appendChild(this.closeButton);
	
	this.content.style.display = 'none';
	this.overlay.style.display = 'none';
	
	this.closeButton.onclick = function() {
		lb.hide();
	}
	
	this.overlay.onclick = function() {
		lb.hide();
	}
}

/**************************************************************
 *
 *	Called at a specific interval, keeps the lightbox layed out
 *
 **************************************************************/
LightBox.prototype.layout = function() {
	
	var lb = this;
	this.timer = setTimeout(function() { lb.layout(); }, 100);
	
	//	Overlay's width and height
	this.overlay.style.height	= Util.windowHeight() + "px";
	this.overlay.style.width	= Util.windowWidth() + "px";
	
	//	Keep 0,0 of the visible area
	this.overlay.style.top		= Util.bodyY() + 'px';
	this.overlay.style.left		= Util.bodyX() + 'px';
	
	//	keep the content centered
	this.content.style.top		= (((Util.windowHeight() - Util.elementHeight(this.content)) / 2) + Util.bodyY()) + 'px';
	this.content.style.left		= (((Util.windowWidth() - Util.elementWidth(this.content)) / 2) + Util.bodyX()) + 'px';

}