/* idTabs ~ Sean Catchpole - Version 1.1 */

/* Options (in any order):

 start (number|string)
    Index number of default tab. ex: idTabs(0)
    String of id of default tab. ex: idTabs("tab1")
    default: class "selected" or index 0

 change (boolean)
    True - Url will change. ex: idTabs(true)
    False - Url will not change. ex: idTabs(false)
    default: false

 click (function)
    Function will be called when a tab is clicked. ex: idTabs(foo)
    If the function returns true, idTabs will show/hide content (as usual).
    If the function returns false, idTabs will not take any action.
    The function is passed four variables:
      The id of the element to be shown
      an array of all id's that can be shown
      the element containing the tabs
      and the current settings

 selected (string)
    Class to use for selected ex: idTabs(".current")
    default: ".selected"

*/
(function($){
  $.fn.idTabs = function(){
    //Loop Arguments matching options
    var s = $.extend({},$.idTabs.settings);
    for(var i=0; i<arguments.length; ++i) {
      var a=arguments[i];
      switch(a.constructor){
        case Object: $.extend(s,a); break;
        case Boolean: s.change = a; break;
        case Number: s.start = a; break;
        case Function: s.click = a; break;
        case String:
          if(a[0]=='.') s.selected = a;
          else s.start = a;
        break;
      };
    }
    if(s.selected.substr(0,1)!='.') //play nice
      s.selected='.'+s.selected;
    
    return this.each(function(){ $.idTabs(this,s); }); //Chainable
  }

  $.idTabs = function(self,options) {
    //Settings
    var s = $.extend({},$.idTabs.settings,options);
    
    //Setup Tabs
    var showId = function(){
      if($(this).is(s.selected))
        return s.change; //return if already selected
      var id = "#"+this.href.split('#')[1];
      var aList = []; //save tabs
      var idList = []; //save possible elements
      $("a",self).each(function(){
        if(this.href.match(/#/)) {
          aList[aList.length]=this;
          idList[idList.length]="#"+this.href.split('#')[1];
        }
      });
      if(s.click && !s.click(id,idList,self,s)) return s.change;
      var sel = s.selected.substr(1);
      //Clear tabs, and hide all
      for(i in aList) $(aList[i]).removeClass(sel);
      for(i in idList) $(idList[i]).hide();
      //Select clicked tab and show content
      $(this).addClass(sel);
      $(id).show();
      return s.change; //Option for changing url
    };

    //Bind idTabs
    var list = $("a[href^='#']",self).unbind('click',showId).click(showId);

    //Select default tab
    var test;
    if(typeof s.start == "number" &&(test=list.eq(s.start)).length); //Select num tab
    else if(typeof s.start == "string" //Select tab linking to id
         &&(test=list.filter("[href='#"+s.start+"']")).length);
    else if((test=list.filter(s.selected)).length); //Select tab with selected class
    else test=list.eq(0); //Select first tab
    test.removeClass(s.selected.substr(1)).click(); //Select tab

    return self; //Chainable (sorta)
  };

  //Defaults
  $.idTabs.settings = {
    start:null,
    change:false,
    click: false,
    selected:".active"
  };

  //Version
  $.idTabs.version = "1.1";

  //Auto-run
  $(function(){ $(".idTabs").idTabs(); });

})(jQuery)
