/**
 * User: maurogadaleta
 * Date: 02/09/11
 * Time: 20:41
 */
jQuery(document).ready(function () {
    var newWidth = 0;
    $('.banner_item').each(function () {
        BANNER.number_elements++;
        newWidth += $(this).width();
    });

    $('#banners ul.mask').width(newWidth);

    BANNER.constructor();
});

var BANNER = BANNER || {};
BANNER.activeItem = 1;
BANNER.nextItem = 1;
BANNER.number_elements = 0;
BANNER.automatic = [];

BANNER.constructor = function () {
    BANNER.listeners();
};
BANNER.listeners = function () {
    $('#banners #prev').click(BANNER.previous);
    $('#banners #next').click(BANNER.next);

    $('#banners .number_banner').click(function () {
        clearInterval(BANNER.automatic);
        BANNER.nextItem = $(this).attr('href');
        BANNER.goToItem();
        return false;
    })

    BANNER.automatic = setInterval(function () {
        BANNER.nextItem++;
        if (BANNER.nextItem > BANNER.number_elements)
            BANNER.nextItem = 1;

        BANNER.goToItem();
    }, 5000);
};
BANNER.previous = function () {
    clearInterval(BANNER.automatic);
    BANNER.nextItem = BANNER.activeItem - 1;
    if (BANNER.control()) {
        BANNER.goToItem();
    }

};
BANNER.next = function () {
    clearInterval(BANNER.automatic);
    BANNER.nextItem = BANNER.activeItem + 1;
    if (BANNER.control()) {
        BANNER.goToItem();
    }
};
BANNER.control = function () {
    if (BANNER.nextItem > 0 && BANNER.nextItem <= BANNER.number_elements)
        return true;
    else
        return false;
};
BANNER.goToItem = function () {
    $('#banners .banner_item .image,#banners .banner_item .text .top').fadeOut(function () {
        var mask = $(this);
        var that = $('#banners ul.mask');
        that.animate({
            marginLeft:-((BANNER.nextItem - 1) * 583)
        }, 0, function () {
            mask.fadeIn();
        });
    });

    $('#banners ul.mask li').removeClass('active').find('.banner_' + BANNER.nextItem).addClass('active');
    BANNER.activeItem = BANNER.nextItem;
};
