if (typeof(console) == 'undefined') {
    var console = {
        log: function(){}
    };
}

if (typeof(getPageSize) == 'undefined') {
	function getPageSize() {
	
		var xScroll, yScroll;
	
		if (window.innerHeight && window.scrollMaxY) {
			xScroll = document.body.scrollWidth;
			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
			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 = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	}
}

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

var Overlay = Class.create();

Overlay.prototype = {
	
	overlay: undefined,
	
	initialize: function() {
		
		this.options = Object.extend({
			afterCreation: false
		}, arguments[0] || {});
		
		var b = document.getElementsByTagName("body").item(0);
		this.overlay = document.createElement("div");
		this.overlay.setAttribute('id','overlay');
		this.overlay.style.display = 'none';
		this.overlay.style.position = 'absolute';
		this.overlay.style.top = '0';
		this.overlay.style.left = '0';
		this.overlay.style.zIndex = '90';
	 	this.overlay.style.width = '100%';
		b.insertBefore(this.overlay, b.firstChild);
		
		hideSelectBoxes();
	
		var arrayPageSize = getPageSize();
		this.overlay.style.height = (arrayPageSize[1] + 'px');		
		
		Effect.Appear(this.overlay, {
			to: 0.6,
			duration: 0.25,
			afterFinish: (function() {
				if (this.options.afterCreation) {
					this.options.afterCreation.bind(this)();
				}
			}).bind(this)
		});
		
		
	},
	
	remove: function() {
		new Effect.Fade(this.overlay, { 
			duration: 0.5, 
			afterFinish: (function() { 
			    try {
    				this.overlay.remove();
			    } catch (e) { }
				showSelectBoxes();
			}).bind(this)
		});
		
	}	

};

Position.center = function(element) {
    if (element.hasClassName('centered')) return;
    var options = Object.extend({
        zIndex: 999,
        update: false
    }, arguments[1] || {});
    element = $(element)
    if(!element._centered) {
        Element.setStyle(element, {position: 'absolute', zIndex:options.zIndex });
        element._centered = true;
    }
    var dims = Element.getDimensions(element);
    Position.prepare();
    var winWidth = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0;
    var winHeight = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
    var offLeft = (Position.deltaX + Math.floor((winWidth-dims.width)/2));
    var offTop = (Position.deltaY + Math.floor((winHeight-dims.height)/2));
    element.style.top = ((offTop != null && offTop > 0) ? offTop : '0')+ 'px';
    element.style.left = ((offLeft != null && offLeft > 0) ? offLeft :'0') + 'px';
    element.addClassName('centered');
    if(options.update) {
        Event.observe(window, 'resize', function(evt) {
            Position.center(element);
        }, false);
        Event.observe(window, 'scroll', function(evt) {
            Position.center(element);
        }, false);
    }
}

Element.addMethods();
