//*****************************************************************************
//** Filename: displaySlide.js
//**   Author: Branden McCready
//*****************************************************************************
// * Display "spotlight" and "new arrival" slides, pulling data and settings in from XML files
//*****************************************************************************

String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g, "");
}

function loadXML(xmlFile) {
	try //Internet Explorer
		{
			var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.load(xmlFile);
		}
	catch(e)
	{
	  try //Firefox, Mozilla, Opera, etc.
		{
			var xmlhttp = new window.XMLHttpRequest();
			xmlhttp.open("GET",xmlFile,false);
			xmlhttp.send(null);
			var xmlDoc = xmlhttp.responseXML.documentElement;
		}
	  catch(e)
		{return;}
	}
	return xmlDoc;
}

function displaySlide(xmlFile) {
	document.write(buildSlide(xmlFile));
}

function rotateSlide(xmlFile, container, rotate) {
	if (!rotate) { rotate = 30; }
	rotateSeconds = rotate * 1000;
	document.write('<div id="' + container + '">'+ buildSlide(xmlFile) +'</div>');
	setInterval("document.getElementById(\"" + container + "\").innerHTML = buildSlide(\"" + xmlFile + "\")", rotateSeconds);
}

function buildSlide(xmlFile) {
	var xmlDoc = loadXML(xmlFile);
	if (xmlDoc) {
		var xmlObj = xmlDoc.getElementsByTagName("item");
		var rndSlide = (Math.ceil(xmlObj.length * Math.random()))-1;

		var slideImage = "" + xmlObj[rndSlide].getElementsByTagName("image")[0].firstChild.nodeValue.trim();
		var slideAlt = "" + xmlObj[rndSlide].getElementsByTagName("alt")[0].firstChild.nodeValue.trim();
		var slideUrl = "" + xmlObj[rndSlide].getElementsByTagName("url")[0].firstChild.nodeValue.trim();
		var slideStyle = "" + xmlDoc.getElementsByTagName("style")[0].firstChild.nodeValue.trim();
		var slideImagePre = "" + xmlDoc.getElementsByTagName("imageprefix")[0].firstChild.nodeValue.trim();

		return('<a href="' + slideUrl + '"><img src="' + slideImagePre + slideImage + '" alt="' + slideAlt + '" style="' + slideStyle + '" /></a>');
	}
}