function ATHomeDisplay_go()
{
	this.destinationElement = $(this.destinationElementId);
	this.destinationElement.addClass("ATHomeDisplay");
	
	this.bigImageBack = new Element("img");
	this.bigImageBack.addClass("ATHomeDisplay_bigImage");
	this.bigImageBack.src = "images/athomedisplay/blank.gif";
	this.bigImageBack.fx = new Fx.Tween(this.bigImageBack);
	this.destinationElement.appendChild(this.bigImageBack);
	this.bigImageFront = new Element("img");
	this.bigImageFront.addClass("ATHomeDisplay_bigImage");
	this.bigImageFront.src = "images/athomedisplay/blank.gif";
	this.bigImageFront.fx = new Fx.Tween(this.bigImageFront);
	this.destinationElement.appendChild(this.bigImageFront);
	
	this.itemBackContainer = new Element("div");
	this.itemBackContainer.fx = new Fx.Tween(this.itemBackContainer);
	this.itemBackContainer.addClass("ATHomeDisplay_itemBackContainer");
	this.destinationElement.appendChild(this.itemBackContainer);
	
	this.frame = new Element("div");
	this.frame.addClass("ATHomeDisplay_frame");
	this.frame.instance = this;
	this.frame.addEvent("click",function(){
		this.instance.frameClick();
	});
	this.destinationElement.appendChild(this.frame);
	
	this.itemContainer = new Element("div");
	this.itemContainer.fx = new Fx.Tween(this.itemContainer);
	this.itemContainer.addClass("ATHomeDisplay_itemContainer");
	this.destinationElement.appendChild(this.itemContainer);
	
	for( var i = 0; i < this.item.length; i++ )
	{
		this.item[i][3]  = new Element("div");
		this.item[i][3].addClass("ATHomeDisplay_backItem");
		
		if( this.activeIndex == i )
		{
			this.item[i][3].addClass("active");
		}
		
		var text = new Element("span");
		text.appendChild(document.createTextNode(this.item[i][0]));
		this.item[i][3].appendChild(text);
		this.itemBackContainer.appendChild(this.item[i][3]);
		
		if( i < this.item.length-1 )
		{
			var separator = new Element("div");
			separator.addClass("ATHomeDisplay_itemseparator");
			this.itemBackContainer.appendChild(separator);
		}
		
		var item = new Element("div");
		item.index = i;
		item.addClass("ATHomeDisplay_item");
		item.appendChild(document.createTextNode(this.item[i][0]));
		item.instance = this;
		item.addEvent("click",function(){
			$clear(this.instance.timer);
			this.instance.setActiveIndex(this.index);
			this.instance.timer = this.instance.timeOut.delay(5000,this.instance);
		});
		this.itemContainer.appendChild(item);
	}
	
	// Breedte van container aanpassen
	var backWidth = 0;
	var width = 0;
	for( var i = 0; i < this.itemBackContainer.childNodes.length; i++ )
	{
		backWidth += parseInt(this.itemBackContainer.childNodes[i].offsetWidth);
		width += parseInt(this.itemBackContainer.childNodes[i].offsetWidth) + 2;
	}
	
	if( backWidth < this.destinationElement.getWidth() )
	{
		backWidth = this.destinationElement.getWidth();
	}
	if( width < this.destinationElement.getWidth() )
	{
		width = this.destinationElement.getWidth();
	}
	
	this.itemBackContainer.setStyle("width",backWidth + "px");
	this.itemContainer.setStyle("width",width + "px");
	
	this.setActiveIndex(0);
	this.runTimeOut();
}

function ATHomeDisplay_addItem(title,image,url)
{
	var img = new Image();
	img.src = image;
	this.item[this.item.length] = Array(title,img,url,null,null);
}

function ATHomeDisplay_setActiveIndex(index)
{
	if( this.item[index] == null )
	{
		return;
	}
	this.bigImageFront.src = this.bigImageBack.src;
	this.bigImageFront.setStyle("opacity",1);
	this.bigImageBack.setStyle("opacity",0);
	this.bigImageBack.src = this.item[index][1].src;
	this.bigImageBack.fx.start("opacity",[0,1]);
	
	if( this.activeIndex != -1 )
	{
		this.bigImageFront.fx.start("opacity",[1,0]);
		this.item[this.activeIndex][3].removeClass("active");
	}
	
	this.activeIndex = index;
	this.item[this.activeIndex][3].addClass("active");
	
	var width = 0;
	
	for( var i = 0; i < this.activeIndex; i++ )
	{
		width += this.item[i][3].getWidth()+2;
	}
	
	var max = this.itemBackContainer.getWidth()-this.destinationElement.getWidth();
	
	if( width > max )
	{
		width = max;
	}
	
	
	var newLeft = -width;
	
	this.itemBackContainer.fx.start("left",newLeft + "px");
	this.itemContainer.fx.start("left",newLeft + "px");
}

function ATHomeDisplay_timeOut()
{
	var newIndex = this.activeIndex+1;
	if( newIndex == this.item.length )
	{
		newIndex = 0;
	}
	this.setActiveIndex(newIndex);
	this.runTimeOut();
}

function ATHomeDisplay_runTimeOut()
{
	$clear(this.timer);
	this.timer = this.timeOut.delay(5000,this);
}

function ATHomeDisplay_frameClick()
{
	if( this.item[this.activeIndex][2] != "" )
	{
		location.href = this.item[this.activeIndex][2];
	}
}

function ATHomeDisplay(destinationElementId)
{
	this.destinationElementId = destinationElementId;
	this.destinationElement = null;
	this.item = Array();
	this.bigImageBack = null;
	this.bigImageFront = null;
	this.frame = null;
	this.itemBackContainer = null;
	this.itemContainer = null;
	this.activeIndex = -1;
	this.timer = null;
	
	this.go = ATHomeDisplay_go;
	this.addItem = ATHomeDisplay_addItem;
	this.setActiveIndex = ATHomeDisplay_setActiveIndex;
	this.timeOut = ATHomeDisplay_timeOut;
	this.runTimeOut = ATHomeDisplay_runTimeOut;
	this.frameClick = ATHomeDisplay_frameClick;
}