function init_slideshow(){
	var divArray = document.getElementsByTagName("div");
	var i =0;
	
	for(i=0;i<divArray.length;i++){
		if(divArray[i].className=="teaser") new setSlideshow(divArray[i].id);	
	}
}

function setSlideshow(id){
	//init
	this.box 			= document.getElementById(id);
	this.buttonBox = document.getElementById(id+"_buttons");
	this.currentSlide 	= 0;
	this.settings 		= this.box.getAttribute("title");
	this.box.removeAttribute("title");
	this.settings 		= this.settings.split(",");
	
	//get settings
	this.delay 	= this.settings[0];
	this.pauze 	= this.settings[1];
	this.rand 	= this.settings[2];
	this.millisec = 1000;
	this.setByButton = 1;
	var self = this;
	
	this.imgArray = 	this.box.getElementsByTagName("div");
	this.butArray = 	this.buttonBox.getElementsByTagName("div");
	var i = 0;
	
	this.stopslider = function(){
			clearTimeout(this.timer);
	}
	
	//init teaser buttons
	this.buttonArraySel = new Array();
	this.buttonArrayDefault = new Array();
	var d = 0;
	var s = 0;
	
	for(i=0;i<this.imgArray.length;i++){
		//zet pauze mouseover en mouseout
		this.object = document.getElementById(this.imgArray[i].id);
		if(this.pauze==1){
			this.object.onmouseover 		= function(){self.stopslider()}
			this.object.onmouseout			= function(){self.timer = setTimeout(function(){self.startSlideshow();}, self.delay);}
		}
		
		//set button switcher
		this.buttonObject = document.getElementById(this.butArray[i].id);
		if(this.setByButton==1){
			this.buttonObject.onmouseover = function(){self.stopslider();self.setSlide(this.id)}
			this.buttonObject.onmouseout = function(){self.stopslider(); self.timer = setTimeout(function(){self.startSlideshow();}, self.delay);}
		}
		
		//haal default en selected array van de buttons op
		var buttonBox = this.buttonObject.getElementsByTagName("span");
		var j=0;
		for(j=0;j<buttonBox.length;j++)
		{
			if(buttonBox[j].className == "default")
			{
				this.buttonArrayDefault[d] = buttonBox[j];
				d++;
			}
			if(buttonBox[j].className == "sel")
			{
				this.buttonArraySel[s] = buttonBox[j];
				s++;
			}
		}
		
		
		//zet div op onzichtbaar wanneer dit niet de eerste slide bevat
		this.opacity = (i==0) ? 100 : 0;
		this.object.setAttribute("opac",this.opacity);
		this.object = this.object.style; 
		this.object.opacity = (this.opacity / 100); 
		this.object.MozOpacity = (this.opacity / 100); 
		this.object.KhtmlOpacity = (this.opacity / 100); 
		this.object.filter = "alpha(opacity=" + this.opacity + ")";
		if(i==0) this.object.display = "block";
		if(i!==0) this.object.display = "none";
		
	}
	
	document.getElementById('loadingTeaser').style.display= "none";
	
	this.slide = function()
	{
		var i = 0;
		var self = this;
		//alert(this.buttonArrayDefault.length+"-"+this.buttonArraySel.length+"-"+this.imgArray.length)
		for(i=0;i<this.imgArray.length;i++)
		{
			var currentOpac = parseInt(this.imgArray[i].getAttribute("opac"));
			var currentOpacDefault = 100-currentOpac;
			var timerPerc = this.millisec/100;
			
			
			if(i==this.nextSlide)
			{
				var perc = 100-currentOpac;
				var interval = perc*timerPerc;
				opacity(this.buttonArrayDefault[i].id,currentOpacDefault,0,interval,self);
				opacity(this.buttonArraySel[i].id,currentOpac,100,interval,self);
				opacity(this.imgArray[i].id,currentOpac,100,interval,self);
			}
			else
			{
				var perc = currentOpac;
				var interval = perc*timerPerc;
				opacity(this.buttonArrayDefault[i].id,currentOpacDefault,100,interval,self);
				opacity(this.buttonArraySel[i].id,currentOpac,0,interval,self);
				opacity(this.imgArray[i].id,currentOpac,0,interval,self);
			}
		}		
		this.currentSlide = this.nextSlide;
	}
	
	this.setSlide = function(slide)
	{
			var self = this;
			var slide = slide.split("_");
			this.nextSlide = slide[2]-1;
			if(this.currentSlide!=this.nextSlide) this.slide();
	}
	
	this.startSlideshow = function() 
	{
		var i = 0;
		this.nextSlide = this.currentSlide+1;
		this.nextSlide = (this.currentSlide>=this.imgArray.length-1) ? 0 : this.nextSlide;
		this.nextSlide = (this.rand==1) ? Math.floor(Math.random() * (this.imgArray.length + 0)) + 0 : this.nextSlide;
		while(this.nextSlide==this.currentSlide && this.rand==1)
		{
			this.nextSlide = (this.rand==1) ? Math.floor(Math.random() * (this.imgArray.length + 0)) + 0 : this.nextSlide;
		}
		
		this.slide();
		
		var self = this;
		delay = parseInt(this.delay)+parseInt(this.millisec);
		this.timer = setTimeout(function(){self.startSlideshow();}, delay);
	}
	
	this.timer = setTimeout(function(){self.startSlideshow();}, this.delay);
}

function opacity(id, opacStart, opacEnd, millisec, box) {
	if(!this.OpacTimer) this.OpacTimer = new Array();
	if(!box.fadeTimer) box.fadeTimer = new Array();
	if(!box.fadeTimer[id]) box.fadeTimer[id] = new Array();
	var i = 0;
	
	//evt vorige timeouts verwijderen
	for(i=0;i<box.fadeTimer[id].length;i++)
	{
			clearTimeout(box.fadeTimer[id][i]);
	}
	
	//speed for each frame 
	var speed = Math.round(millisec / 100); 
	var timer = 0; 
	var j = 0;
	var object = document.getElementById(id).style;
	//determine the direction for the blending, if start and end are the same nothing happens 
	if(opacStart > opacEnd) {
		object.zIndex = 0;
		for(i = opacStart; i >= opacEnd; i--) {
			box.fadeTimer[id][j] = setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++;
			j++;
		} 
	} else if(opacStart < opacEnd) {
		object.zIndex = 1;
		for(i = opacStart; i <= opacEnd; i++) 
			{
			box.fadeTimer[id][j] = setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++; 
			j++;
		} 
	} 
} 
			
function changeOpac(opacity, id) {
	var div = document.getElementById(id)
	div.setAttribute("opac",opacity);
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100); 
	object.MozOpacity = (opacity / 100); 
	object.KhtmlOpacity = (opacity / 100); 
	object.filter = "alpha(opacity=" + opacity + ")";
	
	if(opacity>0) object.display = "block";
	if(opacity==0) object.display = "none";
}

