var slideArray = [];
var activeColor = "#ffaa16";
var inactiveColor = "#444444";
var activeButton;
var activeSlide;
var nextSlide;
var clicked;
var fadeSpeed = 200;
var slideSpeed = 4000;
var arrayLength;

function ssButton (divID, ssImage, ssCaption)
{
	this.divID = divID;
	this.ssImage = ssImage;
	this.ssCaption = ssCaption;
}

function defineSlideshow(xmlFile)
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	}
	else
	{
		// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.open("GET", xmlFile, false);
	xmlhttp.send();
	xmlDoc = xmlhttp.responseXML;
	
	var xmlItem = xmlDoc.getElementsByTagName("item");
	arrayLength = xmlItem.length;
	
	for (i = 0; i < arrayLength; i = i + 1)
	{
		var xmlDivID = xmlItem[i].getElementsByTagName("divID")[0].childNodes[0].nodeValue;
		var xmlImage = xmlItem[i].getElementsByTagName("image")[0].childNodes[0].nodeValue;
		var xmlCaption = xmlItem[i].getElementsByTagName("caption")[0].childNodes[0].nodeValue;
		slideArray[i] = new ssButton(xmlDivID, xmlImage, xmlCaption);
		var assigner = document.getElementById(xmlDivID);
		assigner.onmouseover = function () {colorButtonActive(this.id);};
		assigner.onmousedown = function () {clickChangeSlide(this.id);};
		assigner.onmouseout = function () {colorButtonInactive(this.id);};
	}
	var elN = document.getElementById("ssnextpicture");
	var el2N = document.getElementById("ssnexttext");
	activeSlide = 0;
	nextSlide = 1;
	elN.style.backgroundImage = "url(" + slideArray[0].ssImage + ")";
	el2N.style.opacity = 0;
	el2N.style.filter = "alpha(opacity=0)";
	el2N.innerHTML = slideArray[0].ssCaption;
	clicked = false;
	setTimeout("if (!clicked) {changeSlide(slideArray[0].divID);}", 5000);
	setTimeout("autoNextSlide()", 5000+slideSpeed);
}

function autoNextSlide()
{
	if (!clicked)
	{
		activeSlide = activeSlide + 1;
		nextSlide = nextSlide + 1;
		if (activeSlide == arrayLength)
		{
			activeSlide = 0;
		}
		if (nextSlide == arrayLength)
		{
			nextSlide = 0;
		}
		changeSlide(slideArray[activeSlide].divID);
		setTimeout("autoNextSlide()", slideSpeed);
	}
}

function colorButtonActive(divID)
{
	var el = document.getElementById(divID);
	el.style.backgroundColor = activeColor;
}

function colorButtonInactive(divID)
{
	if (divID != activeButton)
	{
		var el = document.getElementById(divID);
		el.style.backgroundColor = inactiveColor;
	}
}

function clickChangeSlide(divID)
{
	clicked = true;
	changeSlide(divID);
}

function changeSlide(divID)
{
	var theImage;
	var theCaption;
	for (i = 0; i < arrayLength; i = i + 1)
	{
		if (slideArray[i].divID == divID)
		{
			theImage = "url(" + slideArray[i].ssImage + ")";
			theCaption = slideArray[i].ssCaption;
			break;
		}
	}
	activeButton = divID;
	for (i = 0; i < arrayLength; i = i + 1)
	{
		colorButtonInactive(slideArray[i].divID);
	}
	colorButtonActive(divID);
	fade(theImage, theCaption);		
}

function fade(newImage, newCaption)
{
	if (clicked)
	{
		var elN = document.getElementById("ssnextpicture");
		var el2N = document.getElementById("ssnexttext");
		elN.style.backgroundImage = newImage;
		el2N.innerHTML = newCaption;
	}
	for (i = 90; i >= 0; i = i - 10)
	{
		setTimeout("fadeEngine(" + i + ")",(100-i)/100.0*fadeSpeed);
	}
	setTimeout("resetFade()",fadeSpeed+100);
}

function fadeEngine(level)
{
	var el = document.getElementById("sspicture");
	var el2 = document.getElementById("sstext");
	var el2N = document.getElementById("ssnexttext");
	el.style.opacity = (level/100.0);
	el.style.filter = "alpha(opacity=" + level + ")";
	el2.style.opacity = (level/100.0);
	el2.style.filter = "alpha(opacity=" + level + ")";
	el2N.style.opacity = ((100.0-level)/100.0);
	el2N.style.filter = "alpha(opacity=" + 100.0 - level + ")";
}

function resetFade()
{
	var el = document.getElementById("sspicture");
	var el2 = document.getElementById("sstext");
	var elN = document.getElementById("ssnextpicture");
	var el2N = document.getElementById("ssnexttext");
	el.style.backgroundImage = elN.style.backgroundImage;
	el2.innerHTML = el2N.innerHTML;
	el.style.opacity = 1;
	el.style.filter = "alpha(opacity=100)";
	el2.style.opacity = 1;
	el2.style.filter = "alpha(opacity=100)";
	el2N.style.opacity = 0;
	el2N.style.filter = "alpha(opacity=0)";
	if (!clicked)
	{
		setTimeout("resetFade2()", 10);
		el2N.innerHTML = slideArray[nextSlide].ssCaption;
	}
}

function resetFade2()
{
	var elN = document.getElementById("ssnextpicture");
	elN.style.backgroundImage = "url(" + slideArray[nextSlide].ssImage + ")";
}
