var IDFSponsorsTicker =
{
    randomSponsors: [],
    nextSponsor: 0,
    stopAnimation: false,
    incrementSponsorCounter: function(IN_counter)
    {
        this.nextSponsor = (IN_counter + 1) >= this.randomSponsors.length ? 0 : IN_counter + 1;
    },
    attachImageRollovers: function(IN_selector)
    {
        var selector = IN_selector ? IN_selector : "#sponsorsTicker > div > img";
        $(selector).mouseover(function() { $(this).attr("src", IDFSponsorsTicker.swapSponsorImage($(this).attr("src"))); });
        $(selector).mouseout(function() { $(this).attr("src", IDFSponsorsTicker.swapSponsorImage($(this).attr("src"))); });
    },
    attachTickerClicks: function(IN_selector)
    {
        var selector = IN_selector ? IN_selector : "#sponsorsTicker > div > img";

        $(selector).click(function() { window.location = '/idf/intel-sponsors/'; });
    },
    writeTickerToPage: function(IN_sourceArray, IN_randomArray)
    {
        var htmlOutput = "";
        var counter = IN_randomArray.length >= 6 ? 6 : IN_randomArray.length;

        for (var i = 0; i < counter; i++)
        {
            htmlOutput += '<div><img src="' + IN_sourceArray[IN_randomArray[i]].grayLogoPath + '" name="" alt=""/></div>';
        }

        $("#sponsorsTicker").html(htmlOutput);
        $("#sponsorsTicker > div").fadeIn();
    },
    buildRandomSponsorsArray: function(IN_sourceArray, IN_randomArray)
    {
        if (IN_randomArray.length < (IN_sourceArray.length / 2)) // use the Math.random() function to build half the array
        {
            var randomNumber = IDFSponsorsTicker.returnRandomNumber(0, (IN_sourceArray.length - 1));

            if ($.inArray(randomNumber, IN_randomArray) == -1) // if the randomNumber isn't already in randomArray
            {
                IN_randomArray.push(randomNumber);
            }

            IDFSponsorsTicker.buildRandomSponsorsArray(IN_sourceArray, IN_randomArray);
        }
        else // populate the last half of the array with the remaining available numbers
        {
            for (var i = 0; i < IN_sourceArray.length; i++)
            {
                if ($.inArray(i, IN_randomArray) == -1)
                {
                    IN_randomArray.push(i);
                }
            }
        }
    },
    buildSponsorsArray: function(IN_sourceArray, IN_randomArray)
    {
        for (var i = 0; i < IN_sourceArray.length; i++)
        {
            IN_randomArray.push(i);
        }
    },
    returnRandomNumber: function(IN_min, IN_max)
    {
        return Math.floor(Math.random() * (IN_max - IN_min + 1)) + IN_min;
    },
    swapSponsorImage: function(IN_imagePath)
    {
        var imgPath = "";
        if (IN_imagePath.substr((IN_imagePath.lastIndexOf('.') - 5), 5) != "_gray") // color image is visible, switch to gray
        {
            imgPath = IN_imagePath.substr(0, (IN_imagePath.lastIndexOf('.'))) + "_gray" + IN_imagePath.substr(IN_imagePath.lastIndexOf('.'));
            return imgPath;
        }
        else
        {
            imgPath = IN_imagePath.substr(0, (IN_imagePath.lastIndexOf('.') - 5)) + IN_imagePath.substr(IN_imagePath.lastIndexOf('.'));
            return imgPath;
        }
    },
    fadeOutFirstSponsor: function(IN_callback)
    {
        $("#sponsorsTicker > div:first").animate({ opacity: 0, marginRight: 0, width: 0 }, function() { $("#sponsorsTicker > div:last").fadeIn("fast"); $(this).remove(); });
    },
    createNewSponsorElement: function(IN_counter)
    {
        var newElement = '<div><img src="';
        newElement += goldSponsors[this.randomSponsors[IN_counter]].grayLogoPath;
        newElement += '" alt="" /></div>';

        $("#sponsorsTicker").append(newElement);

        var newEl = $("#sponsorsTicker > div:last > img");

        this.attachImageRollovers(newEl);
        this.attachTickerClicks(newEl);
    },
    animateSponsorImages: function()
    {
        if (!IDFSponsorsTicker.stopAnimation)
        {
            IDFSponsorsTicker.fadeOutFirstSponsor();
            IDFSponsorsTicker.createNewSponsorElement(IDFSponsorsTicker.nextSponsor);
            IDFSponsorsTicker.incrementSponsorCounter(IDFSponsorsTicker.nextSponsor);
        }
    },
    startSponsorTimer: function()
    {
        setInterval(IDFSponsorsTicker.animateSponsorImages, 6000);
    },
    init: function(options)
    {
        if (options.nonRandom) {
            IDFSponsorsTicker.buildSponsorsArray(goldSponsors, IDFSponsorsTicker.randomSponsors);
        } else {
            IDFSponsorsTicker.buildRandomSponsorsArray(goldSponsors, IDFSponsorsTicker.randomSponsors);
        }
        this.nextSponsor = goldSponsors.length >= 6 ? 6 : goldSponsors.length;
        IDFSponsorsTicker.writeTickerToPage(goldSponsors, IDFSponsorsTicker.randomSponsors);
        IDFSponsorsTicker.attachImageRollovers();
        IDFSponsorsTicker.attachTickerClicks();
    }

};

$(document).ready(function()
{
    $("#sponsorsTicker").mouseover(function() { IDFSponsorsTicker.stopAnimation = true; });
    $("#sponsorsTicker").mouseout(function() { IDFSponsorsTicker.stopAnimation = false; });

    var nonRandom = false;
    if ($("#sponsorsTicker").hasClass('non-random')) {
        nonRandom = true;
    }

    IDFSponsorsTicker.init({ nonRandom: nonRandom });

    if (goldSponsors.length > 6)
    {
        IDFSponsorsTicker.startSponsorTimer();
    }
});
