function fader(id,maxWidth,maxHeight, displayLegende, firstDisplay) {
	this.id = id;
	this.maxWidth = maxWidth;
	this.maxHeight = maxHeight;
	this.imgs = new Array();
	this.imgsWidth = new Array();
	this.imgsHeight = new Array();
	this.current = firstDisplay;
	this.timer = null;
	this.nextToDisplay = -1;
	this.fixIeWidth = -1;
	this.isPaused = false;
	this.isDisplayLegende = displayLegende;
}

fader.prototype.start = function() {
	if(!document.getElementById || !document.createElement)return;
	this.faderObject = document.getElementById(this.id);
	var oThis = this;

	if(this.faderObject) {
		this.imgs = this.faderObject.getElementsByTagName('img');
		for(i=0;i<this.imgs.length;i++) {
			if(i != this.current) {
				this.imgs[i].xOpacity = 0;
				this.imgs[i].style.display = "none";
			}
		}

		this.imgs[this.current].style.display = 'block';
		this.imgs[this.current].xOpacity = .99;
		if(this.isDisplayLegende) {
			this.displayLegende(this.current);
		}
		
		this.imgs[this.current].style.marginLeft = ((this.maxWidth - this.imgsWidth[this.current]) / 2) + "px";
		this.imgs[this.current].style.marginTop = ((this.maxHeight - this.imgsHeight[this.current]) / 2) + "px";

		this.faderObject.width = (parseInt(this.imgsWidth[this.current]) + parseInt(this.imgs[this.current].style.marginLeft) + 2) + "px";
		this.faderObject.style.width = (parseInt(this.imgsWidth[this.current]) + parseInt(this.imgs[this.current].style.marginLeft) + 2) + "px";
		this.faderObject.height = (parseInt(this.imgsHeight[this.current]) + parseInt(this.imgs[this.current].style.marginTop) + 2) + "px";
		this.faderObject.style.height = (parseInt(this.imgsHeight[this.current]) + parseInt(this.imgs[this.current].style.marginTop) + 2) + "px";
		
		document.getElementById("photo" + this.current).xOpacity = 0.5;
		this.setOpacity(document.getElementById("photo" + this.current));
				
		this.timer = setTimeout(function(){oThis.fade();},4500);
	}
}

function test() {
	alert("ok");
}

fader.prototype.displayLegende = function(id) {
	for(j=0;j<this.imgs.length;j++) {
		document.getElementById("legende_rotator" + j).style.display = "none";
	}
	document.getElementById("legende_rotator" + id).style.display = "block";
}

fader.prototype.fade = function() {
	if(!this.isPaused) {
		cOpacity = this.imgs[this.current].xOpacity;
		nIndex = this.imgs[this.current+1]?this.current+1:0;
		nOpacity = this.imgs[nIndex].xOpacity;
		oThis = this;

		this.imgs[nIndex].style.marginLeft = ((this.maxWidth - this.imgsWidth[nIndex]) / 2) + "px";
		this.imgs[nIndex].style.marginTop = ((this.maxHeight - this.imgsHeight[nIndex]) / 2) + "px";
		this.faderObject.width = (parseInt(this.imgsWidth[nIndex]) + parseInt(this.imgs[nIndex].style.marginLeft) + 2) + "px";
		this.faderObject.style.width = (parseInt(this.imgsWidth[nIndex]) + parseInt(this.imgs[nIndex].style.marginLeft) + 2) + "px";
		this.faderObject.height = (parseInt(this.imgsHeight[nIndex]) + parseInt(this.imgs[nIndex].style.marginTop) + 4) + "px";
		this.faderObject.style.height = (parseInt(this.imgsHeight[nIndex]) + parseInt(this.imgs[nIndex].style.marginTop) + 4) + "px";
		
		cOpacity-=.02;
		nOpacity+=.02;

		this.imgs[nIndex].style.display = 'block';
		this.imgs[this.current].xOpacity = cOpacity;
		this.imgs[nIndex].xOpacity = nOpacity;

		this.setOpacity(this.imgs[this.current]);
		this.setOpacity(this.imgs[nIndex]);

		if(cOpacity<=0)
		{
			this.imgs[this.current].style.display = 'none';
			document.getElementById("photo" + this.current).xOpacity = 0.99;
			this.setOpacity(document.getElementById("photo" + this.current));
			this.current = nIndex;
			this.fixIeWidth = -1;
			if(this.isDisplayLegende) {
				this.displayLegende(this.current);
			}

			if(!this.isPaused) {
				this.timer = setTimeout(function(){oThis.fade();},4500);
				document.getElementById("photo" + this.current).xOpacity = 0.5;
				this.setOpacity(document.getElementById("photo" + this.current));
			}
		}
		else
		{
			this.timer = setTimeout(function(){oThis.fade();},40);
		}
	}
}

