var Lightbox = {

	listeners: [],

	init: function(options) {
		this.options = Object.extend({
			initialWidth: 540,
			initialHeight: 300
		}, options || {});

		$each(document.links, function(el) {
			if (el.rel && el.rel.test(/^lightbox/i)) {
				el.onclick = this.open.pass(el, this);
			}
		}, this);
		this.eventPosition = this.positionOverlay.bind(this);

		this.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.body);
		this.overlay.addEvent('click', this.close.bindWithEvent(this));
		this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({width: this.options.initialWidth, height: this.options.initialHeight, marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);
		this.canvas = new Element('div').setProperty('id', 'lbCanvas').injectInside(this.center);

		this.elements = $A(document.getElementsByTagName('object'));
		this.elements.extend(document.getElementsByTagName('embed'));
		if (window.ie) this.elements.extend(document.getElementsByTagName('select'));
	},

	addListener: function(fn) {
		this.listeners.push(fn);
	},

	open: function(link) {
		
		var test = link + ' ';
		if (test.indexOf('video') != -1)
		{
			this.video = "true";
		}
		else
		{
			this.video = "false";
		}

		this.loadContent(link);

		this.canvas.setStyles({width: this.options.initialWidth, height: this.options.initialHeight});
		this.center.className = 'lbLoading';
		this.position();
		this.overlay.setStyles({opacity: 0.8, display: 'block'});
		this.center.setStyles({width: 'auto', height: 'auto'});

		this.elements.each(function(el) { el.style.visibility = 'hidden'});
		window.addEvents({'scroll': this.eventPosition, 'resize': this.eventPosition});

		return false;
	},

	position: function() {
		var ww = (window.getWidth() == 0) ? window.getScrollWidth()-22 : window.getWidth();
		var wh = (window.getHeight() == 0) ? window.getScrollHeight() : window.getHeight();
		var st = document.body.scrollTop  || document.documentElement.scrollTop;

		this.center.setStyle('display', 'block');
		size = this.center.getSize();
		size = size['size'];
		
		// if its a video
		if (this.video == "true")
		{
			this.top = st + ((wh - size.y) / 2) - 200;
			this.center.setStyles({top: this.top});			
		}
		else
		{
			this.top = st + ((wh - size.y) / 2);
			this.center.setStyles({top: this.top, 'margin-left': -size.x / 2});
		}
		
		this.overlay.setStyles({top: st, height: wh, width: ww});
	},

	positionOverlay: function() {
		var ww = (window.getWidth() == 0) ? window.getScrollWidth()-22 : window.getWidth();
		var wh = (window.getHeight() == 0) ? window.getScrollHeight() : window.getHeight();
		var st = document.body.scrollTop  || document.documentElement.scrollTop;

		this.overlay.setStyles({top: st, height: wh, width: ww});
	},

	loadContent: function(link) {
		this.loader = new Ajax(link + '&rand=' + Math.floor(Math.random() * 101), {method: 'get', onComplete: this.loadContentComplete.bind(this)}).request();
		var content = this.canvas.getFirst();
		if (content) {
			content.remove();
		}
	},

	loadContentComplete: function(resp) {
		this.center.setStyles({width: 'auto', height: 'auto'}).className = '';
		this.canvas.setStyles({width: 'auto', height: 'auto'}).setHTML(resp);
		if (this.canvas.getElement('.video-player')) {
			this.canvas.getElement('.dialogue').setStyles({width: 'auto', height: 'auto'});
		}
		this.position();

		this.loader.evalScripts();
		$ES('a.close-link').each(function(el) {
			el.addEvent('click', this.close.bindWithEvent(this));
		}, this);
	},

	close: function() {
		this.center.style.display = this.overlay.style.display = 'none';
		this.elements.each(function(el) { el.style.visibility = ''});
		window.removeEvent('scroll', this.eventPosition).removeEvent('resize', this.eventPosition);

		return false;
	}
};

window.addEvent('domready', Lightbox.init.bind(Lightbox));

