
var Fader = function (id,start,coef,timer) {
	this.coef = coef;
	this.timer = timer;
	this.id = id;
	this.element = document.getElementById(id);
	this.element.style.position = "absolute";
	for(var j=0;j<this.element.childNodes.length;j++) {
		if(!this.element.childNodes[j].style) {
			this.element.removeChild(this.element.childNodes[j]);
		}
	}
	this.childs = this.element.childNodes;
	for(var i=0;i<this.childs.length;i++) {
		this.childs[i].style.position="absolute";
		this.childs[i].style.left="0px";
		this.childs[i].style.top="0px";
		if((start && i != start) || (!start && i != 0)) {
			setOpacity(this.childs[i],0);
		} else {
			setOpacity(this.childs[i],1);
		}
	}
	if(start) {
		this.current = start;
	} else {
		this.current = 0;
	}

	this.crossFade = function (next) {
		if(typeof next == "undefined") {
			next = this.current+1;
			if(next >= this.childs.length) {
				next = 0;
			}
		}
		var tmp = this.current;
		this.current = next;
		this.fadeOut(tmp);
		this.fadeIn(this.current);
	}
	
	this.fadeOut = function (num) {
		var opacity = parseFloat(this.childs[num].style.opacity);
		if(opacity > 0 && this.current != num) {
			var _this = this;
			opacity = opacity - this.coef;
			setOpacity(this.childs[num],opacity);
			setTimeout(function() {_this.fadeOut(num);},this.timer);
		}
	}

	this.fadeIn = function (num) {
		var opacity = parseFloat(this.childs[num].style.opacity);
		if(opacity < 1 && this.current == num) {
			var _this = this;
			opacity = opacity + this.coef
			setOpacity(this.childs[num],opacity);
			setTimeout(function() {_this.fadeIn(num);},this.timer);
		}
	}
}

function setOpacity(element,opacity) {
	element.style.opacity=opacity;
	element.style.mozOpacity=opacity;
	element.style.filter="alpha(opacity="+opacity*100+")";
}


