/*
 * Simple tabs plugin
 *
 * @description
 * @author       Razvan Nastase <contact@bonsoftware.com>
 * @version      1.0
 * @created      2011/05/18 09:00:00
 * @modified     2011/05/18 09:00:00
 * @copyright    Copyright (c) 2009, Bon Media Software - All Rights Reserved
 *
 * No part of this document may be reproduced and you may not modify or use this
 * document for any other purposes (including other projects) without written
 * consent from the author. Please see http://www.bonsoftware.com/license for
 * more information.
 */

(function($) {
    var methods = {
        init: function(options) {
            return this.each(function(){
                var $this = $(this);
                var data = $this.data('stabs');

                if (!data) {
                    $(this).data('stabs', {
                        target: $this,
                        options: options
                    });
                }

                build($this);
            });
        }
    };

    $.fn.stabs = function(method) {
        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.stabs');
          return null;
        }
    };

    function build($this) {
        $this.addClass("stabs");

        $this.each(function() {
           var $el = $(this);

           $(this).find("ul li a").unbind('click.stabs');
           $(this).find("ul li a").bind('click.stabs', function() {
               $el.find(".tab").hide();
               $($(this).attr('href')).show();
               $el.find("ul li").removeClass('active');
               $(this).parent().addClass('active');

               return false;
           });

           $(this).find(".tab").hide();

           $($(this).find("ul li.active a").attr('href')).show();
        });
    }
})(jQuery);
