﻿var BannerRotator = new function()
{
    this.BANNER_ARRAY = null;
    this.BANNER_INDEX = -1;
    this.FADE_VALUE = 0.15;
    this.AUTO_PLAY = true;
    this.TIMEOUT = 0;

    this.Init = function(settings)
    {
        BannerRotator.BANNER_ARRAY = settings;
        if(BannerRotator.BANNER_ARRAY.length > 0)
        {
            BannerRotator.BANNER_INDEX = Math.floor(Math.random()*BannerRotator.BANNER_ARRAY.length);
            BannerRotator.ShowBanner(BannerRotator.BANNER_INDEX);
        }
    };
    
    this.GetPageLinks = function()
    {
        var rtnstr = '';
        
        for(var i = (BannerRotator.BANNER_ARRAY.length - 1); i >= 0; i--)
        {
            rtnstr += '<a href="#" class="pagelink' + (BannerRotator.BANNER_INDEX == i ? ' selected' : '') + '" onclick="BannerRotator.UserSelect(' + i + '); return false;"><span></span></a>';
        }
        
        return rtnstr;
    };
    this.Increment = function()
    {
        BannerRotator.BANNER_INDEX++;
        if(BannerRotator.BANNER_INDEX == BannerRotator.BANNER_ARRAY.length) { BannerRotator.BANNER_INDEX = 0; }
        BannerRotator.ShowBanner(BannerRotator.BANNER_INDEX);
    };
    this.Rotate = function()
    {
        $("#banner-rotator .title").fadeTo(1000, 0, function() { 
            $("#banners .banner").fadeTo(1000, .15, BannerRotator.Increment);
        });
    };
    this.ShowBanner = function(index)
    {
        if(BannerRotator.TIMEOUT != 0) { clearTimeout(BannerRotator.TIMEOUT); }
        
        $("#banners .banner").attr("href", BannerRotator.BANNER_ARRAY[index][2]);
        $("#banners .banner img").attr("src", BannerRotator.BANNER_ARRAY[index][0]);
        $("#banner-rotator .title .text").text(BannerRotator.BANNER_ARRAY[index][1]);
        $("#banner-rotator .title .text").attr("href", BannerRotator.BANNER_ARRAY[index][2]);
        $("#banner-rotator #links").empty();
        $("#banner-rotator #links").append(BannerRotator.GetPageLinks());
        
        $("#banners .banner").fadeTo(1000, 1, function() {
            $("#banner-rotator .title").fadeTo(1000, .75, function() {
                if((BannerRotator.BANNER_ARRAY.length > 1) && BannerRotator.AUTO_PLAY) { BannerRotator.TIMEOUT = setTimeout(BannerRotator.Rotate, 10000); }
            });
        });
    };
    this.UserSelect = function(index)
    {
        BannerRotator.AUTO_PLAY = false;
        BannerRotator.BANNER_INDEX = index;
        BannerRotator.ShowBanner(index);
    };
};
