/*****

Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact. 

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html


*****/


window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);

var d=document, imgs = new Array(), zInterval = null, current=0, pause=false;
var afbStart;
var try_init = 0;
var sNoImg = '/images/shownoimg.gif';
var sHttpNoImg = location.protocol + '//' + location.host + sNoImg;

function so_init()
{
	if (!d.getElementById || !d.createElement)
	{
	
		// in de timing kennelijk iets 'mis'
		try_init += 1;
	
		if (try_init < 10) setTimeout(so_init,1000);
		
		return;
	}
/*	
	css = d.createElement("link");
	css.setAttribute("href","/css/xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);
*/	

	// Verschil zit 'm in imageContainerHome vers imageContainer
	imgs = d.getElementById("imageContainer").getElementsByTagName("img");
	
	if (!imgs)
	{
		// ? iets mis met de pagina
		try_init += 1;
		setTimeout(so_init,1000);
		
		return;
	}
  
	var randStart= afbStart - 1; //wordt in template 66 gevuld
	
	// for sure..
	if (isNaN(randStart)) randStart = 0;
	if (randStart < 0) randStart = 0; 
	
	for(i=0;i<imgs.length;i++) imgs[i].xOpacity = 0;
	
	// bepaal de image om mee te starten
	// begin bij de afbStart - 1 (deze is vast of random bepaald)
	var iCntNoImg = 0;
	
	while (((imgs[randStart].src==sHttpNoImg) || (imgs[randStart].src==sNoImg)) && (iCntNoImg < 20))
	{
		
		// imgs[x+1]; als deze bestaat... anders de 0-de 	
		iCntNoImg += 1;
		randStart = imgs[randStart+1]?randStart+1:0;
		
	}

	// gevonden
	imgs[randStart].style.display = "block";
	imgs[randStart].xOpacity = .99;	
	current=randStart;
	setTimeout(so_xfade,5000);  // hier stond 000  dus ...
} 

function so_xfade()
{
	// de volgende image ...

	// neem de xOpacity over van het actuele plaatje (current)
	cOpacity = imgs[current].xOpacity;

	// het volgende plaatje: x+1 of 0
	nIndex = imgs[current+1]?current+1:0;
	
	var iCntNoImg = 0;
	
	while (((imgs[nIndex].src==sHttpNoImg) || (imgs[nIndex].src==sNoImg)) && (iCntNoImg < 20))
	{
		// het volgende plaatje zoeken indien shownoimg.gif
		iCntNoImg += 1;
		nIndex = imgs[nIndex+1]?nIndex+1:0;
	}
	
	if (iCntNoImg == 20)
	{
		// gewoon de volgende noimg pakken
		nIndex = imgs[current+1]?current+1:0;
	}
  
	// (next)
	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity-=.02; // afnemen
	nOpacity+=.02; // toenemen
	
	imgs[nIndex].style.display = "block"; // display de nieuwe
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if (cOpacity<=0)
	{
		imgs[current].style.display = "none";
		
		// nieuw plaatje
		current = nIndex;
		setTimeout(so_xfade,1000);
	}
	else
	{
		// huidig plaatje
		setTimeout(so_xfade,50);
	}
		
	function setOpacity(obj)
	{
		if(obj.xOpacity>.99)
		{
			obj.xOpacity = .99;
			return;
		}
		
		// misschien loopt die fout bij -0...
		if(obj.xOpacity<0)
		{
			return;
		}
		
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
	
}

