/*	Takes a block level element, oDiv, and turns it into an overlay with a transparent background.
	oDiv can be a reference to a block level element or the id of that element. In IE, select elements
	are hidden to overcome the z-index bug. 
	
	oDiv - The id of the div to be turned into an overlay
	fOnShow - The function to be executed when the overlay is made visible
*/
function Overlay(oDiv, fOnShow, fOnHide)
{
 	var oThis = this;
	
	oDiv = Util.Get(oDiv);
	this.oDiv = oDiv.parentNode ? oDiv.parentNode.removeChild(oDiv) : oDiv; // Remove div from DOM (if it's in the DOM)
	this.oBG = document.createElement('div');
	this.fOnShow = fOnShow;
	this.fOnHide = fOnHide;
	
	this.oBG.style.position = 'absolute';
	this.oBG.style.left = '0';
	this.oBG.style.top = '0';
	this.oBG.style.zIndex = 100;
	this.oBG.style.backgroundColor = '#333';
	this.oBG.style.display = 'none';
	Util.SetOpacity(this.oBG, 70);
	
	this.oDiv.style.zIndex = 101;
	this.oDiv.style.visibility = 'hidden';
	this.oDiv.style.position = 'absolute';
	this.oDiv.style.marginTop = '120px';
	
	// Insert background and overlay into DOM
	document.body.insertBefore(this.oDiv, document.body.firstChild);
	document.body.insertBefore(this.oBG, document.body.firstChild);
	
	Util.AddEvent(window, 'resize', function() { oThis.positionDivAndBG(); } );
	Util.AddEvent(window, 'scroll', function() { oThis.positionDivAndBG(); } );
}

Overlay.prototype.Show = function ()
{
 	if (document.all) { // Hide any drop downs on the page (ie6)
		var aDropDowns = document.body.getElementsByTagName('select');
 		for (i = 0; i < aDropDowns.length; i ++) aDropDowns[i].style.visibility = 'hidden';
 	}
	
	this.makeBackgroundTransparent();
	this.positionDivAndBG();
	this.oDiv.style.visibility = 'visible';
	
	if (this.fOnShow) this.fOnShow();
}

Overlay.prototype.Hide = function ()
{
	if (document.all) { // Show the drop downs we hid
		var aDropDowns = document.body.getElementsByTagName('select');
 		for (i = 0; i < aDropDowns.length; i ++) aDropDowns[i].style.visibility = 'visible';
 	}
	
	this.makeBackgroundVisible();
	this.oDiv.style.visibility = 'hidden';
	
	if (this.fOnHide) this.fOnHide();
}

Overlay.prototype.positionDivAndBG = function ()
{
 	this.oBG.style.top = (window.scrollY || document.documentElement.scrollTop || 0) + 'px';
 	this.oBG.style.left = (window.scrollX || document.documentElement.scrollLeft || 0) + 'px';
 	
 	this.oBG.style.height = Util.GetWindowHeight() + 'px';
	this.oBG.style.width = Util.GetWindowWidth() + 'px';	
 	
 	this.oDiv.style.top = (window.scrollY || document.documentElement.scrollTop ||  0) + 'px';
	this.oDiv.style.left = ((Util.GetWindowWidth() - this.oDiv.clientWidth) / 2) + 'px';	// Center it
}

Overlay.prototype.makeBackgroundTransparent = function () {
	this.positionDivAndBG();
	this.oBG.style.display = 'block';
}

Overlay.prototype.makeBackgroundVisible = function () { this.oBG.style.display = 'none'; }
