/*
 * Product Carousel object
 *
 * Params:
 *
 *       idCarousel: id of an empty DIV to be converted into a carousel
 *       aProducts: Array of products to be shown in carousel, eg:
 *                  aProducts = [["Image URL", "Comment", "Affiliate Link URL"], ["Image URL", "Comment2", "Affiliate Link URL"], ...];
 *
 * Returns:
 *       carousel Object
 *
 */


function createCarousel(idCarousel, title, aProducts)
{
    if(aProducts.length<1)
    {
        var oCarousel = document.getElementById(idCarousel);
        oCarousel.style.display = 'none';
        return false;
    }

    for(var j, x, i = aProducts.length; i; j = parseInt(Math.random() * i), x = aProducts[--i], aProducts[i] = aProducts[j], aProducts[j] = x);


    this.oCarousel = document.getElementById(idCarousel);
    this.oCarousel.setAttribute('class', 'carousel');
    this.idCarousel = idCarousel;

    this.oTitle = document.createElement('div');
    this.oTitle.setAttribute('class', 'title');
    var textNode = document.createTextNode(title);
    this.oTitle.appendChild(textNode);
    this.oCarousel.appendChild(this.oTitle);

    this.bannerWidth = (aProducts.length * 110)+20;

    this.oBanner = document.createElement('div');
    this.oBanner.setAttribute('class', 'banner');
    this.oBanner.style.left = '0px';
    this.oBanner.style.width = this.bannerWidth + 'px';
    this.oCarousel.appendChild(this.oBanner);

    this.decodeEntities = function(str) { var tmpTA=document.createElement('textarea'); tmpTA.innerHTML = str; return tmpTA.value; }

    this.itemNo = 1;

    this.oItems = [];
    for(i=0; i<aProducts.length; i++)
    {
        var aSlide = aProducts[i];

        this.oItems[i] = document.createElement('a');
        this.oItems[i].setAttribute('class', 'item');
        this.oItems[i].setAttribute('href', aSlide[2]);
        this.oItems[i].setAttribute('target', '_blank');

        var oImg = document.createElement('img');
        oImg.setAttribute('src', aSlide[0]);
        oImg.setAttribute('alt', aSlide[1]);
        oImg.setAttribute('title', aSlide[1]);
        this.oItems[i].appendChild(oImg);

        var oComment = document.createTextNode(this.decodeEntities(aSlide[1]));
        this.oItems[i].appendChild(oComment);

        this.oBanner.appendChild(this.oItems[i]);
    }

    this.oButtons = document.createElement('div');
    this.oButtons.setAttribute('class', 'buttons');
    this.oCarousel.appendChild(this.oButtons);

    this.timeout = false;
    this.afTimeout = false;

    this.next = function(auto)
    {
        if(this.timeout) return false;

        if(!auto)
        {
            clearTimeout(this.afTimeout);
            this.afTimeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.autoflicker(true)', 15000);
        }

        if(this.itemNo == this.oItems.length-2) return this.rewind();

        var start = parseInt(this.oBanner.style.left);
        var end = start - 110;

        var to = 0;
        for(i=start; i>= end; i-=5)
        {
            this.timeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.setBannerX('+i+')', to*50);
            to++;
        }

        this.timeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.timeout=false', to*50);

        this.itemNo++;
    }

    this.prev = function(auto)
    {
        if(this.timeout) return false;

        if(!auto)
        {
            clearTimeout(this.afTimeout);
            this.afTimeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.autoflicker(true)', 15000);
        }

        if(this.itemNo == 1) return this.fastforward();

        var start = parseInt(this.oBanner.style.left);
        var end = start + 110;

        var to = 0;
        for(i=start; i<= end; i+=5)
        {
            this.timeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.setBannerX('+i+')', to*50);
            to++;
        }

        this.timeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.timeout=false', to*50);

        this.itemNo--;
    }

    this.rewind = function()
    {
        var start = parseInt(this.oBanner.style.left);
        var end = 0;

        var to = 0;
        for(i=start; i<= end; i+=15)
        {
            this.timeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.setBannerX('+i+')', to*50);
            to++;
        }

        this.timeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.setBannerX(0)', to*50);
        this.timeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.timeout=false', to*50);

        this.itemNo = 1;
    }

    this.fastforward = function()
    {
        var start = parseInt(this.oBanner.style.left);
        var end = 0 - (110 * (this.oItems.length-3));

        var to = 0;
        for(i=start; i>= end; i-=15)
        {
            this.timeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.setBannerX('+i+')', to*50);
            to++;
        }

        this.timeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.setBannerX('+end+')', to*50);
        this.timeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.timeout=false', to*50);

        this.itemNo = this.oItems.length-2
    }

    this.autoflicker = function(change)
    {
        clearTimeout(this.afTimeout);
        if(change) { this.next(true); var t = 5000; }
        else var t = parseInt((Math.random()*5+1)*1000);

        this.afTimeout = setTimeout('document.getElementById("'+this.idCarousel+'").carousel.autoflicker(true)', t);
    }

    this.setBannerX = function(x)
    {
        this.oBanner.style.left = x + 'px';
    }

    if(this.oItems.length>3)
    {
        this.oPrevbutton = document.createElement('a');
        this.oPrevbutton.setAttribute('class', 'button');
        this.oPrevbutton.style.left = '0';
        this.oPrevbutton.carousel = this;
        this.oPrevbutton.onclick = function() { this.carousel.prev(); }
        var img = document.createElement('img');
        img.setAttribute('src', 'img/carousel_prev.png');
        this.oPrevbutton.appendChild(img);


        this.oNextbutton = document.createElement('a');
        this.oNextbutton.setAttribute('class', 'button');
        this.oNextbutton.style.right = '0';
        this.oNextbutton.carousel = this;
        this.oNextbutton.onclick = function() { this.carousel.next(); }
        var img = document.createElement('img');
        img.setAttribute('src', 'img/carousel_next.png');
        this.oNextbutton.appendChild(img);

        this.oCarousel.appendChild(this.oPrevbutton);
        this.oCarousel.appendChild(this.oNextbutton);

        this.autoflicker();
    }



    this.oCarousel.carousel = this;

    return this;
}