if (typeof window.Avram == "undefined") { window.Avram = {}; }

/**
 * class Avram.Overlay
 * author Avram Eisner for POP (www.pop.us)
 * version 0.2
 * Avram.Overlay is a simple overlay class
 * It's creation method takes an optional JSON object that sets the background
 * color and opacity. Following creation of the overlay object, the actual overlay
 * can be displayed or hidden with the public functions show() and hide()
 * respectively. The background color and opacity can be changed after instantiation
 * as well, using the setOptions() function.
 */
Avram.Overlay = Class.create({
	/**
	 * Avram.Overlay.IE6 -> Boolean
	 * Uses IE conditional comments to determine if IE6 is the browser. If the browser is IE6,
	 * then the select elements will be hidden so they don't show up through the overlay.
	 **/
	IE6: false /*@cc_on || @_jscript_version <= 5.7 @*/,
	/**
	 * Avram.Overlay.defaultOptions -> Object
	 **/
	// CUSTOM FOR ALADDIN (GS)
	defaultOptions: {
		bgColor: "#fff",
		opacity: 0.66,
		hideOnClick: true
	},
	
	/**
	 * new Avram.Overlay(overlayID[, options]) -> null
	 * Creates a new Avram.Overlay object and sets any specified options.
	 * - overlayID (String): The HTML id to set on the overlay DIV
	 * - options (Object): A JSON object containing the following options:
	 *    bgColor (String): The overlay's background color in hex form (i.e. "#660000" for a dark red).
	 *        default value: "#000000"
	 *    opacity (Double): The overlay's opacity 0.0 (transparent) to 1.0 (opaque)
	 *        default value: 0.5
	 *    hideOnClick (Boolean): If set to true, adds an onclick event to the overlay that hides itself.
	 *        default value: True
	 */
	initialize: function(overlayID, options) {
		// Check if the ID is already being used
		if ($(overlayID) != null) {
			if (window.console) {
				console.error("An element with an id of '%s' already exists on this page.", overlayID);
			} else {
				alert("An element with an id of '"+overlayID+"' already exists on this page.");
			}
			return;
		}	
		
		this.overlayID = overlayID;
		
		// Build the overlay's html
		this._generateHTML(overlayID);
		
		// Insert the overlay html onto the page and register it to this.overlayDiv
		this._insert();
		
		// Set the options for the overlay div
		this._setOptions(options);
	},
	
	
	/* Public Functions */
	
	/**
	 * Avram.Overlay.show([options]) -> null
	 * Displays the overlay on the screen.
	 * - options (Object): A JSON object containing the following options
	 *    (note, this is the same as the constructor parameter of the same name):
	 *    bgColor (String): The overlay's background color in hex form (i.e. "#660000" for a dark red).
	 *        default value: "#000000"
	 *    opacity (Double): The overlay's opacity 0.0 (transparent) to 1.0 (opaque)
	 *        default value: 0.5
	 *    hideOnClick (Boolean): If set to true, adds an onclick event to the overlay that hides itself.
	 *        default value: True
	 */
	show: function(options) {
		if(options) {
			this._setOptions(options);
		}
		
		this.overlayDiv.show();
		
		if (this.IE6) {
			$$("select").reject(function(s) {
				return s.descendantOf(this.overlayDiv);
			}, this).invoke("setStyle", {visibility: "hidden"});
		}
	},
	
	/**
	 * Avram.Ovelay.hide() -> null
	 */
	hide: function() {
		this.overlayDiv.hide();
		
		if (this.IE6) {
			$$("select").invoke("setStyle", {visibility: "visible"});
		}
	},
	
	/**
	 * Avram.Overlay.setOptions(options) -> null
	 * Sets the overlay options.
	 * - options (Object): A JSON object containing the following options
	 *    (note, this is the same as the constructor parameter of the same name):
	 *    bgColor (String): The overlay's background color in hex form (i.e. "#660000" for a dark red).
	 *        default value: "#000000"
	 *    opacity (Double): The overlay's opacity 0.0 (transparent) to 1.0 (opaque)
	 *        default value: 0.5
	 *    hideOnClick (Boolean): If set to true, adds an onclick event to the overlay that hides itself.
	 *        default value: True
	 */
	setOptions: function(options) {
		if(options) {
			this._setOptions(options);
		}
	},
	
	
	/* Private Functions */
	
	/**
	 * Avram.Overlay._generateHTML() -> null
	 * Creates the HTML for the Overlay <div>.
	 */
	_generateHTML: function() {
		this.HTML = "<div id='"+this.overlayID+"' class='aeOverlay' style='display:none;'></div>";
	},
	
	/**
	 * Avram.Overlay._insert() -> null
	 * Inserts the Overlay HTML at the bottom of the <body> tag.
	 */
	_insert: function() {
		$$("body")[0].insert({bottom: this.HTML});
		this.overlayDiv = $(this.overlayID);
	},
	
	/**
	 * Avram.Overlay._setOptions() -> null
	 * Sets the style options and the click event for the overlay (if hideOnClick was set to true).
	 */
	_setOptions: function(options) {
		this._options = Object.extend(this.defaultOptions, options || {});
		
		this.overlayDiv.setStyle({
			backgroundColor: this._options.bgColor,
			opacity: this._options.opacity
		});
		
		if (this._options.hideOnClick) {
			this.overlayDiv.observe("click", function() {
				this.hide();
				aladdinCountrySelector.toggleDisplay();	// Custom for Aladdin (GS)
			}.bind(this));
		}
	}

});