/*
Script: tabSlider-v1.1.js
	tabSlider - tabSlider version 1.1

License:
	MIT-style license.

Copyright:
	Copyright 2008
	Trebbers
	http://www.trebbers.nl

Based on MooTools v1.2.1 Core
	[The MooTools production team](http://mootools.net/developers/).

More information @ www.mootools.nl or http://mootools.trebbers.nl

Keep smiling ;)
*/

var tabSlider = new Class({

	Implements: [Events, Options],

	options: {
		slideContainer:'slide-container',
		evalScripts:false,
		duration:500,
		transition:Fx.Transitions.Quad.easeOut,
		show:'0',
		failure:'<strong class="error">Unable to find the page - Please try again later.</strong>',
		active:'active'
	},

	initialize: function(element,options){
		this.setOptions(options);
		this.sliders = {};
		this.sliders[element] = 0;
		this.active = false;
		if(!$(this.options.slideContainer)) {
			this.log('container "' + this.options.slideContainer + '" doesn\'t exist.');
			return;
		} else {
			this.log('tabSlider instance loaded for "' + this.options.slideContainer + '".');
		}
		$$(element).each(this.tabSlide.bindWithEvent(this,element));
		this.fx = new Fx.Slide($(this.options.slideContainer),{
			duration:this.options.duration,
			transition:this.options.transition,
			onComplete:function(){ this.active=false; }.bind(this)
		}).hide();
		if(this.options.display!='none' && this.options.display>=0 && $$(element)[this.options.display])
			this.openSlide($$(element)[this.options.display],element,true);
		else if(this.options.show!='none' && this.options.show>=0 && $$(element)[this.options.show])
			this.openSlide($$(element)[this.options.show],element,false);
	},
	
	tabSlide: function(el,element){
		el.addEvents({
			'click': function(e){
				e.stop();
				if(!this.active) {
					this.active=true;
					$$(element).removeClass(this.options.active);
					if(this.fx.open && this.sliders[element]==el) {
						this.fx.slideOut();
					} else if(!this.fx.open && this.sliders[element]==el) {
						this.sliders[element].addClass(this.options.active);
						this.fx.slideIn();
					} else if(this.fx.open) {
						this.fx.slideOut().chain(function(){ this.openSlide(el,element,true) }.bind(this));
					} else {
						this.openSlide(el,element,true);
					}
				}
			}.bind(this)
		});
	},
	
	openSlide: function(el,element,transition){ 
		new Request.HTML({
			url:el,
			method:'get',
			update:$(this.options.slideContainer),
			link:'chain',
			evalScripts:this.options.evalScripts,
			onSuccess:function(){
				this.sliders[element] = el;
				this.sliders[element].addClass(this.options.active);
				if(transition)
					this.fx.slideIn();
				else
					this.fx.show();
			}.bind(this),
			onFailure:function(){
				$(this.options.slideContainer).set('html',this.options.failure);
				if(transition)
					this.fx.slideIn();
				else
					this.fx.show();
			}.bind(this)
		}).send();
	},
	
	log: function(text, args) {
		if (window.console) console.log(text.substitute(args || {}));
	}

});