// Two Sisters Shop
// Featured items jQuery plugin

(function($) {

   $.fn.tssFeatured = function(options) {
      // main options
      var opts = $.extend({}, $.fn.tssFeatured.defaults, options);

      return this.each(function(){
         var $this = $(this)
            ,$parent = $this.parent()
            ,$items = $this.children()
            ,$first = $items.first()
            ,count = $items.size()
            ,i = 0
            ,timer = false;
               
         init();
      
         function init() {
            $items.hide();
            $first.show();

            if (count > 1) {
               initInterval();
               
               // pause on mouse over
               $parent.hover(
                  function() {
                     clearInterval(timer);
                  },
                  function() {
                     initInterval();
                  }
               );
            }
         };
         
         function initInterval() {
            timer = setInterval(function() {
               rotate();
            }, opts.speed);
         };
         
         function rotate() {
            if (count > 1) {
               i++;
               var $current = $items.filter(':visible:first');
               $current = ($current) ? $current : $first;
               var $next = $current.next();
               if ($next.length == 0) $next = $first;
               
               $current.fadeOut('fast', function() {
                  $next.fadeIn('slow');
               });
            }
         };
      });
   };
   
   $.fn.tssFeatured.defaults = {
      speed: 4000
   };

})(jQuery);
