﻿

<!--
      /*
      *   autoTabs v.0.3
      *   mas@k2.pl
      */
(function($) {
   $.fn.autoTabs = function(eo, f) {
      // Tab constructor
      function Tab(cc) {
         this.handlers = new Array();
         this.contentContainer = cc;
         this.label = cc.attr("title");
         this.visible = function(b){
            if(b === true) {
               this.contentContainer.show();
            };
            if(b === false) {
               this.contentContainer.hide();
            };
            return this;
         };
         this.setHandler = function(h) {
            this.handlers.push(h);
         };
         this.getHandlers = function() {
            return this.handlers;
         };
         this.addReaction = function(e, f) {
            $(this.handlers).each(function(){
               $(this).bind(e, f)
            });
         };
      };
      // Tab group constructor
      function TabGroup() {
         this.tabs = new Array();
         this.addTab = function(tab) {
            var group = this;
            $(tab.handlers).each(function(){
               $(this).click(function(){
                  group.activateTab(tab);
               });
            });
            this.tabs.push(tab);
         };
         this.activateTab = function(tab) {
            $(this.tabs).each(function(){
               if(this == tab) {
                  $(this.visible(true).getHandlers()).each(function(){$(this).addClass("active")});
               } else {
                  $(this.visible(false).getHandlers()).each(function(){$(this).removeClass("active")});
               };
            });
            return tab;
         };
      };
      if($.isFunction(eo)){
         f = eo;
         eo = null;
      }else if(!$.isFunction(f)){
         f = function(){};
      };
      // extend defaults by explicitly declared options
      var options = $.extend({}, $.fn.autoTabs.defaults, eo);            
      
      //size of selection
      var length = this.length;
      return this.each(function(index){
         // save scope
         var $this = $(this);
         // support for the meta plugin
         var o = $.meta ? $.extend({}, options, $this.data()) : options;
         // create DOM object representing tab group
         var $container = o.container.clone().attr('class', o.className);
         // construct tab and tab group objects
         $this.t = new TabGroup();
         var tabCount = $this.children().length;
         $this.children().each(function(i){
            var thisTab = new Tab($(this));
            // create DOM object representing tab
            var DOMtab = o.tab.clone().css(o.css);
            if(i == 0) DOMtab.addClass("first");
            if(i+1 == tabCount) DOMtab.addClass("last");
            // find elements with no children, select the first of them, and set the text for it
            DOMtab.find("*").andSelf().filter(function(){
               return $(this).children().length == 0;
            }).filter(function(i){
               return i == 0;
            }).text(thisTab.label || o.label + " " + (i + 1));
            thisTab.setHandler(DOMtab);
            $this.t.addTab(thisTab);
            DOMtab.appendTo($container);
         });
         $container.prependTo($this);
         $this.t.activateTab($this.t.tabs[o.active]);
         
         // execute callback only once, at the end of the last iteration
         if(index+1 == length)
            f();
      });
   };
   $.fn.autoTabs.defaults = {
      className: "tabs",
      active: 0,
      container: $("<ul></ul>"),
      tab: $("<li class='tab'></li>"),
      label: "Tab",
      css: {
         cursor: "pointer"
      }
   };
})(jQuery)

$(document).ready(function() {
 
  $("#navTabs").autoTabs({
   id: "tabberTop",
   tab: $('<li class="tab"></li>'),
   css: {
    cursor: "pointer"
   }
  });
  
  var tab = 1;
  $("#navTabs").addClass("tab1");
  $("#navTabs li").click(function() {
   $("#navTabs").removeClass();
   var ind = $(this).prevAll().length + 1;
   $("#navTabs").addClass("tab"+ind);
  });
  
 });
//-->

