var currentBanner = 0;
var listBanners;
var rotate;
var timeOut;

function initBanner(start)
{
    this.listBanners = new Array();
    this.currentBanner = start;
    this.rotate = 1;
    this.timeOut = 0;
}

function addBanner(id_banner, big_img, small_img, description)
{
    var arr = new Array(id_banner, big_img, small_img, description);
    
    this.listBanners.push(arr);    
}

/**
 * Klikken op de small image moet de banner doen stoppen met roteren
 * en description tonen, eventueel met slide
 */
function hoverSmallImage(id_banner, rotate)
{    
    this.rotate = rotate;
    
    for (x in this.listBanners)
    {
        var obj = this.listBanners[x];

        var id = obj[0];        
        
        var name = "#img_small_" + id;
        var textName = "#text_" + id;
        var imgBig = obj[1];
        
        if (id == id_banner)
        {                        
            $(textName).css("display", "block");            
            $(name).css("opacity", 1);
            $("#banner_big").attr("src", imgBig);
            this.currentBanner = id;
        }
        else
        {            
            $(textName).css("display", "none");
            $(name).css("opacity", 0.4);
        }
    }
}

function resumeRotating(id_banner)
{    
    setTimeout(
        function()
        {
            if (this.rotate == 1)
            {                
                var nextBanner = 0;
                if (this.currentBanner > 0)
                {
                    nextBanner = getNextBanner(this.currentBanner);
                }
                else
                {
                    nextBanner = getNextBanner(id_banner);
                }
                
                hoverSmallImage(nextBanner, 1);
                resumeRotating(nextBanner);
            }
            else
            {
                this.timeOut = 1;
            }
        }
        , 4000
    );
}

function setRotate(rotate)
{
    this.rotate = rotate;
    
    if (this.timeOut)
    {
        resumeRotating(this.currentBanner);
        this.timeOut = 0;
    }
}

function getNextBanner(id_banner)
{
    var nextBanner = 0;
    var nextComing = 0;
    
    for (x in this.listBanners)
    {
        var obj = this.listBanners[x];

        var id = obj[0];
        
        if (nextComing)
        {
            nextBanner = id;
            nextComing = 0;
        }
        
        if (id == id_banner)
        {
            nextComing = 1;
        }        
    }
    
    if (!nextBanner)
    {
        var obj = this.listBanners[0];
        nextBanner = obj[0];
    }
    
    return nextBanner;
}
