/**
 * Catharina Bloom
 * (c) 2006-2009 Omines - www.omines.com
 * 
 * All rights explicitly reserved - unauthorized reproduction strictly prohibited
 */

// Compatibility stuff
Element.implement({
	effect: function(property, options){
		return new Fx.Tween(this, $extend({property: property}, options));
	}
});

// Logger class
var Logger = {
	initialize: function()
	{
		// Check if we can use console.log, assuming no
		this.console		= false;
		if (window.console && window.console.firebug != '')
			this.console	= true;
	},
	log: function(message, level)
	{
		if (!this.console)
			return;
		switch (level)
		{
			case 0:	console.log(message);	break;
			case 1:	console.info(message);	break;
			case 2: console.warn(message);	break;
			case 3: console.error(message);	break;
		}
	}
};

function Log(message)	{ Logger.log(message, 0); }
function Info(message)	{ Logger.log(message, 1); }
function Warn(message)	{ Logger.log(message, 2); }
function Error(message)	{ Logger.log(message, 3); }

var Lightbox = {
	initialize: function()
	{
		var closer = this.close.bind(this);
		this.overlay	= new Element('div', {'id':'lb-overlay'}).injectInside(document.body).addEvent('click', closer);
		this.box		= new Element('div', {'id':'lb-box'}).injectInside(document.body);
		this.closelink	= new Element('a', {'id':'lb-closer'}).injectInside(this.box).addEvent('click', closer);
		this.content	= new Element('div', {'id':'lb-content'});
		this.content.injectInside(this.box);
 		this.fx = {
			overlay :this.overlay.effect('opacity', {duration: 500,wait: false}),
			box :this.box.effect('opacity', {duration: 500,wait: false})
		};
 		this.boxProp	= {
 			width: this.box.getStyle('width'),
 			height: this.box.getStyle('height')
 	 	};
		this.overlay.setStyle('visibility', 'hidden');
		this.box.setStyles({
			'visibility':'hidden',
			'width':'250px',
			'height':'50px'
		});
		$$('.lightbox').each(function(el, idx) {
			el.addEvent('click', this.clickLink.bindWithEvent(this));		
		}, this);
	},
	showLoading: function()
	{
		this.content.empty();
		this.loader		= new Element('p', {'id':'lb-loader', 'text':' Loading....'}).injectInside(this.content);
		this.loaderImg	= new Element('img', {'id':'lb-loaderimg','src':'/static/images/ajax-loader.gif','alt':'loading'}).inject(this.loader, 'top');
		this.box.setStyles({
			'visibility':'hidden',
			'width':'250px',
			'height':'50px'
		});
	},
	position: function()
	{
		this.overlay.setStyles({top:0,left:0,width:window.getScrollWidth(),height:window.getScrollHeight()});
		this.box.setStyles({display: '',top:(window.getScrollTop()+32)+'px'});
	},
	clickLink: function(e)
	{
		this.showPage(e.target.href);
    	e.stop();
	},
	showPage : function(getUrl) {
		this.position();
		this.showLoading();
		new Request.HTML( {
			url : getUrl,
			method :'get',
			update :$('lb-content'),
			onComplete : function() {
				$('lb-content').setStyle('display','none');
				this.box.set('tween', {
					duration: 250,
					link: 'chain',
					onChainComplete: function () {
						$('lb-content').setStyle('display','block');
					}
				});
				this.box.tween('width', [250, this.boxProp.width]);
				this.box.tween('height', [50, this.boxProp.height]);
			}.bind(this)
		}).send();
 		this.fx.overlay.start(0, 0.5);
		this.fx.box.start(1);
	},
	close: function()
	{
		this.fx.overlay.start(0);
		this.fx.box.start(0);
	}
};

window.addEvent('domready', function(){
	Logger.initialize();
	Lightbox.initialize();
	$$('a').each(function(a) {
		if (a.get('href') != '' && a.get('rel') == 'external')
		a.set('target', '_blank');
	});
	/*
	$$('a').each(function (e) {
		var props	= e.getProperties();
		if (props.rel != false)
			Log(props.rel);
		//if (e.getProperty('href') && e.getProperty('rel').test('external'))
		//	e.set('target', '_blank');
	});*/
});

