/**
 * Banner carousel initialization
 *
 * $Id: slide-banner.js 15 2011-09-26 21:51:50Z capricorn $
 *
 * @author Alexander "Capricorn" Potehin <alexander@potehin.pro>
 */

/**
 * @see SlideCarousel
 */
function BannerSlideCarousel(baseElem, slideSelector, controlSelector)
{
    SlideCarousel.call(this, baseElem, slideSelector, controlSelector);

    this.timer = null;
    this.autoSlide();
}

BannerSlideCarousel.prototype = new SlideCarousel(null, null, null);

// TODO: Individual period for each Carousel instance
BannerSlideCarousel.prototype.period = 5000;

BannerSlideCarousel.prototype.changeSlide = function(newIndex)
{
    SlideCarousel.prototype.changeSlide.call(this, newIndex);
    this.autoSlide();
}

BannerSlideCarousel.prototype.autoSlide = function()
{
    if (this.timer != null) window.clearTimeout(this.timer);

    var instance = this;
    var nextIndex = (this.slideIndex + 1) % this.slides.length;
    var autoSlideClojure = function() { instance.changeSlide(nextIndex); };

    this.timer = window.setTimeout(autoSlideClojure, BannerSlideCarousel.prototype.period);
}

chsuJQ(document).ready(function() {
    chsuJQ(".banner-carousel").each(function() {
        new BannerSlideCarousel(this, ".slide", ".container > li");
    });
});

