/*--------------------------------------------------------------------------*/
/*	Flightbox	
*	This is a script for creating modal dialog windows (like the ones your operating
*	system uses)
*	
*/

var Flightbox = {
	/* hideAll - closes all open Flightbox windows */
	hideAll: function(){
		lboxes = document.getElementsByClassName('lbox')
		lboxes.each(function(box){
				Element.hide(box)
			}
		)
		if ($('foverlay')){
			Element.remove('foverlay');
			}
	}
}
Flightbox.base = Class.create();
Flightbox.base.prototype = {

	initialize: function(element, options){
		//start by hiding all Flightboxes
		Flightbox.hideAll();
	
		this.element = $(element);
		this.options = Object.extend({
			FlightboxClassName : 'Flightbox',
			closeOnfoverlayClick : false,
			externalControl : false
		}, options || {} )

		//create the foverlay
		new Insertion.Before(this.element, "<div id='foverlay' style='display:none;'></div>");
		
Element.addClassName(this.element, this.options.FlightboxClassName)
	
		//also add a default lbox class to the Flightbox div so we can find and close all Flightboxes if we need to
		Element.addClassName(this.element, 'lbox')
		
		//Tip: make sure the path to the close.gif image below is correct for your setup
		closer = '<img id="close" src="http://www.getv2.com/inventory/templates/nexstar/images/close.jpg" alt="Close" title="Close this window" />'

		//insert the closer image into the div
		new Insertion.Top(this.element, closer);
		
		Event.observe($('close'), 'click', this.hideBox.bindAsEventListener(this) );
		
		if (this.options.closeOnfoverlayClick){
			Event.observe($('foverlay'), 'click', this.hideBox.bindAsEventListener(this) );
		}
		if (this.options.externalControl){
			Event.observe($(this.options.externalControl), 'click', this.hideBox.bindAsEventListener(this) );
		}
				
		this.showBox();	
	},
	
	showBox : function(){
		//show the foverlay
	   Element.show('foverlay');

		//center the Flightbox
	   this.center();
	   
	   	//show the Flightbox
	   Element.show(this.element);
	   return false;
	},
	
	hideBox : function(evt){	
		Element.removeClassName(this.element, this.options.FlightboxClassName)
		Element.hide(this.element);
		//remove the foverlay element from the DOM completely
		Element.remove('foverlay');
		return false;
	},
		
	center : function(){
		var my_width  = 0;
		var my_height = 0;
		
		if ( typeof( window.innerWidth ) == 'number' ){
			my_width  = window.innerWidth;
			my_height = window.innerHeight;
		}else if ( document.documentElement && 
				 ( document.documentElement.clientWidth ||
				   document.documentElement.clientHeight ) ){
			my_width  = document.documentElement.clientWidth;
			my_height = document.documentElement.clientHeight;
		}
		else if ( document.body && 
				( document.body.clientWidth || document.body.clientHeight ) ){
			my_width  = document.body.clientWidth;
			my_height = document.body.clientHeight;
		}
		
		this.element.style.position = 'absolute';
		this.element.style.zIndex   = 98;
		
		var scrollY = 0;
		
		if ( document.documentElement && document.documentElement.scrollTop ){
			scrollY = document.documentElement.scrollTop;
		}else if ( document.body && document.body.scrollTop ){
			scrollY = document.body.scrollTop;
		}else if ( window.pageYOffset ){
			scrollY = window.pageYOffset;
		}else if ( window.scrollY ){
			scrollY = window.scrollY;
		}
		
		var elementDimensions = Element.getDimensions(this.element);
		
		var setX = (( my_width  - elementDimensions.width  ) / 2) - (my_width * .333);
		var setY = 0;
		
		// setX = ( setX < 0 ) ? 0 : setX;
		
		this.element.style.left = setX + "px";
		this.element.style.top  = setY + "px";
		
	}

	
}

