var sliderId = 'slider';
var slideTime = 400;
var idleTime = 14000;

var theSlider = null;
var activeTab = 0;
var idleTimer = null;

function slider (i, auto) {
	if (!auto) {
		window.clearTimeout(idleTimer);
	}

	this.el = document.getElementById('slider');

	if (!document.getElementById('tab' + i)) {
		i = 0;
	}
	document.getElementById('tab' + activeTab).className = '';
	activeTab = i;
	document.getElementById('tab' + activeTab).className = 'active';

	var child = this.el.firstChild;
	while(child.nodeType != 1) {
		child = child.nextSibling;
	}

	this.start_x = this.el.offsetLeft;
	this.end_x = -i * child.offsetWidth;

	this.start_t = new Date().valueOf();
	this.end_t = this.start_t + slideTime;

	if (this.timer) {
		window.clearTimeout(this.timer);
		this.timer = null;
	}

	this.slide = function() {
		var t = new Date().valueOf();
		if (t >= this.end_t) {
			t = this.end_t;
		}

		var percent = (t - this.start_t) / (this.end_t - this.start_t);
		var frac = Math.sin(percent * Math.PI/2);
		if (frac < .5) {
			frac = 2 * (frac * frac) ;
		} else {
			frac = 1 - frac;
			frac = 2 * (frac * frac) ;
			frac = 1 - frac;
		}

		var x = Math.round(this.start_x+ (this.end_x - this.start_x)*frac);
		this.el.style.marginLeft = x + 'px';

		if (t < this.end_t) {
			var strExec = 'theSlider.slide()';
			this.timer = window.setTimeout(strExec,5);
		} else {
			this.timer = null;
		}
	}
	this.slide();
}

function idle(i) {
	theSlider = new slider(i, true);
	idleTimer = window.setTimeout('idle(' + (i+1) + ')', idleTime);
}

window.onload = function() {
	idle(0);	
}
