/**
 * cb-notebook
 *
 * Author:     Johannes Wüller
 * Created On: 29.01.2010
 *
 * Usage:
 *    this is the markup used for cb-notebook:
 *
 *       <div class="__cb-nb">
 *          <div class="__cb-nb-tabs">
 *             <div class="__cb-nb-tab __cb-nb-tab-1">Tab #1</div>
 *             <div class="__cb-nb-tab __cb-nb-tab-2">Tab #2</div>
 *          </div>
 *          <div class="__cb-nb-contents">
 *             <div class="__cb-nb-content __cb-nb-content-1">Content #1</div>
 *             <div class="__cb-nb-content __cb-nb-content-2">Content #2</div>
 *          </div>
 *       </div>
 *
 *    tabs are enumerated by the __cb-nb-tab-x class where x is a unique
 *    identifier (unique in this notebook).  this identifier is automatically
 *    matched against __cb-nb-content-x and displays the matched element when
 *    the tab is clicked.
 *
 *    for activating notebook simply add the following line to your document
 *    ready event:
 *
 *       $.cbNotebook();
 *
 *    you can use this call to realign the measurements after initialization
 *    manually.
 *
 *    the first tab element is active by default.  you can change this behaviour
 *    by adding __cb-nb-default to the tab you want to be active by default.
 */
(function(){
   var initialized = false;
   jQuery.cbNotebook = function() {
      jQuery('.__cb-nb').each(function() {
         var notebook = jQuery(this);
         var tabs = notebook.children('.__cb-nb-tabs').children('.__cb-nb-tab');
         if (!initialized) {
            tabs.after('<div class="__cb-nb-separator"></div>').parent().children('.__cb-nb-separator:last').remove();
         }
         var separators = notebook.children('.__cb-nb-tabs').children('.__cb-nb-separator');
         var container_width = notebook.children('.__cb-nb-tabs').width();
         var separators_width = separators.length * 13;
         var tab_width = parseInt((container_width - separators_width) / tabs.length);
         tabs.width(tab_width);
         if (tab_width * tabs.length + separators_width < container_width) {
            tabs.eq(0).width(tabs.eq(0).width() + 1);
         }
         var default_tab = true;
         if (!initialized) {
            tabs.click(function() {
               notebook.children('.__cb-nb-tabs').children().removeClass('__cb-nb-active __cb-nb-active-right __cb-nb-active-left');
               jQuery(this).prev().addClass('__cb-nb-active-left');
               jQuery(this).next().addClass('__cb-nb-active-right');
               notebook.children('.__cb-nb-contents').children().hide().parent().children('.__cb-nb-content-'+jQuery(this).addClass('__cb-nb-active').attr('class').replace(/.*tab\-(\w+?).*/, '$1')).show();
            }).each(function() {
               if (jQuery(this).hasClass('__cb-nb-default')) {
                  default_tab = false;
                  jQuery(this).click();
               }
            });
            if (default_tab) {
               tabs.eq(0).click();
            }
         }
      });
      initialized = true;
   };
}());

