;(function($){
  $.fn.tabs = function(settings){
    /* Default settings */
    var _default = {
      index : 0,
      cycle : false,
      fxDelay : 300,
      cycleDelay : 5000,
      activeClass : 'sc-active'
    };
    
    $.extend(_default, settings);

    return this.each(function(){
      var $this = $(this),
          $tabs = $this.find('>li>a'),
          $targets = $($tabs.map(function(){return $(this).attr('href')}).get().join(', ')),
          _index = _default.index,
          _interval;
  
      /* Initialize plugin method */
      this.init = function(){
        var _self = this;
        
        $targets.eq(_index).show();
        
        $tabs.bind('click focus', function(){
          if(_default.cycle)
            clearInterval(_interval);
          
          _self.switchTo($tabs.index(this));
          return false;
        }).eq(_index).addClass(_default.activeClass);
        
        if(_default.cycle)
          _interval = setInterval(function(){_self.switchTo(_index+1)}, _default.cycleDelay);
      };
      
      /* Swicth tabs method */
      this.switchTo = function(index){
        if(index == _index) return;
        var index = index%$tabs.length;
            
        $tabs.eq(_index).removeClass(_default.activeClass).end().eq(index).addClass(_default.activeClass);
        $targets.eq(_index).hide().end().eq(index).show();
        _index = index;  
      };
      
      /* Initialize plugin */
      this.init();
    });
  };
})(jQuery);
