﻿//*****************************************************************************
//This JavaScript file contains code to help manage the slide show that appears
//in the user's browser. The content of the slide show will be governed by the
//"captions.xml" file and the images contained in that directory.
//*****************************************************************************

//*****************************************************************************
//Global variable declarations
//*****************************************************************************

//will contain the image names for the slide show
var images = null;
//will contain the caption text for each slide show image
var captionText = null;

//directory path to the slideshow content
var sSlideShowPath = "";

//will handle reading the XML file
var xmlObj = null;
//set slideShowSpeed (milliseconds)
var slideShowSpeed = 5000;
//duration of crossfade (seconds)
var crossFadeDuration = 10;

var timeOut;

//the index of the image in the slide show
var iImageIndex = 0;

//will hold a random number when the page first loads to mix up which slide
//show picture index is shown first
var iRandNum = -1;

//will contain the images for the slide show
var preLoad = null;
//total number images in the slide show (governed by the number of captions
//available in the XML file)
var iNumberOfImages = 0;

//flag variable to help flip flop the picture and caption of each slide show
//from the left side to the right side
var bFlipLeft = true;

//*****************************************************************************
//determine if page holding this JavaScript file is the Home page or not, 
//because the Home page has a different slide show than the rest of the site
var sDocTitle = document.title;
var bIsHomePage = false;
if(sDocTitle == "C&WSG: Home")
{ 
    bIsHomePage = true;
}

//determine if this page is the Admin preview page
var bIsPreviewPage = false;
if(sDocTitle == "Slide Show Preview")
{
    bIsPreviewPage = true;
}

var sCoreImageName = "";
var sXmlFileName = "";

InitializeSlideShow();

//*****************************************************************************
//Function will initialize the slide show
function InitializeSlideShow()
{
   if(bIsHomePage == true)
    {   
        //if this is an Admin preview page, then use a different path
        //because the file sits on the user's local machine
        if(bIsPreviewPage == true)
        {
            sSlideShowPath = "upload\\slideshow\\homepage\\"; 
        }
        else
        {
            sSlideShowPath = "upload/slideshow/homepage/";
        }
        
        sXmlFileName = "captionsHome.xml";
        sCoreImageName = "imageHome";
    }
    else
    {
        //if this is an Admin preview page, then use a different path
        //because the file sits on the user's local machine
        if(bIsPreviewPage == true)
        {
            sSlideShowPath = "upload\\slideshow\\"; 
        }
        else
        {
            sSlideShowPath = "upload/slideshow/";
        }
            
        sXmlFileName = "captions.xml";
        sCoreImageName = "image";
    }

    
    //read the XML file
    loadXMLDoc(sSlideShowPath + sXmlFileName);

}

//*****************************************************************************
//read the XML file
loadXMLDoc(sSlideShowPath + sXmlFileName);

//*****************************************************************************
//This function will load the XML file containing the slide show 
//captions into memory
function loadXMLDoc(sUrl)
{
    //alert("xml url: " + sUrl);
    xmlObj = null;
    
    //open the XML file using XML DOM if this is the Admin Preview page
    if(bIsPreviewPage == true)
    {
        try
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        }
        catch(ex1)
        {
            try //Firefox, Mozilla, Opera, etc.
            {
                xmlDoc = document.implementation.createDocument("","",null);
            }
            catch(ex2) 
            {
                alert("loadXMLDoc error message (ex2): " + ex2.message)
            }
        }
        
        try 
        {
            xmlDoc.async = false;
            xmlDoc.load(sUrl);
            InitializeCaptionsAndImages();
        }
        catch(ex3) 
        {
            alert("loadXMLDoc error message (ex3): " + ex3.message)
        }
    
    }
    else
    {
        if (window.XMLHttpRequest)
        {
            //code for IE7, Firefox, Mozilla, etc.
            xmlObj = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
            //code for IE5, IE6
            xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (xmlObj != null)
        {
            xmlObj.onreadystatechange = InitializeCaptionsAndImages;
            xmlObj.open("GET", sUrl, true);
            xmlObj.send(null);
        }
        else
        {
            alert("Your browser does not support XMLHTTP\n" + 
                  "and will not be able to display the slide show properly.");
        }
    }
}

