var tabModule = Class.create({

	initialize : function(b) {
		this.base = b;
		b.tabModule = this;
		
		this.setOptions();
		this.setProperties();
		this.setEvents();
		this.setCurrentTab(this.options.defaultTab-1);
		this.setTimer();
	},

/*
 *************************
 * INITIALIZATION
 *************************
 */

	setOptions : function() {
		this.options = {
			navSelector : '',
			contentSelector : '',
			currentClass : 'current',
			hoverNavClass : 'hover',
			transition : 'none',
			auto : 0,
			duration : 1,
			navEnabled : true,
			defaultTab : 1
		};
		
		for (var i in this.options) {
			this.options[i] = this.base.getAttribute(i) || this.options[i]; 
		}	

		this.options.duration = parseFloat(this.options.duration);
		this.options.auto = parseFloat(this.options.auto);
		this.options.navEnabled = (this.options.navEnabled=='false')?false : true;
	},

	setProperties : function() {
		this.nav = this.base.select(this.options.navSelector);
		this.con = this.base.select(this.options.contentSelector);
		this.isAnimating = false;
	},

	setCurrentTab : function(x) {
		this.currIndex = x;
		for (var i=0; i<this.nav.length; ++i) {
			if (this.nav[i].hasClassName(this.options.currentClass)) { 
				this.nav[i].removeClassName(this.options.currentClass); 
			}
		}
		this.con[x].addClassName(this.options.currentClass);
		this.nav[x].addClassName(this.options.currentClass);
	},

	setEvents : function() {
		if (!this.options.navEnabled) {return}

		var eventType = (Prototype.Browser.IE) ? 'mouseleave' : 'mouseout';

		this.nav.each(function(n, i){
			n.observe('click', this.onNavClick.bind(this, i));
			n.observe('mouseover', function(){if(i != this.currIndex) {n.addClassName(this.options.hoverNavClass)}}.bind(this,i));
			n.observe(eventType,  function(){n.removeClassName(this.options.hoverNavClass)}.bind(this));
		}.bind(this));
	},

	setTimer : function() {
		if (this.timer) { 
			this.timer.stop(); 
		}
		if (this.options.auto) { 
			this.timer = new PeriodicalExecuter(this.onAutoChange.bind(this), this.options.auto);
		}
	},
	
	stopAutoChange : function() {
		if (this.timer) {
			this.timer.stop();
		}
		
		this.options.auto = 0;
	},

/*
 *************************
 * EVENTS
 *************************
 */

	onAutoChange : function() {
		var i = (this.currIndex + 1 < this.nav.length) ? this.currIndex + 1 : 0;
		this.changeActiveTab(i);
	},

	onNavClick : function(i) {
		if (i == this.currIndex) { 
			return; 
		}
		this.changeActiveTab(i);
	},

	changeActiveTab : function(i) {
		if (this.isAnimating) {
			return;
		}
		var t = tabModule.transitions[this.options.transition];
		t(i, this);
	}

});

/*
 *************************
 * TRANSITIONS
 *************************
 */

tabModule.transitions = {

	none : function(i, scope) {
		var curr = scope.con[scope.currIndex];
		var next = scope.con[i];

		curr.removeClassName(scope.options.currentClass);
		next.addClassName(scope.options.currentClass);

		scope.nav[i].addClassName(scope.options.currentClass);
		scope.nav[scope.currIndex].removeClassName(scope.options.currentClass);
		
		scope.currIndex = i;
		scope.setTimer();
	}, 

	fade : function(i, scope) {
		var curr = scope.con[scope.currIndex];
		var next = scope.con[i];

    new Effect.Opacity(curr, { 
      from:1,
      to:0,
      duration : scope.options.duration,
      beforeStart : function() {	
				next.setStyle({zIndex:4});
				curr.setStyle({zIndex:5});
				curr.removeClassName(scope.options.currentClass);
				scope.isAnimating = true;
				scope.nav[scope.currIndex].removeClassName(scope.options.currentClass);
				scope.nav[i].addClassName(scope.options.currentClass);
			}.bind(this),
			afterFinish : function() {
				next.addClassName(scope.options.currentClass);
				scope.currIndex = i;
				scope.isAnimating = false;
				curr.setStyle({zIndex:1, opacity:1});
				next.setStyle({zIndex:5});
				scope.setTimer();
			}.bind(this),
			queue : {
				position : "end",
				scope : scope.base.id
			}
		});
	}, 

	scroll : function(i, scope) {
		var curr = scope.con[scope.currIndex];
		var next = scope.con[i];

		new Effect.Move(next, {
			x : 0,
			y : 0,
			mode : "absolute",
			beforeStart : function() {
				curr.setStyle({"zIndex" : 4});
				next.setStyle({
					"zIndex" : 5, 
					left : scope.base.scrollWidth + "px" 
				});
				curr.removeClassName(scope.options.currentClass);
				scope.isAnimating = true;
				scope.nav[scope.currIndex].removeClassName(scope.options.currentClass);
				scope.nav[i].addClassName(scope.options.currentClass);
			},
			afterFinish : function() {
				next.addClassName(scope.options.currentClass);
				scope.currIndex = i;
				scope.isAnimating = false;
				curr.setStyle({zIndex:1});
				next.setStyle({zIndex:5});
				scope.setTimer();
			},
			duration : scope.options.duration,
			queue : {
				position : "end",
				scope : scope.base.id
			}		
		});
	}

};

NBAUtil.Events.addDomLoadedHandler(function() { $$('.tabModule').each(function(n) { new tabModule(n); }); });

