var xhr = false;

function getNewFile(thefile) {	// This is to prevent the browser from loading the file itself.
	makeRequest(thefile);
	return false;
}

function makeRequest(url) {
	try {
		xhr = new XMLHttpRequest();
	}
	catch (e1) {
		try {
			xhr = new ActiveXObject ("Msxml2.XMLHTTP");
		}
		catch (e2) {
			try {
				xhr = new ActiveXObject ("Microsoft.XMLHTTP");
			}
			catch (e3) { }
		}
	}
	
	if (xhr) {
		xhr.onreadystatechange = showContents;
		xhr.open("GET", url, true);
		xhr.send(null);
	} else {
		document.getElementById("updateArea").innerHTML = "Sorry";
	}
}

function showContents() {
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			var outMsg = xhr.responseText;
			sortImages(outMsg);
		} else {
			var outMsg = "Error";
		}
	}
}

function sortImages(outMsg) {
	var theHTML = "";	// This is where the final HTML will be stored.
	images = outMsg.split("\n");	// Split the text file at the newlines.
	for (var image in images) {	// Loop through each line of the text file.
		if (images[image] != "") {	// Check there is something in the line.
			bit = images[image].split("%%");// Split the current line.
			
			theHTML = theHTML+"<div>";
			if (bit[2]) {
				theHTML = theHTML+"<a href=\""+bit[2]+"\">";
			}
			theHTML = theHTML+"<img src=\"../../../../homeimages/images/"+bit[0]+"\" alt=\""+bit[1]+"\" />";
			if (bit[2]) {
				theHTML = theHTML+"</a>";
			}
			theHTML = theHTML+"</div>";
		}
	}
	document.getElementById("slideshow").innerHTML = theHTML;
	$('#slideshow').cycle();
}