/*****

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

Modifications by Aristoteles, last modification 27-10-06

- Interval selectable
- Direction selectable
- No need to insert javascript in <head> section (was required for Firefox) because
	- stylesheet integrated in global stylesheet
- <div> follows size of displayed image

*****/

var d 			= document;
var imgs 		= new Array();
var zInterval 	= 5000;
var zDirection	= 1;
var photoIndex	= 0;
var current		= 0;
var pause		= false;
var slideshow;

function so_init(zInterval) {
	if(!d.getElementById || !d.createElement)return;
	
	imgs = d.getElementById("kunstwerk_slideshow").getElementsByTagName("img");
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	
	document.getElementById("kunstwerk_slideshow").style.width 	= (imgs[current].width + 2) 	+ 'px';
	document.getElementById("kunstwerk_slideshow").style.height = (imgs[current].height + 2) 	+ 'px';
	
	slideshow = setTimeout("so_xfade(1)", zInterval);
}

function so_xfade(zDirection, photoIndex)
{
	cOpacity = imgs[current].xOpacity;
	
	if (photoIndex > 0)
	{
		if (current == (photoIndex - 1))
		{
			slideshow = setTimeout("so_xfade(" + zDirection + ", 0)", zInterval);
			return;
		}
		else
		{
			nIndex = photoIndex - 1;
		}
	}
	else
	{
		if (zDirection == 1)
		{
			nIndex = imgs[current+1]?current+1:0;
		}
		else
		{
			if (current - 1 < 0)
			{
				nIndex = imgs.length - 1;
			}
			else
			{
				nIndex = current - 1;
			}
		}
	}
	
	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	//
	// Initialize <div> dimensions according to child-image -->>
	//
	if (cOpacity == 0.94)
	{
		document.getElementById("kunstwerk_slideshow").style.width 	= (imgs[nIndex].width + 2) 	+ 'px';
		document.getElementById("kunstwerk_slideshow").style.height = (imgs[nIndex].height + 2) + 'px';
		
		//
		// For IE6.. reset height to repair layout when img-height changes
		//
		document.getElementById("container").style.height = '0%';
		document.getElementById("container").style.height = '100%';
		
		photo_current	= "photo_" + (current + 1);
		photo_new		= "photo_" + (nIndex + 1);
		document.getElementById(photo_current).className		= "page";
		document.getElementById(photo_new).className		= "page selected";
	}
	
	clearTimeout(slideshow);
	
	if (cOpacity <= 0)
	{
		imgs[current].style.display = "none";
		current = nIndex;

		if (document.getElementById("slideshow_interval").value != 0)
		{
			slideshow = setTimeout("so_xfade(" + zDirection + ", 0)", zInterval);
		}
		else
		{
			clearTimeout(slideshow);
		}
	}
	else
	{
		slideshow_fade = setTimeout("so_xfade(" + zDirection + ", " + photoIndex + ")", 25);
	}
	
	function setOpacity(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) + ")";
	}
	
}
