/*
ie:

window.addEvents({
	domready: function(){
		new ExitPopup($('test'));		
	}
});

*/
var ExitPopup = new Class({
	options: {
		hotspot:{
			'height': '10px'
		},
		modal: {
			'background-color': '#000000',
			'opacity': .7
		}
	},
	exitpopup_hotspot: Class.empty,
	exitpopup_modal: Class.empty,
	exitpopup_container: Class.empty,
	exitpopup_content: Class.empty,
	exitpopup_el: Class.empty,
	disabled: false,
	initialize: function(el, options){
		if ($type(el) != 'element') return;

		// init HOTSPOT
		this.exitpopup_hotspot = new Element('div', {
			'id': 'exit_popup_hotspot',
			'styles': {
				'width': '100%',
				'position': 'absolute',
				'top': '0px',
				'left': '0px',
				zIndex: '20080518'
			}
		})
		.setStyles(this.options.hotspot)
		.injectTop(document.body)
		.addEvents({
			mouseenter: this.show.bind(this) 
		});
		
		// init MODAL
		this.exitpopup_modal = new Element('div', {
			'id': 'exit_popup_modal',
			'styles': {
				width: '100%',
				height: '100%',
				position: 'absolute',
				top: '0px',
				left: '0px',
				display: 'none',
				zIndex: '20080518',
				'background-color': '#000000',
				'opacity': .7
			}
		})
		/*.setStyles(this.options.modal)*/
		.injectAfter(this.exitpopup_hotspot);
		//alert('x='+$(document.body).getSize().size.x+' y='+$(document.body).getSize().size.y);
		
		// init COTAINER
		this.exitpopup_container = new Element('div', {
			'id': 'exit_popup_container',
			'styles': {
				position: 'absolute',
				top: '0px',
				left: '0px',
				width: '100%',
				zIndex: '20080524'
			}
		})
		.injectAfter(this.exitpopup_modal);
		
		this.exitpopup_el = el;
		
		// init content holder
		this.exitpopup_content = new Element('div', {
			'id': 'exit_popup_content',
			'styles': {				
				dislay: 'none'
			}
		})
		.injectTop(this.exitpopup_container)
		.adopt(this.exitpopup_el);
		
		
		var closex = $E('a#close', this.exitpopup_content);
		if (closex) {
			closex.addEvents({
				click: this.hide.bind(this) 
			});
		};
		var closex = $E('a#close_disable', this.exitpopup_content);
		if (closex) {
			closex.addEvents({
				click: this.hide_disable.bind(this) 
			});
		};
		
		
	},
	show: function(e){
		if (this.disabled) return;
		
		var s = $(window).getSize();
		this.exitpopup_modal.setStyles({
			display:'block',
			width: s.size.x+'px',
			height: (s.size.y+100)+'px'
		});
		this.exitpopup_content.setStyles({
			display:'block'
		});
		this.exitpopup_el.setStyles({
			display:'block'
		});
		window.fireEvent('scroll');
	},
	hide: function(e){
	
		e = new Event(e);
		e.stop();
		
		this.exitpopup_modal.setStyle('display','none');
		this.exitpopup_content.setStyle('display','none');
		this.exitpopup_el.setStyle('display','none');
		
		this.disabled = true;
		
		//delay the exit popup for another 10 seconds
		(function(){this.disabled = false;}).delay(10000,this);

	},
	hide_disable: function(e){
	
		e = new Event(e);
		e.stop();
		
		this.exitpopup_modal.setStyle('display','none');
		this.exitpopup_content.setStyle('display','none');
		this.exitpopup_el.setStyle('display','none');
		this.disabled = true;
	}
});
ExitPopup.implement(new Options);
ExitPopup.implement(new Events);

window.addEvents({
	domready: function(){
		if ($('exitpopup')) (function(){ new ExitPopup($('exitpopup')); }).delay(10000);
	},
	load: function(){
		window.addEvents('scroll', function(){
			var s = $(window).getSize();
			try{
			if ($('exit_popup_modal').getStyle('display') == 'block'){
				$('exit_popup_modal').setStyle('top',(s.scroll.y-50));
				$('exit_popup_container').setStyle('top',s.scroll.y);
			}
			$('exit_popup_hotspot').setStyle('top',s.scroll.y);
			}catch(e){};
		});
	} 
});
