/**************************************************************

	Script		: Overlay
	Version		: 1.2
	Authors		: Samuel birch
	Desc		: Covers the window with a semi-transparent layer.
	Licence		: Open Source MIT Licence

	Modified: Leonardo Di Lella (leonardo@dilella.org) - MooTools 1.2 support

**************************************************************/

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;
}


var Overlay = new Class({
	
	getOptions: function(){
		return {
			colour: '#000',
			opacity: 0.7,
			zIndex: 1,
			container: document.body,
			id:'',
			before:false,
			onClick: new Class()
		};
	},

	initialize: function(options){
		this.setOptions(this.getOptions(), options);
		
		this.options.container = $(this.options.container);
		
		this.container = new Element('div').setProperty('id', 'OverlayContainer'+this.options.id).setStyles({
			position: 'absolute',
			left: '0px',
			top: '0px',
			width: '100%',
			visibility: 'hidden',
			overflow: 'hidden',
			zIndex: this.options.zIndex
		});
		
		if(this.options.before)
		this.container.inject(this.options.before,'before');
		else
		this.container.inject(this.options.container,'inside');
		
		this.iframe = new Element('iframe').setProperties({
			'id': 'OverlayIframe',
			'name': 'OverlayIframe',
			'src': 'javascript:void(0);',
			'frameborder': 1,
			'scrolling': 'no'
		}).setStyles({
			'position': 'absolute',
			'top': 0,
			'left': 0,
			'width': '100%',
			'height': '100%',
			'filter': 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)',
			'opacity': 0,
			'zIndex': 1
		}).inject(this.container,'inside');
		
		this.overlay = new Element('div').setProperty('id', 'Overlay').setStyles({
			position: 'absolute',
			left: '0px',
			top: '0px',
			width: '100%',
			height: '100%',
			zIndex: 2,
			backgroundColor: this.options.colour
		}).inject(this.container,'inside');
		
		this.container.addEvent('click', function(){
			this.options.onClick();
		}.bind(this));
		
		this.fade = new Fx.Morph(this.container, 'opacity').set(0);
		this.position();
		
		window.addEvent('resize', this.position.bind(this));
	},
	
	position: function(){ 
		if(this.options.container == document.body){ 
			var arrayPageSize = getPageSize();
			
			this.container.setStyles({top: '0px', height: arrayPageSize[1]});
			//this.container.setStyles({top: '0px', height: '100%'});
		}else{ 
			var myCoords = this.options.container.getCoordinates(); 
			this.container.setStyles({
				top: myCoords.top+'px', 
				height: myCoords.height+'px', 
				left: myCoords.left+'px', 
				width: myCoords.width+'px'
			}); 
		} 
	},
	
	show: function(){
	
	    var arrayPageSize = getPageSize();
		this.container.setStyles({top: '0px', height: arrayPageSize[1]});
					if(!this.options.before)
					$$('select').each(function(el){
                        el.setStyle('visibility', 'hidden');
                       });

		this.fade.start({'opacity':[0,this.options.opacity], 'visibility':'visible'});
		
	},
	
	hide: function(){
	                 $$('select').each(function(el){
                        el.setStyle('visibility', 'visible');
                       });
					
		this.fade.start({'opacity':[0,this.options.opacity], 'visibility':'hidden'});
	}
	
	
	
});

Overlay.implement(new Options);



/*************************************************************/

var periodicalid=0;
var overlay_corect = function (){
        
         if($('OverlayContainer_dec'))	
         {
           //var arrayPageSize = getPageSize();
		      var scroll = $(document.body).getScrollSize();
		  
		   if($(this.ids+'_body'))
           //if(parseFloat($(this.ids+'_body').getStyle('height'))+150>arrayPageSize[1])		
		   $('OverlayContainer_dec').setStyles({top: '0px', height: scroll.y});	
		 }  
		 else
		 $clear(periodicalid);
		 
		 this.lp++;
		 
		 if(this.lp>10)
		 $clear(periodicalid);
     }
