/**
 * @author Rodolfo Solorzano
 * @version 0.1
 */

(function($){
    $.extend({
        fnsCarouselHomeBimboNutricion : {
            params : {
                bannerWidth : 589, //Ancho de cada banner
                nextIndex : 1, //Próximo banner al iniciar
                timeInterval: 12000, //Tiempo que durará el proceso total (animacion y pausa)
                interval : null //Variable que controla el intervalo
            },
            
            moveTo: function(index) {
                if(typeof index == 'undefined'){
                    index = this.params.nextIndex;
                }
                this.params.bannerFeed.animate({left: -1*this.params.bannerWidth*index},2000);
                //console.log(index);
                var title = '';
                if(typeof this.params.buttons[index-1] != 'undefined'){
                    title = this.params.buttons[index-1].title;
                }
                this.params.articleTitle.html(title);
                index++;
                if (index >= this.params.banners.size()){
                    index = 0;
                }
                this.params.nextIndex = index;
            },
            autoMove: function() {
               this.params.interval = setInterval('$.fnsCarouselHomeBimboNutricion.moveTo()',this.params.timeInterval);
            },
            moveOnClick: function() {
                this.params.buttons.bind('click',function(event){
                   event.preventDefault();
                   $.fnsCarouselHomeBimboNutricion.moveTo($(event.target).parent().index()+1);
                   clearInterval($.fnsCarouselHomeBimboNutricion.params.interval);
                });
            },
            execute: function() {
                var bannerFeedWidth = this.params.banners.size()*this.params.bannerWidth;
                this.params.bannerFeed.css('width',bannerFeedWidth+'px');
                this.autoMove();
                this.moveOnClick();
            }
        }
    });
    
    $.fn.carouselHomeBimboNutricion = function(){
        var parentFunction = $.fnsCarouselHomeBimboNutricion;
        var params = parentFunction.params;
        params.bannerFeed = $(this);
        params.banners = $(this).children('li');
        params.buttons = $('div#contArticulos ul#lista li a');
        params.articleTitle = $('div#contArticulos h3');
        return parentFunction.execute();
    }
})(jQuery);

