(function( $ ){

  var methods = {
    init : function( options ) {
      var settings={
        tabSelector: '.slide-tabs a',
        contentSelector: '.tab',
        interval: 5000,
        fadeInterval: 1000,
        animation: true
      };
      
      $.extend(settings,options);
      
      var parent=this;
      var container=$(this);
      var tabs=$(settings.contentSelector,this);
      var navButtons=$(settings.tabSelector,this);
      
      tabs.hide();
      $(settings.contentSelector+':first',this).show();
      $(settings.tabSelector+':first').addClass('current');
      
      var currentItem=0;
      var items=tabs.length;
      var timeOut;
      
      this.nextSlide=function()
      {
        currentItem=(currentItem+1)%items;
        navButtons.removeClass('current');
        $(settings.tabSelector+':eq('+currentItem+')',container).addClass('current');
        if (settings.animation) 
        {
          timeOut = setTimeout(parent.nextSlide, settings.interval);
        }
        tabs.fadeOut(settings.fadeInterval/2);
        tabs.hide();
        $(settings.contentSelector+':eq('+currentItem+')',container).fadeIn(settings.fadeInterval/2);
      }
      
      if (settings.animation) 
      {
        timeOut = setTimeout(this.nextSlide, settings.interval);
      }
      
      navButtons.click(function(){
        navButtons.removeClass('current');
        $(this).addClass('current');
        navButtons.each(function(index){
          if($(this).attr('class')=='current')
          {
            if (settings.animation) 
            {
              clearTimeout(timeOut);
              timeOut = setTimeout(parent.nextSlide, settings.interval);
            }
            currentItem=index;
            tabs.fadeOut(settings.fadeInterval/2);
            tabs.hide();
            $(settings.contentSelector+':eq('+currentItem+')',container).fadeIn(settings.fadeInterval/2);
          }
        });
        return false;
      });
    }
  };
  
  $.fn.slideTabs = function( method ) {
    
    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.slideTabs' );
    }    
  
  };

})( jQuery );
