;(function($){
    $.fn.tabs = function(tabOptions) {
        // support mutltiple elements
        if (this.length > 1){
            this.each(function() { $(this).tabs(tabOptions) });
            return this;
        }

        // SETUP private variabls;
        var tabs = this;


        var baseOptions = {
            tabsClass: '.tabList li',
            tabsSelected: '.selected',
            tabPanels: '.tab',
            tabPrefix: '.tab',
            debug: false
        };

        // SETUP private functions;
        var callbacks = function(thisObject, functionList) {
            var returnValue = true;
            if ($.isFunction(functionList)){
                returnValue = functionList.call(thisObject);
            }else if ($.isArray(functionList)){
                for (var i = 0; i < functionList.length; i++){
                    returnValue = functionList[i].call(thisObject);
                }
            }
            return returnValue;
        };

        // setup options
        var options = $.extend({}, baseOptions, tabOptions);
        
        var intialize = function() {
                // support MetaData plugin
                if ($.meta){
                    options = $.extend({}, options, this.data());
                }

                // Scope selectors
                $(options.tabsClass, tabs).click( function(e){
                                                                tabs.changeTab(this);
                                                            });
                tabs.changeTab($(options.tabsSelected, tabs).get(0));
        };

        this.changeTab = function(element) {
              var newTabIndex = 0;
              $(options.tabsClass, this).each(function(){
                   if (this == element) {
                        tabs.currentTab = newTabIndex;
                        $(element).addClass(options.tabsSelected);
                   } else {
                        $(this).removeClass(options.tabsSelected);
                   }
                   newTabIndex++;
              });

              var tabOptions = {};
              if (options['tab' + tabs.currentTab] != undefined) {
                   tabOptions = options['tab' + tabs.currentTab];
              }

              if (callbacks(this, tabOptions.beforeLoad)){
                  $(options.tabPanels, this).hide();
                  callbacks(this, tabOptions.onload);
                  $('.tab' + tabs.currentTab, this).show();
                  callbacks(this, tabOptions.afterload);
              }
        };

         
         /**
          * Debug Logging
          * @param {Object} data
          */
         var log = function(data){
              if (options.debug) {
                   try {
                        console.log("%o: %o", data, this);
                   } catch (e) {
                        $('BODY').append($("<div class='error'>" + data + "</div>"));
                   }
              }
         };

         this.getCurrentTab = function() {
            return this.currentTab || 0;
         }

         intialize();
         
        return this;
    }
})(jQuery);