/**
 *
 * @auther Adam Brill adam@solutionset.com
 * @date 20070122
 *
 * @see prototype.js v1.6
 * @see SS.Base.js for v1.6
 */

SS.Overlay = Class.create(SS.Base, {
	/**
	 * Object used to store settings. Extends the parent class settings
	 * @var Object
	 */
	s: {
		color: "#000",
		zIndex: 200,
		opacity: 50
	},
	
	/**
	 * Object used to store nodes. Extends the parent class nodes
	 * @var Object
	 */
	n: {
		element: null,
		background: null
	},
	
	/**
	 * @param function $super
	 * @param DOM Element element
	 * @param object options
	 * @return void
	 */
	initialize: function($super, element, options) {
		$super();
		
		this.n.element = $(element);
		
		//replacing getDimensions with a custom getPageSize by Lokesh Dhakar - http://www.lokeshdhakar.com
		//var dimensions = Element.getDimensions(document.body);
		var dimensions = this.getPageSize();
		var viewport = document.viewport.getDimensions();

		if (dimensions.width < viewport.width) dimensions.width = viewport.width;
		if (dimensions.height < viewport.height) dimensions.height = viewport.height;
		this.n.background = new Element('div').setStyle({
			width: "100%",
			height: "100%",
			position: "absolute",
			top: "0",
			left: "0",
			background: this.s.color,
			width: dimensions.width + "px",
			height: dimensions.height + "px",
			zIndex: this.s.zIndex,
			opacity: (this.s.opacity/100),
			filter: "alpha(opacity=" + this.s.opacity + ")"
		});
		
		this.n.element.setStyle({
			zIndex: this.s.zIndex+1
		});
		
		Element.insert(document.body, {bottom:this.n.background});
		
		this._captureEvents();
	},
	
	//
    //  getPageSize()
    //
    getPageSize: function() {
	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		
		//matching this to what initialize expects
		//return [pageWidth,pageHeight];
		return {width: pageWidth, height: pageHeight};
	},
	
	/**
	 * @param DOM Event e
	 * @return void
	 */
	onCloseElement: function(e) {
		Event.stop(e);
		this.remove();
	},
	
	/**
	 * @param void
	 * @return void
	 */
	remove: function() {
		this.n.background.remove();
	},
	
	/**
	 * @param void
	 * @return void
	 */
	_captureEvents: function() {
		Element.observe(this.n.element, "ss:close", this.onCloseElement.bindAsEventListener(this));
	}
});
