﻿// XML Parsing

//Declare global variables

var xmlHttp;
var xmlDoc;
var showcaseImageLink;
var showcaseHREF;

//Initialize the array that will hold the showcase elements
var showcaseElementsArray = new Array();

var currentShowcaseImage = 0;

//shocaseLink and Image are the two html elements that will be accessed
var showcaseLink = document.getElementById('showcaseLink');
var showcaseImage = document.getElementById('showcaseImage');

//Initialize the rotateInterval to hold the ID of the setInterval call
var rotateInterval;

function loadXMLDoc(url)
{
    xmlHttp = GetXmlHttpObject();

    if (xmlHttp == null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }

    xmlHttp.onreadystatechange = processReqChange;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function GetXmlHttpObject()
{
var xmlHttp = null;
try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
    // Internet Explorer
    try
    {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
        catch (e)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
return xmlHttp;
}

function processReqChange() 
{
    // only if req shows "complete"
    if (xmlHttp.readyState == 4) {
        // only if "OK"
        if (xmlHttp.status == 200) {
            // ...processing statements go here...
            
            
            populateArrayData();
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function populateArrayData() {
    var xmlDoc = xmlHttp.responseXML.documentElement;
    
    showcaseImageLink = xmlDoc.getElementsByTagName('ShowcaseItemImageURL');
    showcaseHREF = xmlDoc.getElementsByTagName('ShowcaseItemHREF');
    showcaseAltText = xmlDoc.getElementsByTagName('ShowcaseItemAltText');

    for (var currentNodeValue = 0; currentNodeValue < showcaseImageLink.length; currentNodeValue++) {
        //showcaseElementsArray is multi-dimensional with an array of the showcase elements
        showcaseElementsArray[currentNodeValue] = new Array(
            showcaseImageLink[currentNodeValue].childNodes[0].nodeValue,
            showcaseHREF[currentNodeValue].childNodes[0].nodeValue,
            showcaseAltText[currentNodeValue].childNodes[0].nodeValue
        );
  
    }

    showcaseElementsArray.sort(sortArrayRandomOrder);
    
    //Call the image rotator to populate the element with a default image
    showcaseImageRotator();
    startShowcaseImageRotation();
}

//The rotator function sets the HTML Element attributes
function showcaseImageRotator() {
    if(currentShowcaseImage >= showcaseElementsArray.length)
    {
        currentShowcaseImage=0;
    }
    showcaseImage.setAttribute('src', showcaseElementsArray[currentShowcaseImage][0]);
    showcaseLink.setAttribute('href', showcaseElementsArray[currentShowcaseImage][1]);
    showcaseImage.setAttribute('alt', showcaseElementsArray[currentShowcaseImage][2]);    

    currentShowcaseImage++;
}

//Start the image rotation
function startShowcaseImageRotation() {
   rotateInterval=setInterval('showcaseImageRotator()', 3000);
}

//Pause the image rotation
function pauseShowcaseImageRotation() {
    clearInterval(rotateInterval);
}

function sortArrayRandomOrder() {
    return (Math.round(Math.random())-0.5);
}

//Call loadXMLDoc and pass the name of an xml file
loadXMLDoc('/showcaseManager/classicASP/ShowcaseAdRotatorXML.asp');