//jQuery('.widget-with-list').ismRotator({'isButtons' : 1, 'isScrollable' : 1, 'isRotating': 1}); 

$(document).ready(function(){
	jQuery('.sliderBox ').ismRotator({'isButtons' : 1,  'isRotating': 1, 'rotateTime' : 5}); 
});


(function($){

    $.fn.ismRotator = function(method, options) {

        if(this.length != 1)
            return this.each(function() { $(this).ismRotator(method, options); });

        /**
         *  isButtons - show/hide buttons
         *  isScrollable - show/hide arrows for scrolling
         *  isRotating - rotate banners yes/no
         *  length - lenth of ul li list
         *  index - current index in list to show
         *  rotateTime - how often banners are changed
         */
        var settings = {
            'isButtons' : 0,
            'isScrollable' : 0,
            'isRotating' : 0,
            'length' : -1,
            'index' : 0,
            'rotateTime' : 3,
            'timer' : 0,
            'first' : true
        };

        if(typeof options == 'undefined') {
            if(typeof method == 'object')
                options = method;
            else
                options = {};
        }
        
        // merge settings and options
        var settings = $.extend(settings, options);
        var self = this;

        var methods = {
            init : function() {                
                settings.length = self.find('ul li').length;
                if (settings.length == -1) return;

                self.find('ul').wrap('<div class="ism-rotator-container"></div>');
                self.find('ul li').first().addClass('current');
				if (settings.length == 1) return;
                methods.customize();
            },

            customize : function() {
                methods.addButtons();
                methods.addScrolling();
                methods.rotate();
            },

            addButtons : function() {
                if (settings.isButtons == 0) return;
                
                self.find('.ism-rotator-container').append('<div class="pager" style="width:'+(settings.length*21)+'px"></div>');
                for (var i = 0; i < settings.length; i++) {
                    self.find('div.pager').append('<span class="radio"></span>');
                }
                self.find('.radio').first().addClass('current');
                self.find('.radio').click(methods.clickButton);
            },

            clickButton : function() {
                methods.setExactIndex($(this).index());
                methods.next();
                methods.start();
            },

            addScrolling : function() {
                if (settings.isScrollable == 0) return;

                self.find('ul').before('<div class="scrolling prev"></div>').after('<div class="scrolling next"></div>');                
                self.find('.scrolling').click(methods.clickScrolling);
            },

            clickScrolling : function() {                                
                if ($(this).hasClass('prev')) {
                    methods.setNextIndex(-1);
                } else if (($(this).hasClass('next'))) {
                    methods.setNextIndex(1);
                }
                methods.next();
                methods.start();
            },

            rotate : function() {
                if (settings.isRotating == 0) return;
                
                methods.start();
                
                if (settings.first === true) {
                    methods.setNextIndex(0);
                    settings.first = false;
                } else {
                    methods.setNextIndex(1);
                }
                methods.next();
            },

            //start rotating
            start : function() {
                if (settings.isRotating == 0) return;

                clearInterval(settings.timer);
                settings.timer = setInterval(methods.rotate, settings.rotateTime * 1000);
            },

            setIndex: function (index) {
                settings.index = index;
                if (settings.length <= index) {
                    settings.index = 0;
                } else if (index < 0) {
                    settings.index = settings.length - 1;
                }
            },

            setExactIndex : function(index) {
                methods.setIndex(index);
            },

            setNextIndex : function(delta) {
                var index = settings.index + (delta);
                methods.setIndex(index);
            },

            next : function() {                
                self.find('ul li').fadeOut(0).removeClass('current').eq(settings.index).fadeIn(400).addClass('current');

                if (settings.isButtons == 1) {
                    self.find('.radio').removeClass('current').eq(settings.index).addClass('current');
                }
            }
        };

        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || ! method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' +  method + ' does not exist on jQuery.ism-rotator');
        }

        return self;
    };

})(jQuery);
