
var e24mooWin = new Class ({
	initialize: function(content, options) {
		this.width = options.width;
		this.height = options.height;
		this.closebtn = options.closebtn;
		this.callback = options.callback;
		this.content = content;
		this.currentEl = undefined;
		this.working = false;
		this.eventPosition = this.position.bind(this);
		
		this.overlay = new Element('div', {
		    'styles': {
		    	'display': 'none',
		    	'background': '#07963C',		    	
		    	'position':window.ie6?'absolute':'fixed',
		    	'z-index':7000,
		        'left': '0px',
		        'top': '0px',
		        'width': '0px',
		        'height': '0px'
		    },
		    'class': 'e24lb_overlay'
		});
		this.overlay.injectInside(document.body);
		this.window = new Element('div', {
		    'styles': {
		    	'display':'none',
		    	'background': '#fff',
		    	'position':window.ie6?'absolute':'fixed',
		    	'z-index':7001,
		    	'overflow':'hidden',
		    	'text-align':'center',
		        'left': '0px',
		        'top': '0px',
		        'width': '0px',
		        'height': '0px'
		    },
		    'class': 'e24lb_window'
		});
		this.window.injectInside(document.body);
		this.container = new Element('div', {
		    'styles': {
		    	'background': '#fff',
				'padding':'5px'
		    },
		    'class': 'e24lb_container'
		    
		});
		this.container.injectInside(this.window);
		this.content.injectInside(this.container);
		this.content.setStyle('display', 'block');
		
		this.containerfx = new Fx.Style(this.container, 'opacity', {duration:500});
		this.overlayfx = new Fx.Style(this.overlay, 'opacity', {duration: 200, transition: Fx.Transitions.Quad.easeOut});
		this.windowfx = new Fx.Styles(this.window, {duration: 500, transition: Fx.Transitions.Quad.easeOut});		
		this.windowfx.addEvent('onComplete', function() {
		   if (this.closing) {
		   		this.closing = false;				   
				window['removeEvent']('scroll', this.eventPosition)['removeEvent']('resize', this.eventPosition);
		   		this.overlay.setStyles({display: 'none', opacity: 0});
				this.window.setStyles({display: 'none'});
				if (this.remove) this.content.remove();
		   }
		   else {
     			this.container.setStyles({display: 'block', opacity: 0});
     			this.containerfx.start(0, 1.0);
		   		if (this.callback) {
		   			this.callback();
		   		}
		   }
	    }.bind(this));		

		this.overlay.addEvent('click', this.close.bindWithEvent(this));
		this.closebtn.addEvent('click', this.close.bindWithEvent(this));
	},
	
	position: function(){
		if (window.ie6) {
			this.overlay.setStyles({'top': window.getScrollTop(), 'width': window.getWidth(), 'height': window.getHeight()});
		}	
		this.window.setStyles({'left': window.getWidth()/2 - this.width/2 + (window.ie6?window.getScrollLeft():0), 'top': window.getHeight()/2 - this.height/2 + (window.ie6?window.getScrollTop():0)});
	},

	show: function(el, options) {
		this.width = options.width.toInt();
		this.height = options.height.toInt();
		this.remove = options.remove;
		this.zindex = options.zindex?options.zindex:7000;

		
		if (this.working || this.closing || this.currentEl == el) return;
		this.working = true;
		this.currentEl = el;
     	this.container.setStyles({display: 'none', opacity: 0 });
		this.overlay.setStyles({'z-index':this.zindex+1,width:window.getWidth() + window.getScrollLeft() + 'px', height:window.getHeight() + window.getScrollTop() + 'px', display: 'block', opacity: 0});
		this.overlayfx.start(0, 0.8);
		
		window['addEvent']('scroll', this.eventPosition)['addEvent']('resize', this.eventPosition);
		
		var coords = el.getCoordinates();
		this.window.setStyles({'z-index':this.zindex+2, left:coords.left - (window.ie6?window.getScrollLeft():0) + 'px', top:coords.top - (window.ie6?window.getScrollTop():0) + 'px', width:coords.height + 'px', height:coords.height + 'px', display: 'block'});
		this.windowfx.start({
			//'opacity': [0, 1],
			'left': [coords.left - (!window.ie6?window.getScrollLeft():0), window.getWidth()/2 - this.width/2 + (window.ie6?window.getScrollLeft():0)],
			'top': [coords.top - (!window.ie6?window.getScrollTop():0), window.getHeight()/2 - this.height/2 + (window.ie6?window.getScrollTop():0)],
			'height': [coords.height, this.height],
			'width': [coords.width, this.width]
		});
		
	
	},
	
	fire: function(event, el) {
		event = new Event(event);
		event.stop();
		this.show(el, {});
	},

	close: function(event) {
		if (event) {
			event = new Event(event);
			event.stop();
		}	
		if (this.closing) return;

		this.working = false;		
		this.closing = true;		
		var coords = this.currentEl.getCoordinates();
		this.currentEl = undefined;
		this.containerfx.start(1.0, 0);
		this.windowfx.start({
			//'opacity': [1, 0],
			'left': [window.getWidth()/2 - this.width/2 + (window.ie6?window.getScrollLeft():0), coords.left - (!window.ie6?window.getScrollLeft():0)],
			'top': [window.getHeight()/2 - this.height/2 + (window.ie6?window.getScrollTop():0), coords.top - (!window.ie6?window.getScrollTop():0)],
			'height': [this.height, coords.height],
			'width': [this.width, coords.width]
		});
		this.overlayfx.start(0.8, 0);
	}
});			

