﻿
//create an array to store image paths
var imageArray = new Array();

//create a variable to control the timer
var tmrRotator;

//this function sets the global array to the passed in image list (from the server -- called onload)
function InitializeRotator(FileList) {
    imageArray = FileList.split(",");
    ShowImages();
}

//this function gets called every x seconds to swap client logos
function ShowImages() {
    //make a copy of the original array because every time this function is called we need the original values
    var images = imageArray.slice();
    var totalImages = images.length;
    var tagIndex = 0;
    
    for (tagIndex = 1; tagIndex <= 8; tagIndex++) {
        var imgClient = document.getElementById("imgClient" + tagIndex);
        var imageIndex = GetRandomNumber(totalImages - 1);

        imgClient.src = images[imageIndex];
        images.splice(imageIndex, 1);
        totalImages = totalImages - 1;
    }

    tmrRotator = setTimeout("ShowImages()", 10000);
}

//this function gets a random number between 0 and the number passed in
function GetRandomNumber(MaxNumber) {
    return (Math.floor(Math.random() * (MaxNumber + 1)));
}


//The following code will be used to handle the random image display on the services/portfolio pages. Each page will
//have a set of images from which to choose and each time the page loads, x (TotalDisplay) of those images will display.
//Passing 0 for TotalDisplay will display all.
function DisplayPortfolio(TotalDisplay) {
    var TotalAvailable = $("img[rel]").length;

    //passing 0 is equal to all
    if (TotalDisplay == 0) {
        TotalDisplay = TotalAvailable;
    }

    //build the available array
    var AvailableArray = [];
    var ImageIndex = 0;
    for (ImageIndex = 0; ImageIndex < TotalAvailable; ImageIndex++) {
        AvailableArray[ImageIndex] = "img" + (ImageIndex + 1);
    }

    var images = AvailableArray.slice();    //copy of array for processing
    var DisplayedIndex = 0;
    
    for (DisplayedIndex = 1; DisplayedIndex <= TotalDisplay; DisplayedIndex++) {
        var imageIndex = GetRandomNumber(TotalAvailable - 1);
        var imgPortfolio = document.getElementById(images[imageIndex]);

        imgPortfolio.style.display = "inline";

        images.splice(imageIndex, 1);
        TotalAvailable = TotalAvailable - 1;
    }
}
