var waitForXml = false;

var UrlArray = new Array();		// List of all existend xml-url's
var IdArray = new Array();		// List of all existend id's
var Id;							// Current id

var ImgIndex = 0;				// Current image index
var Delta = 0;

var HttpRequest;

var Opacity;
var RowCount;

/**
 * Saves all xml-url's and id's.
 * @param url
 * @param id
 * @return
 */
function loadSlideShow(url, id)
{
	// Wait 1 sec, if one current xml is running.
	if (waitForXml)
	{
		window.setTimeout("loadSlideShow('" + url + "', '" + id + "')", 1000);
		return;
	}
	
	// Save id and url.
	IdArray[IdArray.length] = id;
	UrlArray[UrlArray.length] = url;
	
	// Load the xml for first picture.
	waitForXml = true;
	loadXML(url, id);
	
	return;
}

/**
 * Changes the picture.
 * @param id
 * @param delta
 * @return
 */
function changeImage(id, delta)
{
	Delta = delta;
	
	Opacity = 1;
	
	// row count in picture description
	RowCount = document.getElementById(id + "_text").innerHTML.split("<br>").length;
	if (RowCount == 1)
		RowCount = document.getElementById(id + "_text").innerHTML.split("<br/>").length;
	if (RowCount == 1)
		RowCount = document.getElementById(id + "_text").innerHTML.split("<br />").length;
	if (RowCount == 1)
		RowCount = document.getElementById(id + "_text").innerHTML.split("<BR>").length;
	if (RowCount == 1)
		RowCount = document.getElementById(id + "_text").innerHTML.split("<BR/>").length;
	if (RowCount == 1)
		RowCount = document.getElementById(id + "_text").innerHTML.split("<BR />").length;
	
	scrollTextOut(id);
	
	return;
}

function scrollTextOut(id)
{
	if (document.getElementById(id + "_text").style.top == "")
		document.getElementById(id + "_text").style.top = "0px";

	var textTop = parseInt(document.getElementById(id + "_text").style.top, 10);
	
	if (textTop <= -RowCount * 50)
	{
		document.getElementById(id + "_text").style.top = (-RowCount * 50) + "px";
		fadeImageOut(id);
		return;
	}
	
	textTop -= 2;
	
	document.getElementById(id + "_text").style.top = textTop.toString() + "px";

	window.setTimeout("scrollTextOut('" + id + "')", 10);
}

function fadeImageOut(id)
{
	if (Opacity <= 0)
	{
		Opacity = 0;
		document.getElementById(id).style.opacity = Opacity;
		document.getElementById(id).style.filter = 'alpha(opacity = ' + (Opacity * 100) + ')';
		
		chgImage(id);
		return;
	}
	
	Opacity = Opacity - 0.1;
	
	document.getElementById(id).style.opacity = Opacity;
	document.getElementById(id).style.filter = 'alpha(opacity = ' + (Opacity * 100) + ')';
	
	window.setTimeout("fadeImageOut('" + id + "')", 33);
}

function chgImage(id)
{
	var index = getIdIndex(id);
	if (index == -1)
		return;
	
	// load xml.
	loadXML(UrlArray[index], id);
	
	window.setTimeout("fadeImageIn('" + id + "')", 1000);
	return;
}

function fadeImageIn(id)
{
	if (Opacity >= 1)
	{
		Opacity = 1;
		document.getElementById(id).style.opacity = Opacity;
		document.getElementById(id).style.filter = 'alpha(opacity = ' + (Opacity * 100) + ')';
		
		scrollTextIn(id);
		return;
	}
	
	Opacity = Opacity + 0.1;
	
	document.getElementById(id).style.opacity = Opacity;
	document.getElementById(id).style.filter = 'alpha(opacity = ' + (Opacity * 100) + ')';
	
	window.setTimeout("fadeImageIn('" + id + "')", 33);
}

function scrollTextIn(id)
{
	var textTop = parseInt(document.getElementById(id + "_text").style.top, 10);
	
	if (textTop >= 0)
	{
		document.getElementById(id + "_text").style.top = "0px";
		return;
	}
	
	textTop += 2;
	
	document.getElementById(id + "_text").style.top = textTop.toString() + "px";

	window.setTimeout("scrollTextIn('" + id + "')", 10);
}

function getIdIndex(id)
{
	for (var i = 0; i < IdArray.length; i++)
		if (IdArray[i] == id)
			return i;

	return -1;
}

/**
 * Loads the xml.
 * @param url
 * @param id
 * @return
 */
function loadXML(url, id)
{
	HttpRequest = false;
	// FF, Safari, ...
	if (window.XMLHttpRequest && navigator.appName.indexOf("Microsoft") == -1)
	{
		HttpRequest = new XMLHttpRequest();
		
		if (HttpRequest.overrideMimeType)
			HttpRequest.overrideMimeType('text/xml');
	}
	// IE
	else if (window.ActiveXObject) 
	{
		try
		{
			HttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try
			{
				HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}
	
	if (!HttpRequest)
		return false;
	
	Id = id;
	HttpRequest.onreadystatechange = loadImage;
	HttpRequest.open('GET', url, true);
	HttpRequest.send(null);
}

/**
 * Loads the picture.
 * @return
 */
function loadImage() 
{
	if (HttpRequest.readyState == 4) 
	{
		if (HttpRequest.status == 200) 
		{
			//alert(HttpRequest.responseText);
			
			var xmlDoc = HttpRequest.responseXML;
			
			// Get total images count
			var imgTotal = 0;
			for (var i = 0; i < xmlDoc.getElementsByTagName("slideshow").item(0).childNodes.length; i++)
			{
				if (xmlDoc.getElementsByTagName("header").item(i) == null)
					break;
				
				imgTotal++;
			}
			
			// Get current img index.
			var imgIndex = 0;
			if (document.getElementById(Id + "_imgIndex"))
				imgIndex = parseInt(document.getElementById(Id + "_imgIndex").value, 10) + Delta;
			if (imgIndex < 0)
				imgIndex = imgTotal - 1;
			else if (imgIndex >= imgTotal)
				imgIndex = 0;
			
			// Set header
			var header = "";
			header += "<input id='" + Id + "_imgIndex' type='hidden' value='" + imgIndex + "' />";
			header += "<span class='head'>" + xmlDoc.getElementsByTagName("header").item(imgIndex).firstChild.data + "</span><br />";
			header += xmlDoc.getElementsByTagName("alt").item(imgIndex).firstChild.data.replace("\\n", "<br />");
			document.getElementById(Id + "_text").innerHTML = header;
			
			// Set image
			var img = "";
			img += "<img src='" + xmlDoc.getElementsByTagName("src").item(imgIndex).firstChild.data + "' ";
			img += "width='640' height='360' ";
			img += "alt='" + xmlDoc.getElementsByTagName("alt").item(imgIndex).firstChild.data + "' />";
			document.getElementById(Id + "_img").innerHTML = img;
			
			waitForXml = false;
		}
	}
}
