var LiSwitcher = function() {}

LiSwitcher.prototype = {
    initialize: function(CONTROLS, VIEWPORT) {
        this.CONTROLS = CONTROLS || "div#liSwitcher-controls";
        this.PREVIOUS = $(this.CONTROLS).children("a#liSwitcher-previous");
        this.NEXT = $(this.CONTROLS).children("a#liSwitcher-next");
        this.VIEWPORT = VIEWPORT || "div#liSwitcher-viewport";
        this.ITEMS = $(this.VIEWPORT).children("ul").children("li");
        this.INTERVAL = 5000;
        this.index = 0;
        var Scope = this;
        this.timer = setInterval(function() { Scope.nextImage(); }, this.INTERVAL);
        this.jsActive(this.CONTROLS, this.ITEMS);
        this.eventHandlers(this.PREVIOUS, this.NEXT);
    },
    
    jsActive: function(CONTROLS, ITEMS) {
        $(CONTROLS).show();
        $(ITEMS).not(":first").hide();
    },
    
    fadeIn: function(index) {
        this.ITEMS.eq(index).fadeIn();
    },
    
    hide: function(index) {
        this.ITEMS.eq(index).hide();
    },    
    
    nextImage: function() {
        this.hide(this.index);
        this.index++;
        if(this.index >= this.ITEMS.length) {
            this.index = 0;
        }
        this.fadeIn(this.index);
    },
    
    previousImage: function() {
        this.hide(this.index);
        this.index--;
        if(this.index <= -1) {
            this.index = this.ITEMS.length - 1;
        }
        this.fadeIn(this.index);
    
    },
    
    eventHandlers: function(PREVIOUS, NEXT) {
        var Scope = this;
        $(PREVIOUS).bind("click",function() {
            Scope.previousImage();
            clearInterval(Scope.timer);
            return false;
        });
        $(NEXT).bind("click",function() {
            Scope.nextImage();
            clearInterval(Scope.timer);
            return false;
        });
    }

};


var liSwitcher = new LiSwitcher();
$(function() {
    liSwitcher.initialize();
});
