// JavaScript Document
var LadyOverlay = new Class({
	options: {
		countdown: 'count-down',
		transition: Fx.Transitions.linear,
		duration: 350,
		wait: 300,
		url: ''
	},
	
	initialize: function(el, options) {
		this.element = $(el); if (!this.element) return;
		this.setOptions(options);
		this.layout = $(el);
		this._close = $(el+'_close');
		
		this._close.addEvent('click', function(e) { this.toggle(e); }.bind(this));
		
		this.overlay = new Element("div").injectAfter(this.layout);
		this.overlay.setStyle('display', 'none');
		this.overlay.setStyles({'position': 'absolute', 'background': 'none repeat scroll 0 0 #000000', 
							   'visibility': 'hidden', 'left': 0, 'top': 0, 'z-index': 777, 'width': window.getWidth(), 'height': window.getHeight() + window.getScrollHeight()});				
		
		this.fxoverlay = new Fx.Elements(this.overlay, {wait: false, duration: this.options.duration, 
								   transition: this.options.transition, onComplete: function(){}.bind(this)});
		this.OverlayEvents();
		
		if(!Cookie.get(this.options.url))
		{
			this.toggle();
			
			if(this.options.wait) {
				this.count = this.options.wait;
				this.countdown();			
			}
			Cookie.set(this.options.url, 1);
		}
	},
	
	countdown: function() {
		this.count -= 1000;
		$(this.options.countdown).innerHTML = ((this.count/1000) < 10) ? ('0' + (this.count/1000)) : (this.count/1000);
		if(this.count >= 0) {
			this.countdown.delay(1000, this);
		} else {
			this.hide();
		}
	},
	
	toggle: function() {
		this[this.visible ? 'hide' : 'show']();
	},
	
	show: function() {
		var left = window.getWidth()/2 - this.layout.getStyle('width').toInt()/2;
		this.layout.setStyles({'display': 'block', 'z-index': 9999, 'position': 'fixed', 'left': left});
		
		var lazy = new Fx.Elements(this.layout, {wait: false, duration: this.options.duration, 
								   transition: this.options.transition, onComplete: function(){}.bind(this)});
		lazy.start({'0' : {'opacity': [0, 1]}});
		
		//Show overlay
		this.overlay.setStyle('display', 'block');
		this.fxoverlay.start({'0' : {'opacity': [0, 0.5]}});
		
		this.visible = true;
	},
	
	hide: function() {
		this.layout.setStyles({'display': 'none', 'z-index': 0, 'position': 'relative'});
		var lazy = new Fx.Elements(this.layout, {wait: false, duration: this.options.duration, 
								   transition: this.options.transition, onComplete: function(){this.overlay.setStyles({'display': 'none'});}.bind(this)});
		lazy.start({'0' : {'opacity': [1, 0]}});		
		
		//Hide overlay
		this.fxoverlay.start({'0' : {'opacity': [0.5, 0]}});
		
		this.visible = false;
	},
	
	OverlayEvents: function() {		
		this.overlay.addEvent('click', function() {
			if(this.visible) this.hide(this.layout);
		}.bind(this));				
	}
});
LadyOverlay.implement(new Options);
LadyOverlay.implement(new Events);