fader.prototype.displayImage = function(idPhoto) {
	if(this.current != idPhoto) {
		oThis = this;
		
		if(!this.isPaused) {
			clearTimeout(this.timer);
		}
		this.imgs[idPhoto].xOpacity = .99;
		this.setOpacity(this.imgs[idPhoto]);
		this.imgs[idPhoto].style.display = 'block';
		this.imgs[idPhoto].style.marginLeft = ((this.maxWidth - this.imgsWidth[idPhoto]) / 2) + "px";
		this.imgs[idPhoto].style.marginTop = ((this.maxHeight - this.imgsHeight[idPhoto]) / 2) + "px";
		
		this.faderObject.width = (parseInt(this.imgsWidth[idPhoto]) + parseInt(this.imgs[idPhoto].style.marginLeft) + 2) + "px";
		this.faderObject.style.width = (parseInt(this.imgsWidth[idPhoto]) + parseInt(this.imgs[idPhoto].style.marginLeft) + 2) + "px";
		this.faderObject.height = (parseInt(this.imgsHeight[idPhoto]) + parseInt(this.imgs[idPhoto].style.marginTop) + 4) + "px";
		this.faderObject.style.height = (parseInt(this.imgsHeight[idPhoto]) + parseInt(this.imgs[idPhoto].style.marginTop) + 4) + "px";
		
		document.getElementById("photo" + this.current).xOpacity = 0.99;
		this.setOpacity(document.getElementById("photo" + this.current));
		
		this.imgs[this.current].xOpacity = 0;
		this.setOpacity(this.imgs[this.current]);
		this.imgs[this.current].style.display = 'none';
		if(idPhoto - this.current != 1 && idPhoto - this.current != (0 - (this.imgs.length-1))) {
			nIndex = this.imgs[this.current+1]?this.current+1:0;
			this.imgs[nIndex].xOpacity = 0;
			this.setOpacity(this.imgs[nIndex]);
			this.imgs[nIndex].style.display = 'none';
		}
		this.current = idPhoto;
		
		document.getElementById("photo" + this.current).xOpacity = 0.5;
		this.setOpacity(document.getElementById("photo" + this.current));
		
		if(this.isDisplayLegende) {
			this.displayLegende(this.current);
		}
		if(!this.isPaused) {
			this.timer = setTimeout(function(){oThis.fade();},4500);
		}
	}
}

fader.prototype.pause = function() {
	this.isPaused = true;
}

fader.prototype.resume = function() {
	this.timer = setTimeout(function(){oThis.fade();},40);
	this.isPaused = false;
}

fader.prototype.setOpacity = function(obj) {
	if(obj.xOpacity>.99)
	{
		obj.xOpacity = .99;
		return;
	}

	obj.style.opacity = obj.xOpacity;
	obj.style.MozOpacity = obj.xOpacity;
	obj.style.filter = 'alpha(opacity=' + (obj.xOpacity*100) + ')';
}
