var Lightbox = new Class({
	bg: false,
	content: false,
	is_visible: false,
	close_btn: false,
	
	options: {
		width: 300,
		bg_close: true,
		fixed: false,
		y: false
	},
	
	initialize: function(content_id, options) {
		this.setOptions(options);
		
		this.close_btn = new Element('div', {
			'class': 'close_btn',
			'html': '<span>&times;</span> Close',
			'events': {
				'click': this.hide.bind(this)
			}
		});
		
		$(document.body).adopt(this.close_btn);
		
		this.bg = new Element('div', { 
			'style': 'position: absolute; top: 0px; left: 0px; background: #000; opacity: 0.85; filter: alpha(opacity=85); z-index: 1500;'
		});
		
		$(document.body).adopt(this.bg);
		
		if(this.options.bg_close) this.bg.addEvent('click', this.hide.bind(this));
		
		this.content = $(content_id).moveToBottom();
		this.content.setStyle('position', 'absolute');
		this.content.setWidth(this.options.width);
		this.content.setX(0);
		
		if( this.options.y ) {
			this.content.setY(this.options.y);
		}
		
		this.hide();
		
		window.addEvent('resize', this.redraw.bind(this));
		window.addEvent('scroll', this.redraw.bind(this));
	},
	
	redraw: function(e) {
		if(this.is_visible) {
			var w = window.getWidth();
			var h = window.getHeight();
			
			this.content.setX(w / 2 - this.content.getWidth() / 2);
			
			if(!this.options.y)
				this.content.setY(window.getScrollTop() + (h / 2 - this.content.getHeight() / 2));
			
			this.bg.setWidth(w);
			this.bg.setHeight(window.getScrollHeight());
			//this.bg.setY(window.getScrollTop());
		}
	},
	
	show: function() {
		if (!this.is_visible) {
			this.close_btn.show();
			this.is_visible = true;
			this.content.show();
			
			this.bg.setHeight(window.getScrollHeight());
			this.bg.show();
			
			if (Browser.Engine.webkit) {
				this.bg.setY(window.getScrollTop());
			}
			
			if( this.options.fixed ) {
				this.options.y = window.getScrollTop() + (window.getHeight() / 2 - this.content.getHeight() / 2)	
				if (this.options.y < 0) this.options.y = 20;
				this.content.setY(this.options.y);
			}
			
			this.redraw();
				
			try {
				pageTracker._trackEvent('Lightbox', 'Open', this.content.id);
			} catch(e) { }
		}
	},
	
	hide: function() {
		this.is_visible = false;
		this.bg.hide();
		this.content.hide();
		this.close_btn.hide();
	},
	
	close: function() {
		this.hide();
	}
});
Lightbox.implement(new Options);
