//triggerMode : mousehover, click, autoh

function Tabs(target, mode, speed, target_is_menu) {this.init.apply(this, arguments);}
Tabs.prototype = {
	init: function(target, mode, speed, target_is_menu) {
		this.target = $(target);
		this.labels = this.target.find(target_is_menu === true?'label':'.tab-menu label');
		this.currentLabel = this.labels.not('not(.current)').get(0);
		var othis = this;
		this.labels.bind((mode == 'autoplay' || mode == 'hover')?'mouseover':'click', function() {othis.setTab(this);});
		if(mode == 'autoplay') {
			if(typeof parseInt(speed) != 'undefined') {
				speed = parseInt(speed);
			} else {
				speed = (speed == 'fast')?1000:(speed == 'slow')?4000:2500;
			}
			this.timer = setInterval(function(){othis.nextTab();}, speed);
			this.labels.each(function(){
				$(this).add('#' + this.htmlFor).hover(
					function(){clearInterval(othis.timer);},
					function(){othis.timer = setInterval(function(){othis.nextTab();}, speed);}
				);
			});
		}
	},
	nextTab : function () {		
		this.setTab(this.labels.get( (this.labels.index(this.currentLabel) + 1) % this.labels.length));
	},
	setTab : function (label) {
		if(label == null || this.currentLabel == label) return;
		$(this.currentLabel).add('#'+this.currentLabel.htmlFor).removeClass('current');	
		$(label).add('#'+label.htmlFor).addClass('current');
		this.currentLabel = label;	
	}
};