/*
    @author Jacob Overgaard <jo at smartweb dot dk>
    @company SmartWeb ApS
    @file jquery.SimpleTabs.js
    
    @name Simple Tabs
    @version 0.1b
*/

(function($) {
    
    var settings = {
        onBeforeLoad: function(){},
        onLoad: function(){},
        onBeforeChange: function(){},
        onChange: function(){},
        onBeforeDestroy: function(){}
    };
    
    var methods = {
        
        init: function(options) {
            return this.each(function() {
        
                if (options) {
                    $.extend(settings, options);
                }
                
                // Onbeforeload callback
                settings.onBeforeLoad.call(this, $(this));
                
                var $this = $(this);
                var tabs = $('>li[data-tab]', $this);
                var firsttime = true;

                // Run through all tabs, perform various tasks
                tabs.each(function() {
                    
                    // Content element for this tab
                    var content_el = $( $(this).data('tab') );

                    // Hide all non-active content areas
                    if (firsttime && $(this).hasClass('active')) {
                        content_el.show();
                        firsttime = false;
                    } else {
                        $(this).removeClass('active');
                        content_el.hide();
                    }
                    
                    // Bind click event to the tab
                    $(this).click(function() {
                        // Disable all tabs
                        methods.destroyTabs(tabs);
                        // Enable the selected tab
                        methods.enable( $(this) );
                    });
                });

                // If there are no active classes, make the first tab active
                if (firsttime && tabs.length > 0) {
                    var firsttab = $( tabs[0] );
                    firsttab.addClass('active');
                    $( firsttab.data('tab') ).show();
                }
                
                // Onload callback
                settings.onLoad.call(this, $this);
            });
        },
        
        enable: function(tab) {
            // Enable a single tab
            tab.addClass('active');
            settings.onBeforeChange.call(this, tab, tab.data('tab'));
            $( tab.data('tab') ).show();
            settings.onChange.call(this, tab, tab.data('tab'));
        },
        
        destroyTabs: function(tabs) {
            // Loop through all tabs
            tabs.each(function() {
                $(this).removeClass('active');
                $( $(this).data('tab') ).hide();
            });
        },
        
        destroy: function() {
            return this.each(function() {
                settings.onBeforeDestroy.call(this, $(this));
                var $this = $(this);
                var tabs = $('>li[data-tab]', $this);
                tabs.each(function() {
                    $(this).removeClass('active').unbind('click');
                    $( $(this).data('tab') ).show();
                });
            });
        }
    };
    
    $.fn.SimpleTabs = function(method, options) {
        if ( methods[method] ) {
          return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
          return methods.init.apply( this, arguments );
        } else {
          $.error( 'Method ' +  method + ' does not exist on jQuery.SimpleTabs' );
        }
    }
    
})(jQuery);
