/**
 * Menu component and its initialization
 *
 * $Id: slide-menu.js 19 2011-10-11 20:16:53Z capricorn $
 *
 * @author Alexander "Capricorn" Potehin <alexander@potehin.pro>
 */

/**
 * @see SlideCarousel
 */
function MenuSlideCarousel(baseElem, slideSelector, controlSelector)
{
    SlideCarousel.call(this, baseElem, slideSelector, controlSelector);

    this.active = false;
    chsuJQ(this.controls).removeClass("active");
    chsuJQ(this.viewport).css("z-index", "1000");

    chsuJQ(this.baseElem).mouseleave(function() { this.handler.hide(); });

    this.slideHeight = 0;
    this.refreshViewport();
}

MenuSlideCarousel.prototype = new SlideCarousel();

/**
 * @see SlideCarousel#refreshViewport
 */
MenuSlideCarousel.prototype.refreshViewport = function()
{
    SlideCarousel.prototype.refreshViewport.call(this);

    chsuJQ(this.slides).each(function() { this.handler.slideHeight = Math.max(this.handler.slideHeight, chsuJQ(this).height()); });
    chsuJQ(this.slides).height(this.slideHeight);
}

/**
 * @see SlideCarousel#changeSlide
 */
MenuSlideCarousel.prototype.changeSlide = function(newIndex)
{
    var oldStatus = this.active;

    if (!this.active)
        this.active = true;
    else
        this.active = newIndex != this.slideIndex;

    if (oldStatus != this.active)
    {
        if (this.active)
            chsuJQ(this.slides).slideDown("slow");
        else
            chsuJQ(this.slides).slideUp("slow");
    }

    SlideCarousel.prototype.changeSlide.call(this, newIndex);

    if (!this.active) chsuJQ(this.controls).removeClass("active");
};

/**
 * Hide menu slides if active
 */
MenuSlideCarousel.prototype.hide = function()
{
    this.active = false;
    chsuJQ(this.slides).slideUp("slow");
    chsuJQ(this.controls).removeClass("active");
}

chsuJQ(document).ready(function() {
    var carousel = new MenuSlideCarousel(chsuJQ("#menu"), ".content", ".menubar > li");
});

