/**
 * HBV Rotating Teaser. Auto-rotates over tabbed navis.
 * Adjust timings within the class' DELAYS object.
 * @author Lennart Pegel <lennart@neofonie.de>
 *
 * @example
 *    new RotatingTeaser('#teasertabs ul');
 */
function RotatingTeaser(sel) {
	this.DELAYS = {
		START: 8000,	// very first switch to next tab
		NORMAL: 8000,	// tab-to-tab delay
		RELOOP: 8000	// delay between last tab and loop restart
	};
	this.jTabs = null;
	this.curr = 0;
	this.timer = null;
	this.stopped = 0;
	var me = this;

	this.init = function(sel) {
		this.jTabs = $(sel).tabs().find('li a');
		if (!this.jTabs.length) return;
		$('body').bind('mouseover', this.timeNext);
		$(this.jTabs[0]).parents('div.module').bind('mouseover', this.pause).bind('mousedown', this.stop);
		this.timer = setTimeout(this.next, this.DELAYS.START);
	}

	this.pause = function() {
		if (!me.timer) return false;
		clearTimeout(me.timer);
		me.timer = null;
		return false;
	}
	
	this.stop = function() {
		if (me.stopped) return;
		me.stopped = 1;
		me.pause();
		$('body').unbind('mouseover', me.timeNext);
		$(me.jTabs[0]).parents('div.module').unbind('mouseover', me.pause).unbind('click', me.stop);
	}

	this.timeNext = function() {
		if (me.timer || me.stopped) return false;
		var delay = (me.curr==me.jTabs.length-1)? me.DELAYS.RELOOP : me.DELAYS.NORMAL;
		me.timer = setTimeout(me.next, delay);
		return false;
	}

	this.next = function() {
		if (me.stopped) return;
		clearTimeout(me.timer);
		me.timer = null;
		me.curr = (me.curr+1) % me.jTabs.length;
		$(me.jTabs[me.curr]).trigger('click');
		me.timeNext();
	}

	this.init(sel);
}
