(function($) {
    $.carruselHome = {
        defaults: {
            autoscroll: 0,
            start: 0,
            start_to_start: false
        }
    };
    $.fn.extend({
        carruselHome: function(args) {
            var config = $.extend({}, $.carruselHome.defaults, args);
            this.each(function() {
                var self = this;
                this.lista = $(this).find('li');
                var li = this.lista.get(0);
                this.left_inicial = ($(this).outerWidth() / 2) - ($(li).outerWidth() / 2);
                this.left_increment = $(li).outerWidth() + 10;
                this.pos = 0;
                // Repintado de flechas
                this.evalArrows = function() {
                    if (this.pos === 0) {
                        $(this).children('.carrusel-prev').addClass('carrusel-prev-disabled');
                    }
                    else {
                        $(this).children('.carrusel-prev').removeClass('carrusel-prev-disabled');
                    }
                    if (this.pos === this.lista.length - 1) {
                        $(this).children('.carrusel-next').addClass('carrusel-next-disabled');
                    }
                    else {
                        $(this).children('.carrusel-next').removeClass('carrusel-next-disabled');
                    }
                };
                // Movimiento a placer
                this.moveTo = function(pos) {
                    this.pos = pos || 0;
                    var self = this;
                    this.lista.each(function(i) {
                        this.position.left = self.left_inicial + (self.left_increment * (i - pos));
                        var li    = this;
                        var dest  = {};
                        dest.left = this.position.left;
                        dest.top  = this.position.top;
                        if ($(this).hasClass('marcado')) {
                            $(this)
                                .removeClass('marcado')
                                .css('z-index', 10);
                            dest.width       = 130;
                            dest.height      = 90;
                            dest.borderWidth = 1;
                        }
                        if (self.pos === i) {
                            $(this)
                                .addClass('marcado')
                                .css('z-index', 20);
                                
                            dest.width       = 190;
                            dest.height      = 150;
                            dest.top         = 0;
                            dest.left        = dest.left - 40;
                            dest.borderWidth = 3;
                        }
                        $(this).animate(dest, 250);
                    });
                    self.evalArrows();
                };
                // Defaults de las flechas
                $('<div class="carrusel-prev disabled"></div>')
                    .click(function() {
                        if (self.pos === 0) { return; }
                        self.moveTo(--self.pos);
                        self.evalArrows();
                        clearInterval(self.timer);
                    })
                    .appendTo(this);
                $('<div class="carrusel-next"></div>')
                    .click(function() {
                        if (self.pos === self.lista.length - 1) { return; }
                        self.moveTo(++self.pos);
                        self.evalArrows();
                        clearInterval(self.timer);
                    })
                    .appendTo(this);
                // Evento Click de los <li>
                this.lista.each(function(i) {
                    var left = self.left_inicial + (self.left_increment * i);
                    $(this).css({
                        'left': left,
                        'display': 'block'
                    });
                    this.n = i;
                    this.position = $(this).position();
                    $(this).click(function(e) {
                        if (!$(this).hasClass('marcado')) {
                            self.moveTo(this.n);
                        }
                        clearInterval(self.timer);
                    });
                });
                // Evento Click de los <a>
                $(this).find('a').click(function(e) {
                    if (!$(this).parents('.marcado').length) {
                        e.preventDefault();
                    }
                });
                // Lógica de movimiento
                this.moveTo(config.start);
                firsttime=true;
                if (typeof(config.autoscroll) == 'number' && config.autoscroll > 0) {
                    this.timer = setInterval(function() {
                        if (self.pos === self.lista.length - 1) {
                            self.pos = -1;
                        }
                        if (firsttime===true & config.start_to_start===true) { firsttime=false; self.pos=-1; }
                        self.moveTo(++self.pos); self.evalArrows(); 
                    }, config.autoscroll);
                }
            });
            return this;
        }
    });
})(jQuery);