/*!
 * jQuery customSlider Plugin
 * Version: 1.0 (13-JUN-2009)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires: jQuery v1.3.1 or later
 *
 */
(function($){ //jQuery.noConflict()compliant
   $.fn.customSlider = function(options) {

      // build main options before element iteration
      //----------------------------------------------------------------------
      var s = $.extend({}, $.fn.customSlider.defaults, options);

      // loop through each tab and enable analytics
      return this.each(function()
      {
         //container
         var _obj = $(this);
         //next button
         var _nextButton = _obj.find(s.nextButtonClass);
         //array with slide objects
         var _items = new Array();

         var animating = false;

         _obj.find(s.itemClass).each(
            function(i, el){
               var $item = $(el);
               $item.hide();
               _items.push($item);
            }
         );

         //mostro il primo elemento
         if(_items.length > 0)
         {
            _items[0].show();

            //num elementi
            var numElements   = _items.length;
            //elemento corrente
            var curElement    = 0;
            var nextElement   = 1;

            _nextButton.click(
               function(){

                  if ( !animating ) {
                     animating = true;

                     if ( curElement == numElements ) {
                        curElement = 0;
                     }

                     if(nextElement == numElements)
                        nextElement = 0;

                     _items[curElement].fadeOut(600, function() {
                        _items[nextElement].fadeIn(600, function(){

                           curElement = curElement+1;
                           nextElement = nextElement+1;

                        });
                     });

                     animating = false;

                  }
               }
            );
         }
         else
         {
            _nextButton.hide();
         }

      });
   };

   $.fn.customSlider.defaults = {
      itemClass: '.slide-item',
      speed: "normal",
      nextButtonClass: '.next'

   };

})(jQuery);