var g_timeInMs = 5000;	// Zeit in Millisekunden in denen weitergeschaltet werden soll
var g_divs = null;		// Alle Divs im slide_container
var g_current = 0;		// Aktuell sichtbarer div
var g_interval = null;	// Timeout zum automatischen weiterblenden
var g_fading = false;	// wird gerade übergeblendet?
var g_init = false;

// Slideshow initialisieren
//window.addEventListener?window.addEventListener("load",initSlideshow,false):window.attachEvent("onload",initSlideshow); 

function initSlideshow()
{
	loadDivs();
	//loadIEPngFix();
	handleClickStartStop();
	updateSlideInfo();
	g_init = true;
}

function loadIEPngFix()
{
	if (document.all && document.styleSheets && document.styleSheets[0] &&
	 	document.styleSheets[0].addRule)
	{
		// Feel free to add rules for specific tags only, you just have to call it several times.
	  	document.styleSheets[0].addRule('div#slide_controlpanel img.transparent', "behavior: url('/js/iepngfix.htc')");
	}
}

function loadDivs()
{
	if(!document.getElementById || !document.getElementsByTagName) return;
	
	// Alle divs holen und global speichern
	container = document.getElementById("slide_container");
	if(container)
	{
		g_divs = container.getElementsByTagName("div");
		
		for(i = 0; i < g_divs.length; i++)
		{
			g_divs[i].myOpacity = 0;
			
			// Bilder laden
			(new Image()).src = g_divs[i].getElementsByTagName("img")[0].src;
		}
		
		g_divs[g_current].myOpacity = 0.99;
	}

}

function fadeToDiv(fadeto)
{
	if(fadeto == g_current) return;
	
	g_fading = true;
	
	curOpacity = g_divs[g_current].myOpacity;
	newOpacity = g_divs[fadeto].myOpacity;
	
	curOpacity -= 0.03;
	newOpacity += 0.03;
	
	g_divs[g_current].myOpacity = curOpacity;
	g_divs[fadeto].myOpacity = newOpacity;
	g_divs[fadeto].style.display = "block";
	
	setOpacity(g_divs[g_current]);
	setOpacity(g_divs[fadeto]);
	
	if(newOpacity < 0.99)
	{
		window.setTimeout("fadeToDiv("+fadeto+")", 20);
	}
	else
	{
		g_fading = false;
		g_divs[g_current].style.display = "none";
		g_divs[g_current].myOpacity = 0;
		g_current = fadeto;
		updateSlideInfo();
	}
}

// Setzt die Opazität Browserunabhängig
function setOpacity(obj)
{
	if(obj.myOpacity > 0.99)
	{
		obj.myOpacity = 0.99;
		return;
	}
	
	obj.style.opacity		= obj.myOpacity;
	obj.style.MozOpacity	= obj.myOpacity;
	obj.style.filter		= "alpha(opacity=" + (obj.myOpacity * 100) + ")";
}

function nextDiv()
{

	var next = g_divs[g_current + 1] ? g_current + 1 : 0;
	fadeToDiv(next);
}

function prevDiv()
{
	var prev = g_current -1;
	if(prev < 0)
	{
		prev = g_divs.length -1;
	}
	fadeToDiv(prev);
}

function restartInterval()
{
	stopInterval();
	g_interval = window.setInterval("handleInterval()", g_timeInMs);
}

function stopInterval()
{
	if(g_interval)
	{
		window.clearInterval(g_interval);
		g_interval = null;
	}
}

function handleInterval()
{
	nextDiv();
}

function handleClickStartStop()
{

	//var button = document.getElementById("startStopButton");
	if(g_interval)
	{
		stopInterval();
		//button.setAttribute("src", "/img/slideshow/button_play.gif");
		//button.setAttribute("alt", "play");
	}
	else
	{
		restartInterval();
		//button.setAttribute("src", "/img/slideshow/button_stop.gif");
		//button.setAttribute("alt", "stop");
	}
}

function handleClickNext()
{
	if(!g_fading)
	{
		if(g_interval)
		{
			restartInterval();
		}
		nextDiv();
	}
}

function handleClickPrev()
{
	if(!g_fading)
	{
		if(g_interval)
		{
			restartInterval();
		}
		prevDiv();
	}
}

function updateSlideInfo()
{
	//var text = (g_current + 1) + " von " + g_divs.length;
	//document.getElementById("slideinfo").innerHTML = text;
	
	links = document.getElementsByName("slidelink");

	for(i=0; i<links.length; i++)
	{
		links[i].className = "";
	}
	
	links[g_current].className = "act";
}
