(function($) {
    $.fn.customFadeIn = function(speed, callback) {
        $(this).fadeIn(speed, function() {
                if(jQuery.browser.msie)
                        $(this).get(0).style.removeAttribute('filter');
                if(callback != undefined)
                        callback();
        });
    };
    $.fn.customFadeOut = function(speed, callback) {
        $(this).fadeOut(speed, function() {
                if(jQuery.browser.msie)
                        $(this).get(0).style.removeAttribute('filter');
                if(callback != undefined)
                        callback();
        });
    };
})(jQuery);

(function($){
    
    /**
     * Slide show
     *
     * @param   string      elemId      The id of the wrapper element
     */
    var SlideShow = function(elemId,delay) {
        
        var self = this;
        
        // Store some values
        self.$container     = $('#' + elemId);
        self.delay          = delay;
        self.$items         = this.$container.find('.rotator li');
        self.total          = self.$items.length;
        self.timer          = null;
        self.fadeDuration   = 1000;
        self.position	    = 1;
      
        // Show/Hide for the first item
        self.$items.not(':first').hide();
        self.$items.filter(':first').addClass('current').customFadeIn(1,function(){});
    };
    
    SlideShow.prototype = {
    	
        play: function()
        {
            var self = this;      
            self.timer = setInterval(function(){
                self.next.call(self);
            },self.delay);
        },
        next:  function()
        {
        	var self = this;
        	self.position += 1;
            if (self.position > self.total) {
            	self.position = 1;	
            }
            self.seek(self.position);
        },
        seek:  function(itemNumber)
        {
            var self = this;
            if (!self.$items.find(':animated').length) {
                self.$items.filter(function(){return $(this).hasClass('current');}).customFadeOut(self.fadeDuration,function(){
                     self.$items.filter(function(){return $(this).hasClass('current');}).removeClass('current');
                     $(self.$items.eq(itemNumber-1)).addClass('current').customFadeIn(self.fadeDuration,function(){});
                });
            }// End if
        }
    };
    
    $(document).ready(function(){
        var show = new SlideShow('image_banner',5000);
        show.play();
    });
})(jQuery);
