if (window.addEventListener) {
	window.addEventListener("load", loadFlipper, false);
} else {
	window.attachEvent("onload", loadFlipper);
}
function loadFlipper() {
	new Flipper($i("features")).start();
}
function $i(s, p) {
	return (p || document).getElementById(s);
}
function $t(t, p) {
	return (p || document).getElementsByTagName(t || "*");
}
Function.bind = function(f, o) {
	for (var i = 0, l = Function.bind.cache.length; i < l; i++) {
		var fn = Function.bind.cache[i];
		if (fn.fnct == f && fn.obj == o) {
			return fn.method;
		}
	}
	
	var fn = {
		fnct: f,
		obj: o,
		method: function() {
			return f.apply(o, arguments);
		}
	};
	
	Function.bind.cache.push(fn);
	return fn.method;
};
Function.bind.cache = [];

function Flipper(parent, duration, pause, easing) {
	this.parent = parent;
	this.items = [];
	var children = $t("*", parent);
	for (var i = 0, child; child = children[i]; i++) {
		if (child.parentNode == parent) {
			this.items.push(child);
		}
	}
	this.item = this.items[0];
	
	this.duration = (duration || 1);
	this.pause = (pause || 8);
	this.fps = 200;
	this.frames = this.fps * this.duration;
	this.current = 0;
	this.easing = (easing || Easing.easeIn);
	this.random = false;
}
Flipper.prototype = {
	start: function() {
		if (this.thread) {
			clearInterval(this.thread);
		}
		this.thread = setInterval(Function.bind(this.flip, this), (this.pause + this.duration) * 1000);
	},
	
	stop: function() {
		if (this.thread) {
			clearInterval(this.thread);
		}
	},
	
	flip: function() {
		var old = this.item;
		this.getNext();
		this.setOpacity(0);
		this.setIndex(old);
		this.current = 0;
		this.started = new Date();
		this.action();
	},
	
	action: function() {
		if (this.current < this.frames) {
			this.check();
			this.setOpacity(this.easing(this.current, 0, 100, this.frames));
			setTimeout(Function.bind(this.action, this), 1);
		}
	},
	
	check: function() {
		var time = (new Date() - this.started);
		var calcFrame = Math.round((time * this.frames) / (this.duration * 1000)) + 1;
		this.current = (calcFrame <= this.frames ? calcFrame : this.frames);
	},
	
	getNext: function() {
		var old = this.item;
		if (this.random) {
			while (this.item == old) {
				this.item = this.items[Math.floor(Math.random() * this.items.length)];
			}
		} else {
			for (var i = 0, l = this.items.length - 1; i <= l; i++) {
				var item = this.items[i];
				if (item == this.item) {
					this.item = this.items[i < l ? i + 1 : 0];
					break;
				}
			}
			this.item.style.display = "";
			this.item.style.visibility = "visible";
		}
	},
	
	setOpacity: function(opac) {
		opac = Math.round(opac);
		this.item.style.opacity = opac / 100;
		this.item.style.filter = "alpha(opacity=" + opac + ")";
	},
	
	setIndex: function(old) {
		for (var i = 0, item; item = this.items[i]; i++) {
			item.style.zIndex = (item == this.item ? 10 : (item == old ? 5 : 0));
		}
	}
};
var Easing = {
	linear: function(t, b, c, d) { return c*t/d + b; },
	easeIn: function(t, b, c, d) { return c*(t/=d)*t + b; },
	easeOut: function(t, b, c, d) { return -c *(t/=d)*(t-2) + b; },
	easeInOut: function(t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	}
/*
	easeIn: function(t, b, c, d) { return c*(t/=d)*t*t + b; },
	easeOut: function(t, b, c, d) { return c*((t=t/d-1)*t*t + 1) + b; },
	easeInOut: function(t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	}
*/
};