
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 fadetime = 5000, timer, backward=false, running = false;

function so_init()
{
	if(!d.getElementById || !d.createElement)return;

	imgs = d.getElementById('content_bild').getElementsByTagName('img');
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = 'block';
	imgs[0].xOpacity = .99;
	
	preload_image = new Image(); 
    preload_image.src="assets/templates/zeno3/button_play.png"; 
	
	if(imgs.length > 1)
		timer = setTimeout(so_xfade,fadetime);

}

function so_xfade()
{
	if(!pause){
		running = true;
		cOpacity = imgs[current].xOpacity;
		if(backward){
			nIndex = current-1;
			if(nIndex < 0)
			 	nIndex = imgs.length-1;
		}
		else
			nIndex = imgs[current+1]?current+1:0;
		nOpacity = imgs[nIndex].xOpacity;
		
		cOpacity-=.05;
		nOpacity+=.05;
	
		imgs[nIndex].style.display = 'block';
		if(backward){
			if(nIndex == imgs.length-1){
				imgs[nIndex].style.top = '-400px';
				imgs[0].style.top = '0px';
			}
			else if(current == 1){
				imgs[current].style.top = '-400px';
				imgs[0].style.top = '0px';
			}
			else{
				imgs[nIndex].style.top = '0px';
				imgs[current].style.top = '-400px';
			}
		}
		else{
			if(nIndex == 0){
				imgs[nIndex].style.top = '0px';
				imgs[imgs.length-1].style.top = '-400px';
			}
			else{
				imgs[nIndex].style.top = '-400px';
			}
		}
		imgs[current].xOpacity = cOpacity;
		imgs[nIndex].xOpacity = nOpacity;
	
		setOpacity(imgs[current]);
		setOpacity(imgs[nIndex]);
	
		if(cOpacity<=0)
		{
			imgs[nIndex].style.top = '0px';
			imgs[current].style.display = 'none';
			backward = false;
			running = false;
			current = nIndex;
			timer = setTimeout(so_xfade,fadetime);
		}
		else
		{
			timer = setTimeout(so_xfade,50);
		}
	}
	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) + ')';
	}
		
}

function so_pause(){
	button = d.getElementById('pause');
	if(pause){
		pause = false;
		button.style.background = 'url(assets/templates/zeno3/button_stop.png)';
		if(imgs.length >1)
			setTimeout(so_xfade,50);
	}
	else{
		button.style.background = 'url(assets/templates/zeno3/button_play.png)';
		clearTimeout(timer);
		pause = true;
	}
}

function so_forward(){
	if(!running){
		clearTimeout(timer);
		if(imgs.length >1)
			setTimeout(so_xfade,50);
	}
}

function so_backward(){
	if(!running){
		clearTimeout(timer);
		backward = true;
		if(imgs.length >1)
			setTimeout(so_xfade,50);
	}
}