//*****************************************************************************
//Function will capture the caption text stored in the XML file, and set the 
//names of the images in an array.
function InitializeCaptionsAndImages()
{
    //variable will be a reference to the caption XML nodes in the XML file
    var captionNodes = null;
    
    if(bIsPreviewPage == true)
    {
        captionNodes = xmlDoc.getElementsByTagName("caption");
    }
    else
    {
        if(xmlObj.readyState!=4) return;
        if(xmlObj.status!=200)
        {
            alert("Problem retrieving XML data");
            return;
        }
        
        captionNodes = xmlObj.responseXML.documentElement.getElementsByTagName("caption");
    }

    //alert("captionNodes Nodes length = " + captionNodes.length);
    
    var iActualImageNumberInName = 0;
    
    images = null;
    captionText = null;
    preLoad = null;
    
    images = new Array();
    captionText = new Array();
    preLoad = new Array();
    
    //loop through the captions in the XML file and capture them in a file level variable,
    //set the slide show image names, and store a reference to the images for later user
    for (i = 0; i < captionNodes.length; i++)
    {
        {
            try
            {
                iActualImageNumberInName = i + 1;
                captionText[i] = captionNodes[i].firstChild.nodeValue;
                images[i] = sSlideShowPath + sCoreImageName + iActualImageNumberInName + ".jpg"
                
                preLoad[i] = new Image();
                preLoad[i].src = images[i];
            }
            catch (ex)
            {
                //don't do anything for now
            }
        }
    }
    
   
    //alert("before assign to iNumberOfImages, images length = " + images.length);
    iNumberOfImages = images.length;
    
    //set the slide show index to a random number so that the slide show does not 
    //always start at the same index
    iRandNum = Math.random();
    iRandNum = iRandNum * (iNumberOfImages - 1);
    iRandNum = Math.ceil(iRandNum);
    iImageIndex = iRandNum;
}

//*****************************************************************************     
//Main function for running the slide show
function runSlideShow()
{   
    //exit if there are no images to deal with
    if(iNumberOfImages == 0)
    {
        //set the time for which to run this function again    
        timeOut = setTimeout('runSlideShow()', slideShowSpeed);
        return;
    }
    
    {
        try
        {
            //set the filter for the div tag containing the slide show
            if (document.all){
                document.getElementById("divSlideShowMaster").style.filter="blendTrans(duration=5)";
                document.getElementById("divSlideShowMaster").style.filter="blendTrans(duration=crossFadeDuration)";
                document.getElementById("divSlideShowMaster").filters.blendTrans.Apply();
            }
           
            document.images.imgForSlideShow.src = preLoad[iImageIndex].src;
            
            //get references to the HTML objects that need to manipulated for the slide show
            var captionTextObj = document.getElementById("divSlideShowText");
            captionTextObj.innerHTML = captionText[iImageIndex];
            captionTextObj.style.fontFamily = "Arial, Verdana, Sans-Serif";
            //captionTextObj.style.color = "Blue"; 
            captionTextObj.style.fontWeight = "bold";

            var imgObj = document.getElementById("imgForSlideShow");
            
            //adjust specs depending on whether or not this slide show is for the Home page
            if(bIsHomePage == true)
            {
                imgObj.height = 300;
                captionTextObj.style.fontSize = 15;
            }
            else
            {
                imgObj.height = 150;         
            }
            
            var tdSlideShowLeftObj = document.getElementById("tdSlideShowLeft");
            var tdSlideShowRightObj = document.getElementById("tdSlideShowRight");
                   
            //depending on the flip variable flag , swap the location
            //of the picture and caption by alternating them from
            //the left side to the right side of the display area
            if(bFlipLeft == true)
            {
                bFlipLeft = false;
                tdSlideShowLeftObj.removeChild(imgObj);
                tdSlideShowRightObj.removeChild(captionTextObj);
                
                tdSlideShowLeftObj.appendChild(captionTextObj);
                tdSlideShowRightObj.appendChild(imgObj);
            }
            else
            {   
                bFlipLeft = true;
                
                tdSlideShowLeftObj.removeChild(captionTextObj);
                tdSlideShowRightObj.removeChild(imgObj);

                tdSlideShowLeftObj.appendChild(imgObj);
                tdSlideShowRightObj.appendChild(captionTextObj);
            }
               
            if (document.all){
                document.getElementById("divSlideShowMaster").filters.blendTrans.Play();
            }
        }
        catch (ex)
        {
            //don't do anything, let the slide show remain as it was before the error
            
//            alert("ERROR: iImageIndex = " + iImageIndex + 
//                  "\nName: " + ex.name + 
//                  "\nMessage: " + ex.message + 
//                  "\npreLoad.length = " + preLoad.length);
        }
        finally
        {
            //increment the Image index so that the next image in the array will be used the
            //next time this function is called
            iImageIndex = iImageIndex + 1;
            
            //if the end of the array has been reached, then reset the Image index
            if (iImageIndex > iNumberOfImages - 1)
            {
                iImageIndex = 0;
            }
                    
            //set the time for which to run this function again    
            timeOut = setTimeout('runSlideShow()', slideShowSpeed);
        }
    }
}



