window.onresize = function () {
	lightboxWindow.resizeLightbox();
}

/**
 LightboxWindow provides the container for modal pop up windows within the application.  The assumption for this object
 is that there will be only 1 light box window created for the entire application.  Use of this object depends on the
 presence of the 'pagebody' div which is provided automatically by our default catalogHeader and catalogFooter components.
 Upon object creation the LightboxWindow will create all markup objects dynamically and insert the markup as the first
 child of the body element as soon as the pagebody body div is available in the dom.  To use this object:
 1.	The YAHOO user interface libraries are required in order to use this object.
 2.	A light box window object will automatically be created for you. This global object is 'lightboxWindow'.  Additional new
		LightboxWindow objects should not be manually created.
 LightBoxWindow contains several public functions for controlling the lightbox.  These are the only functions which should be
 called outside the LightboxWindow functions explicitly.

 1. lightboxWindow.Populate(newContent) will open the lightbox window with the content specified.
 2. lightboxWindow.Close() will close the window and clear its contents
 3. lightboxWindow.resizeLightbox() will adjust the placement of the lightbox on the screen.

 When displayed the LightboxWindow will disable the content displayed beneath the popup by positioning an iframe over the
 page or viewable window, which ever is larger.  The Lightbox will attempt to position the modal window content in the
 center of the viewable window automatically when opened.  In order to maintain this when the user resizes the window automatically
 the resizeLightbox function can be called from the onresize event.  This occurs automatically in checkout.
*/
var LightboxWindow = function(){
	this.pageDiv;
	this.lightboxDiv;
	this.ifrm;
	this.newContent = null;
	YAHOO.util.Event.onAvailable('pagebody', this.createMarkup, this);
}

/**
 * createMarkup is called directly from the object when it is instantiated and as soon as the pagebody element has
 * been made available by the dom.  It will create the markup necessary if not already present in the dom and set
 * the properties of the LightboxWindow to the necessary elements
 * @param obj is the reference to the LightboxWindow which was created.
 */
LightboxWindow.prototype.createMarkup = function(obj){
	obj.pageDiv = YAHOO.util.Dom.get("pageLightboxDiv");
	if( !obj.pageDiv ){
		var e_pagediv = document.createElement("div");
		e_pagediv.setAttribute("id", "pageLightboxDiv");
		e_pagediv.innerHTML = '<iframe id="lightboxUnderlay" style="display:none" class="lightboxUnderlay" src="/common/images/shim.gif" scrolling="no" frameborder="0"></iframe>'+
													'<div id="lightboxDiv" style="display:none"></div><div id="lightboxContent"></div>';
		obj.pageDiv = e_pagediv;
		YAHOO.util.Dom.insertBefore(obj.pageDiv, "pagebody");
		var tdiv = document.getElementById("pageLightboxDiv");
		obj.lightboxDiv = YAHOO.util.Dom.get("lightboxDiv");
		obj.ifrm = YAHOO.util.Dom.get("lightboxUnderlay");
	}
	obj.lightboxDiv = YAHOO.util.Dom.get("lightboxDiv");
	obj.ifrm = YAHOO.util.Dom.get("lightboxUnderlay");
}

/**
 * Hide will close the lightbox window, clear its content and hide any helper elements. Is is called from the Close
 * function and should not be called directly outside the LightboxWindow functions.
 */
LightboxWindow.prototype.hide = function(){
	var contentDiv = document.getElementById('lightboxContent');
	contentDiv.innerHTML = "";
	contentDiv.style.top = "0px";
	this.lightboxDiv.style.display = this.ifrm.style.display = "none";
};

/** Show displays the helper objects which keep the user from interacting with data beneath the light box. */
LightboxWindow.prototype.show = function(){ this.lightboxDiv.style.display = this.ifrm.style.display = "block"; };

/**
 * Populate will open the light box window and set the content within the window.  This function should be called
 * as lightboxWindow.Populate(<your content>) in order to open a light box window.
 * @param newContent is the markup that will display in the window.
 */
LightboxWindow.prototype.Populate = function(newContent){
	this.newContent = newContent;
	//try one more time to create the necessary objects if they aren't there.  if it fails again, throw error.
	if( !this.pageDiv ){ this.createMarkup(this); }
	if( this.pageDiv ){
		this.displayLightboxInfo();
	}else{
		throw (new LightboxError( "Failure in attempting to open the LightboxWindow component.  Check that pagebody element exists"));
	}
};

/**
 * displayLightboxInfo is called from populate and does the work to phyically populate the light box window. It should
 * not be called directly outside the LightboxWindow functions.
 */
LightboxWindow.prototype.displayLightboxInfo = function() {
	NMAjax.setInnerHtml("lightboxContent", this.newContent);
	this.adjustLightbox();
	this.show();
};

/**
 * Close will close the light box window, clear it's contents and disable all helper elements which keep the user from
 * interacting with the content displayed beneath the window.
 */
LightboxWindow.prototype.Close = function(){
	if( this.pageDiv ){
		this.hide();
	}
};

/** Resize light box will reposition the lightbox window so that it displays in the center of the viewable browser space. */
LightboxWindow.prototype.resizeLightbox = function(){
	if(this.pageDiv && this.lightboxDiv && this.lightboxDiv.style.display == "block") {
		this.adjustLightbox();
	}
};

/**
 * AdjustLightbox is called both when the light box opens and when the light box must be repositioned.  It will place the
 * helper elements which disable the underlying content in the correct locations and attempt to center the modal window
 * content in the center of the viewable space.  This function should not be called explicitly from outside the LightboxWindow
 * functions.
 */
LightboxWindow.prototype.adjustLightbox = function(){
	var pageWidth = document.getElementById("pagebody").offsetWidth;
	var pageHeight = document.getElementById("pagebody").offsetHeight;

	if(pageHeight > 0) {
		var winHeight = Screen.viewheight();
		winHeight = winHeight ? winHeight : 0;

		var maxHeight = Math.max(pageHeight, winHeight);
		this.lightboxDiv.style.width = pageWidth + "px";
		this.lightboxDiv.style.height = maxHeight + "px";

		var lightboxContent = document.getElementById("lightboxContent");

		lightboxContent.style.width = pageWidth + "px";

		var csize = NMUtil.getDimensions(lightboxContent);
		var heightDiff = (winHeight - csize.height) / 2;

		var top = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
		top = top ? top : 0;

		var parentTop = this.pageDiv.offsetTop;
		top = top - parentTop;

		var boxTop = Math.round(top + heightDiff);
		boxTop = (boxTop < 0) ? 0 : boxTop;
		lightboxContent.style.top = boxTop + "px";
		//visibility needs to be set or the Region function will not return any data.
		YAHOO.util.Dom.setStyle(this.lightboxDiv, "visibility", "visible");
		YAHOO.util.Dom.setStyle(this.lightboxDiv, "display", "block");
		var lightboxRegion = YAHOO.util.Region.getRegion(this.lightboxDiv);
		this.ifrm.style.left = "0px";
		this.ifrm.style.height = (lightboxRegion.bottom - lightboxRegion.top)+"px";
		this.ifrm.style.width = (lightboxRegion.right - lightboxRegion.left)+"px";
	}
};
var lightboxWindow = new LightboxWindow();

