﻿// ~/Scripts/_WebService/Sponsors.js
// This file will perform all functions used to display the Sponsors in the dialog box
// on the main page of the website.

// Declare the common variables to use throughout the sponsor ads
var banner_list = new RotatingAds("banner_list", "imgBannerAd", "aBannerAd", 7000, 0);
var side_list = new RotatingAds("side_list", "imgSponsor", "aSponsor", 5000, 1);
var memoriam_list = new RotatingAds("memoriam_list", "imgMemoriam", "aMemoriam", 5000, 1);

//===========================================================================
//===========================================================================
// This method accepts a result set and populates the control specified
// within the sControl variable.  sControl is set via the InitRequest method
function PopulateSponsors(sXML, type) {
    var xmlObject = StringtoXML(sXML);
    var _XML = xmlObject.getElementsByTagName("Table");
    var list;
    
    // Make sure that there are rows to add
    if (_XML.length > 0) {
        // Set the list
        if (type == _AdType.BannerAd){
            list = banner_list;
        }    
        else {
            list = side_list;
        }
        
        // Iterate through the table and add HTML Rows & contents into it.
        for (var i = 0; i < _XML.length; i++) {
            // Add the image to the array
            var fileName = getNodeText(_XML[i], "FileName");
            var sponsorName = getNodeText(_XML[i], "Description");
            var sponsorWebsite = getNodeText(_XML[i], "Website");
            list.createImageItem("../Images/Sponsors/".concat(fileName), sponsorName, sponsorWebsite);
        }
        
        // Start the images
        list.rotateImage();
    }
}
//===========================================================================
//===========================================================================
// This method accepts a result set and populates the control specified
// within the sControl variable.  sControl is set via the InitRequest method
function PopulateMemoriams(sXML) {
    var xmlObject = StringtoXML(sXML);
    var _XML = xmlObject.getElementsByTagName("Table");

    // Make sure that there are rows to add
    if (_XML.length > 0) {
        // Iterate through the table and add HTML Rows & contents into it.
        for (var i = 0; i < _XML.length; i++) {
            // Add the image to the array
            var fileName = getNodeText(_XML[i], "FileName");
            var MemoriamName = getNodeText(_XML[i], "Name");
            var MemoriamPassing = GetSQLDate(getNodeText(_XML[i], "DateOfBirth")) + ' - ' + GetSQLDate(getNodeText(_XML[i], "DateOfPassing"));
            var MemoriamLink = "javascript:LoadIFrame('./Forms/Public/Memoriam.aspx?ID=" + getNodeText(_XML[i], "Memoriam_ID") + "')";
            memoriam_list.createImageItem("../Images/Memoriams/".concat(fileName), MemoriamName + '|' + MemoriamPassing, MemoriamLink);
        }

        // Start the images
        memoriam_list.rotateImage(true);
    }
}
//===========================================================================
//===========================================================================
// This is the object that will be used to rotate through the graphics
function RotatingAds(arrayName, imageName, anchorName, interval, random) {
    this.array = arrayName;
    this.image_list = new Array();
    this.image_index = -1;
    this.img = imageName;
    this.anchor = anchorName;
    this.interval = interval;
    this.random_display = random;
    
    //===========================================================================
    //===========================================================================
    // This method is used to create a new image file upon loading
    // These image files are kept within an array and are used when rotating
    // through the list of sponsors.
    this.createImageItem = function(image_location, image_title, image_website) {
        // Create the image object
        var image_item = new Image();
        var jj = this.image_list.length;
        
        // Set the image properties
        image_item.src = image_location;
        image_item.title = image_title;
        image_item.longDesc = image_website;
        
        // Add it to the list
        this.image_list[jj++] = image_item;
    }    
    //===========================================================================
    //===========================================================================
    // This method is used to Rotate throughout all of the images in the list.
    // It will recursively call on itself.  It accepts two parameters.
    // 1 - What is the name of the image container that the sponsor ad displays in
    // 2 - What is the name of the link which surrounds the sponsors ad
    this.rotateImage = function(isMemoriam) {
        // Check the parameter
        if (isMemoriam == undefined)
            isMemoriam = false;

        // Find the next image
        var new_image = this.getNextImage();

        // Update the properties of the current image file
        document[this.img].style.display = "inline";
        document[this.img].src = new_image.src;
        document[this.img].alt = new_image.title;
        document[this.img].title = new_image.title;

        if (isMemoriam == true) {
            setInnerText(document.getElementById('lblMemoriam'), new_image.title.substr(0, new_image.title.indexOf('|'))) + 'a';
            setInnerText(document.getElementById('lblMemoriamDate'), new_image.title.substr(new_image.title.indexOf('|') + 1)) + 'b';
            document.getElementById(this.anchor).href = new_image.longDesc;
        }
        else {
            // Update the link to now reference the correct website.
            // Have it open in a new window.
            if (new_image.longDesc.length <= 10) {
                document.getElementById(this.anchor).href = 'javascript:void(0);';
            }
            else {
                document.getElementById(this.anchor).href = "javascript:OpenWindow('" + new_image.longDesc + "');";
            }
        }

        // If more than one image, recursively call itself
        if (this.image_list.length > 1) {
            // Recursively call itself.  Set the timeout based on the # of seconds
            // specified in the interval at the top.
            var recur_call = this.array + ".rotateImage(" + isMemoriam.toString() + ")";
            setTimeout(recur_call, this.interval);
        }
    }
    //===========================================================================
    //===========================================================================
    // This method is used to locate the next image to display.  Will either
    // grab the next image in sequence or find a random image
    this.getNextImage = function() {
        if (this.random_display) {
            // Randomly choose an image
            this.image_index = this.FindRandomImage(0, this.image_list.length - 1);
        }
        else {
            // Select the next image in sequence
            this.image_index = (this.image_index + 1) % this.image_list.length;
        }
        
        // Return the image file itself
        return this.image_list[this.image_index];
    }
    //===========================================================================
    //===========================================================================
    // This method is used to generate a random number.  This is only used
    // when the random option is turned on (random_display = 1)
    this.FindRandomImage = function(LowValue, HighValue) {
        var range = HighValue - LowValue + 1;
        return Math.floor(Math.random() * range) + LowValue;
    }
}
