var Overlay = new Class({

	initialize: function(options){;
		this.options    = $extend({ closeLink: true, destroyContent: false, centerOverlay: true, overlayFixedWidth: false, cloneContent: true }, options || {});
	    this.overlayMask = new Element('div', { 'id' : 'overlay-mask' }).setStyles({'background-color' : '#000', 
	    																			'width' : '100%', 
	    																			'height' : '100%', 
	    																			'position' : 'fixed', 
	    																			'top' : '0', 
	    																			'left' : '0',
	    																			'z-index' : '100'
	    																			}).fade('hide');
		compatibleOverlay = Browser.Engine.trident4 || (this.overlayMask.currentStyle && (this.overlayMask.currentStyle.position != "fixed"));
		
		if (compatibleOverlay) this.overlayMask.setStyles({'width': window.getScrollSize().x, 'height': window.getScrollSize().y, 'position': 'absolute'});
		
		var closeAction = function() { this.hide(); return false; }
		var boundHide = closeAction.bind(this);
	
		this.overlayMask.addEvent('click', boundHide);
		
		
		this.overlayWindow = new Element('div', { 'id' : 'overlay' }).fade('hide');
		if (!this.options.overlayFixedWidth) this.overlayWindow.setStyles({width: 'auto', left: 'auto', right: 'auto'});
		this.overlayWindow.setStyles({'margin' : '5em 0'});
		this.contents = new Element('div', { 'id' : 'contents' }).inject(this.overlayWindow);
		
		if (this.options.closeLink) {
			this.closeButton = new Element('p', { 'id' : 'close' }).inject(this.overlayWindow);
			this.closeLink = new Element('a', { 'html' : 'Close&nbsp;this&nbsp;window' }).setStyles({width: '100%', cursor: 'pointer'}).inject(this.closeButton);
			this.closeLink.addEvent('mouseup', boundHide);
		}
			
		this.overlayMask.set('tween', {duration: '400'});
	    this.overlayWindow.set('tween', {duration: '400'});
		
		$(document.body).adopt(this.overlayWindow, this.overlayMask); 
	},	

	show: function(content, options){
    this.options = $extend(this.options, options || {});
    
	    this.injectContent(content);
	
		this.overlayWindow.setStyles({'top' : window.getScrollTop() + 20 });
		
    if (this.options.centerOverlay) {
		  var winSize = window.getScrollSize();
		  var overlaySize = this.overlayWindow.getSize();
		  this.overlayWindow.setStyles({ 'left' : (winSize.x/2)-(overlaySize.x/2), 'top' :  window.getScrollTop() + 20 });
		}	
		
		this.fadeInOverlayMask.bind(this).delay('0');
	    this.fadeInOverlayWindow.bind(this).delay('200');
	},
	
	hide: function(){
	    this.overlayMask.fade('out');
	    this.overlayWindow.fade('out');
	    
	    if(this.options.destroyContent) {
	     this.destroyContents.delay('400', this);
	    }
	    
	},
	
	injectContent: function(content){
    this.contents.empty();
		if (this.options.cloneContent) {
	    	content.clone().inject(this.contents);
	    } else {
	    	content.inject(this.contents);
	    }
	},
	
	destroyContents: function(){
	   this.contents.empty();
	},
		
	fadeInOverlayMask: function(){
		this.overlayMask.fade(0, 0.8);
	},
	
	fadeOutOverlayMask: function(){
		this.overlayMask.fade('out');
	},
	
	fadeInOverlayWindow: function(){		
		this.overlayWindow.fade(0,1); 
	},
	
	fadeOutOverlayWindow: function(){
	    this.overlayWindow.fade('out');
	}
});