/**
 * Basic tabs plugin for jQuery
 * 
 * Call by using:
 * --------------
 * $('#tabmenu').infoTabs();
 * 
 * or to set options:
 * $('#tabmenu').infoTabs({
 *   option: 'value'
 * });
 * 
 * 
 * Options:
 * --------
 * activeClass      : [string] The class that the active tab should get
 * tabContentClass  : [string] Class that the content divs have
 * cancelLinkAction : [bool] If set to true, the tab id will not be displayed
 *                    in the address bar
 * 
 * 
 * Requires a html layout like this:
 * ---------------------------------
 * <div id="tabmenu">
 *   <ul>
 *     <li class="active"><a href="#tabid1">tab1</a></li>
 *     <li><a href="#tabid2">tab2</a></li>
 *   </ul>
 *   <div id="tabid1">
 *     <p>tab 1 content</p>
 *   </div>
 *   <div id="tabid2">
 *     <p>tab 2 content</p>
 *   </div>
 * </div>
 * 
 * 
 * Notes:
 * ------
 * Parent div can be any jQuery identifier, no need for an ID.
 * The <li class="active"> determins the default active tab
 * 
 * 
 * @copyright 2008 info.nl
 * @author jorn
 */
(function($){
  $.fn.infoTabs = function(opts){
    opts = $.extend({
      activeClass: 'active',
      tabContentClass: 'tabcontent',
      cancelLinkAction: false
    }, opts || {});
    
    // Fixed variables
    var tabParent = $(this);
    
    // Return the jQuery object for chaining. In this context 
    // tabParent equals this, but for clarity the variable name is used
    return init();
    
    
    /**
     * Initialises the tabs, hides all but the active tab.
     * 
     * This function also looks at the url, so it is possible to deeplink to
     * one of the tabs.
     * Order of active tab preference:
     * 1. url
     * 2. class
     * 3. default (= first tab)
     * 
     * @return [object] The tabParent jQuery object
     */
    function init(){
      var activeId;
      
      if( location.href.lastIndexOf('#') > 0 ){
	      var urlTab = location.href.substr(location.href.lastIndexOf('#'));
        
        tabParent.find('ul:first li a').each(function(){
          if( $(this).attr('href') == urlTab ){
            activeId = urlTab;
            swapTabs(tabParent.find('li.'+ opts.activeClass), tabParent.find('ul li a[href='+ urlTab +']').parent());
            return false; // break;
          }
        });
      }
      
      if( !activeId ){
        // Search for the active tab. If none is set, default to the first
        if (tabParent.find('ul:first li.' + opts.activeClass).is('li')) {
          activeId = tabParent.find('ul li.' + opts.activeClass + ' a').attr('href');
        }
        else {
          activeId = tabParent.find('ul li:first-child a').attr('href');
          tabParent.find('ul:first li:first-child').addClass(opts.activeClass);
        }
      }
      
      tabParent.find('div.'+opts.tabContentClass).hide(); // Hide none-active tabs
      tabParent.find(activeId).show(); // Make sure the right content div is shown, in case it was hidden by css
      
      // Loop over tab links and call the init function for each
      // The unbind method is used to avoid click conflict when the plugin is called more than once
      tabParent.find('ul:first li a').unbind('click').click(tabAction);
      
  		return tabParent;
    }
    
    
    /**
     * Initialises the tab links.
     * In this context, this corresponds to the link clicked
     */
    function tabAction(){
      var newTab = $(this).attr('href');
      swapTabs(tabParent.find('li.'+ opts.activeClass +':first'), $(this).parent());
      swapContent(tabParent.find('.'+ opts.tabContentClass +':visible'), $(newTab));
      
      // Check if the default link action should be stopped
      if( opts.cancelLinkAction )
        return false;
    }
    
    /**
     * Swaps the active state of tabs.
     * 
     * @param {Object} oldTab
     * @param {Object} newTab
     */
    function swapTabs(oldTab, newTab){
      oldTab.removeClass(opts.activeClass);
      newTab.addClass(opts.activeClass);
    }
    
    /**
     * Swaps the visible content of the tabs.
     * 
     * @param {Object} oldContent
     * @param {Object} newContent
     */
    function swapContent(oldContent, newContent){
      oldContent.hide();
      newContent.show();
    }
  }
})(jQuery);



