(function ($) {
	
    $.tabbed = function (el, options) {
        var self = this,
			node = $(el),
			tabs = $('li a', node);

		node.data('tabbed', this);

		/**
		 *	Initializing
		 */
        this.init = function () {
            this.options = $.extend({}, $.tabbed.defaultOptions, options);
			
			this.active = 0;
			var elements = '';
			this.contents = new Array;
			
			tabs.each(function () {
				elements += '#' + $(this).attr('href') + ', ';

				if ($(this).parent().is('.active')) {
					self.active = $(this).parent().index();
				}
			});

			self.contents = $(elements);
			
			tabs.eq(0).addClass('first');
			tabs.eq(this.active).parent().addClass('active');
			this.contents.eq(this.active).show();
			
			tabs.each(function (i) { $(this).data('i', i); }).click(this._click);
        };
		
		this._click = function (e) {
			var index = $(this).data('i');
			
			tabs.parent().removeClass('active').eq(index).addClass('active');
			self.contents.hide();
			self.contents.eq(index).show();
			
			e.preventDefault();
		};

        this.init();
    };

    $.tabbed.defaultOptions = {
		active: 0
    };

    $.fn.tabbed = function (options) {
        return this.each(function () {
            (new $.tabbed(this, options));
        });
    };

})(jQuery);
