jQuery.widget("snoopal.jCarousel", {

    _init: function(){

        // Caching element...
        this.options.$el = $(this.element[0]);

        this.options._totalPanels = this._calcPanels();
        this.options._panelWidth = this._calcWidth();
        this._bindEvents();
        this._adaptControls();

    },

    options: {
        $next: null,
        $prev: null,
        panelsNodeQuery: '> ul',
        _currentPanel: 0,
        disabledClass: "disabled"
    },

    _bindEvents: function() {

        var opts = this.options,
            self = this;

        opts.$next
                .bind("click", function(){
                    if(!$(this).data("isDisabled")){
                        self.scrollNext.call(self, $(this));
                    }
                })
                .bind("disable", function(){
                    self.disableScroller.call(self, $(this));
                })
                .bind("enable", function(){
                    self.enableScroller.call(self, $(this));
                });

        opts.$prev
                .bind("click", function(){
                    if(!$(this).data("isDisabled")){
                        self.scrollPrev.call(self, $(this));
                    }
                })
                .bind("disable", function(){
                    self.disableScroller.call(self, $(this));
                })
                .bind("enable", function(){
                    self.enableScroller.call(self, $(this));
                });

    },

    _calcPanels: function(){
        return this.options.$el.find(this.options.panelsNodeQuery).length;
    },

    _calcWidth: function(){
        return this.options.$el.find(this.options.panelsNodeQuery).width();  
    },

    scrollNext: function(element){
        var $el = this.options.$el,
            l = $el.css("left");

        $el.animate({
            left: parseInt(l)-this.options._panelWidth
        }, 500);

        this.options._currentPanel = this.options._currentPanel+1;

        this._adaptControls();

    },

    scrollPrev: function(){

        var $el = this.options.$el,
            l = $el.css("left");

        $el.animate({
            left: parseInt(l)+this.options._panelWidth
        }, 500);

        this.options._currentPanel = this.options._currentPanel-1;

        this._adaptControls();

    },

    disableScroller: function($scroller){
        $scroller.data("isDisabled", true).addClass(this.options.disabledClass);
    },

    enableScroller: function($scroller){
        $scroller.data("isDisabled", false).removeClass(this.options.disabledClass);
    },

    _canNext: function(){
        return (this.options._currentPanel < this.options._totalPanels-1);
    },

    _canPrev: function(){
        return (this.options._currentPanel > 0);
    },

    _adaptControls: function(){
        this._canNext() ? this.options.$next.trigger("enable") : this.options.$next.trigger("disable");
        this._canPrev() ? this.options.$prev.trigger("enable") : this.options.$prev.trigger("disable");
    }

});
