/** 
 * Modal Window Class
 *	All modal functions should be controlled by this class 
 */
// Start With The Base ModalWindow Class 
var ModalWindow = Class.create({
	
	// On init of class, run the following 
	initialize: function(settingsObj) {
		
		this.title 		= ""; 
		this.content 	= ""; 
		
		// A constant status keeping track of the modal window
		this.status 	= "begin";
		
		this.outer_id 				= "obj_modal_outer";
		this.background_id			= "obj_modal_background";
		this.content_position_id	= "obj_modal_content_positioning";
		
		if(settingsObj.title) 	 	this.title 		= settingsObj.title;
		if(settingsObj.content)  	this.content 	= settingsObj.content;
		if(settingsObj.closeAfter)	this.closeAfter	= settingsObj.closeAfter;
		
		// position
		if(settingsObj.position)	this.position	= settingsObj.position;
		
		//** IE 6 specific 
		// requires you to have set this var previously to true or false/undefined 
		if(isRunningIE6OrBelow) {
			this.position	= "absolute"; // must always be absulute posistion 
		}
		
		if(this.content!='') {
			this.open();
		}
		
		if(settingsObj.scrollTo) Effect.ScrollTo(this.content_position_id);
		
		if(this.closeAfter) {
			
			setTimeout(this.close.bind(this), this.closeAfter);
		}
		
	},
	
	// Open the modal window 
	open: function(modal_content) {
		
	   // first make sure the modal window is not already open
	   if(!this.isOpen()) {
		
		if(modal_content==undefined) modal_content = this.content;
	
		var backDiv = document.createElement('div'); 
		backDiv.id = this.outer_id;
		
		backDiv.innerHTML  = "<div id='"+ this.background_id +"'> &nbsp; </div>";
		backDiv.innerHTML += "<div id='"+ this.content_position_id +"'> "+ modal_content +" </div>";
		
		document.body.appendChild(backDiv);
		
		// set positioning of modal inner part (content_position_id) 
		if(this.position) $(this.content_position_id).style.position = this.position; 
		
		//setTimeout(function() {
		
			//alert(this.background_id);
			new Effect.Opacity(this.background_id, { from: 0, to: 0.8, duration: 0.5, queue: 'end' });
		
			//setTimeout(function() {
				new Effect.Opacity(this.content_position_id, { from: 0, to: 1.1, duration: 0.5 });
				new Effect.SlideDown(this.content_position_id, { duration: 0.5 });	
			//}, 1000);
		//}, 200); 
		
		// set the status to be open 
		this.status = "open";
		
		// if there is a title for this modal then set the browsers page title to be this title (remember to save the old title for later)
		if((this.title!='') && document.title) { 
			this.sites_old_title = document.title;
			document.title = this.title;
		}
		
		// If IE 6, and hide all drop down menus on-open (they showed over the top of the modal), re-show them on close
		if(isRunningIE6OrBelow) {
			$$('select').each(function(el) {
				  el.style.display = "none";
			});
		}
		
	   }
		
	},
	
	// Close the modal window 
	close: function() { 
		
	   // first make sure the modal window has not already been closed 
	   if(this.isOpen()) {
	 	
		$(this.background_id).fade();
		
		new Effect.SlideUp(this.content_position_id, { duration: 0.8 });	
		$(this.content_position_id).fade();
		 
		setTimeout(this.removeOuterElement.bind(this), 600);
		
		// set the status to be closed 
		this.status = "closed";
		
		// if there is a title set for this modal than it will have changed the browser title on open, so now we need to change it back
		if((this.title!='') && document.title) document.title = this.sites_old_title;
		
	   }
		
		// If IE 6, re-open the closed select 
		if(isRunningIE6OrBelow) {
			$$('select').each(function(el) {
				  el.style.display = "inline";
			});
		}
		
	},
	
	// Remove the outer div of the modal (its container) 
	removeOuterElement: function() {
		document.body.removeChild(document.getElementById(this.outer_id)); 
	},
	
	// Test the modal to check if its currently open or not, returns true or false, 
	isOpen: function() {
		if(this.status=='open') return true;
		else return false;
	}
	
	/**
	 * Additional methods to add later 
	 *  - ajax load content (with optional object loaded fetaures (such as loading icon, and more)
	 *  - auto close button generation? might have to look into other ways of closing the modal? 
	 *  - sub classes for cart, send to friend and more 
	 *  - log that can be turned on and off either using firebug or an element to print to ? fot help with testing
	 *  - load counter?
	 *  - styling, such as positioning type for content? fixed or absolute?
	 *  - in built title placement? or maybe title is used on a sub class using a prebuilt layout? 
	 *  - 
	 *  - 
	 */

	
// End Class 
});
