
(function($) {

   $.fn.Rotate = function(options) {
      var opts = $.extend({}, $.fn.Rotate.defaults, options);
      
      var jFC = opts.controller;
      var jFS = opts.slideWrapper;
      var jSel = opts.selectedWrapper;
      
      var $slides = $(opts.slides).children();
      var $navCommands = $(jFC);
      
      var currentIndex = 0;            
      
      var timer;
      var maxItems = $(jFC).length;
      
      $slides.css({opacity: 0.0 });
      $slides.eq(currentIndex).css({opacity: 1.0});
      $navCommands.eq(currentIndex).addClass(jSel);
      
      $(this).find(jFC).each(function(i) {
         $(this).hover(function() {            
            clearInterval(timer);
            
            if (currentIndex != i)
               swapImage(i);
         }, function() {
            dotimer();
         });         
      });
      
      var swapImage = function(nextIndex) {
         current = $slides.eq(currentIndex);
         next = $slides.eq(nextIndex);
         
         $(jFC).removeClass(jSel);
         next.css({opacity: 0.0}).addClass('show').stop().animate({opacity:1.0}, 1000);         
         $(jFC).eq(nextIndex).addClass(jSel);
         current.stop().animate({opacity:0.0}, 1000).removeClass('show');
         currentIndex = nextIndex;         
      }
      
      var rotateNext = function() {
         var nextIndex = currentIndex + 1;
         if (nextIndex >= maxItems)
            nextIndex = 0;
         swapImage(nextIndex);         
      }
      
      var dotimer = function (){
			if((opts.auto) == true) {
				if(timer != null) 
					clearInterval(timer);
			    
        		timer = setInterval(rotateNext, 4000);
			}
		}
      
      dotimer();      
   }
   
   $.fn.Rotate.defaults = {
      controller: "", // must be class
      slideWrapper: "",
      selectedWrapper: "active",
      auto: true,
      easing: "swing",
      duration: 400,
      width: "100%"
   };
})(jQuery);
