(function ($) {
	
	$.tabs = function (options) {
		return $.tabs.impl.init(data, options);
	};
	
	$.fn.tabs = function (options) {
		return $.tabs.impl.init(this, options);
	};
	
	/*
	 * tabs default options
	 */
	$.tabs.defaults = {
	    defaultTab: null
	};
	
	$.tabs.impl = {
		
		/*
		 * tabs options
		 */
		opts: null,
		
		/*
		 * tabs helper
		 */
		helper: {},
		
		/*
		 * Initialize the tabs
		 */
		init: function (tabs, options) {
            
            var self = this;
            
            self.opts = $.extend({}, $.tabs.defaults, options);
            
            self.helper.container = $(tabs);
            self.helper.menu = $('<ul class="tab-menuitem-collection"></ul>');
            self.helper.content = $('<ul class="tab-contentitem-collection"></ul>');
            
            // create menu and content elements
            self.helper.container.children().each(function(i, item){
                
                var $menuItem = $('<li class="tab-menuitem"><a href="#">' + $(item).find('.tab-title').html() + '</a></li>');
                var $contentItem = $('<li class="tab-contentitem">' + $(item).find('.tab-content').html() + '</li>');
                
                // set first item as active
                if ($(item).hasClass('tab-' + self.opts.defaultTab))
                {
                    $menuItem
                        .addClass('tab-menuitem-active')
                        ;
                    $contentItem
                        .addClass('tab-contentitem-active')
                        ;
                }
                
                if (i == self.helper.container.children().length - 1 || self.helper.container.children().length == 1)
                {
                    $menuItem
                        .addClass('tab-menuitem-last')
                        ;
                }
                else if (i == 0)
                {
                    $menuItem
                        .addClass('tab-menuitem-first')
                        ;
                }
                
                // append items
                self.helper.menu.append($menuItem);
                self.helper.content.append($contentItem);
                
            });
            
            if (self.helper.menu.find('.tab-menuitem-active').length < 1)
            {
                $(self.helper.menu.children()[0])
                    .addClass('tab-menuitem-active')
                    ;
                
                $(self.helper.content.children()[0])
                    .addClass('tab-contentitem-active')
                    ;
            }
            
            // replace markup w/ new menu and content
            self.helper.container.replaceWith(self.helper.menu);
            self.helper.menu.after(self.helper.content);
            
            // bind events
            self.bindEvents();
            
			return self;
		},
		
		/*
		 * Bind events
		 */
		bindEvents: function () {
            
            var self = this;
            
            self.helper.menu.find('.tab-menuitem a').click(function(e){
                e.preventDefault();
                
                var $li = $(this).parent();
                
                if (!$li.hasClass('tab-menuitem-active'))
                {
                    $li.addClass('tab-menuitem-active').siblings()
                        .removeClass('tab-menuitem-active')
                        ;
                    
                    $(self.helper.content.find('.tab-contentitem')[$li.prevAll().length])
                        .addClass('tab-contentitem-active')
                        .siblings()
                        .removeClass('tab-contentitem-active')
                        ;
                }
            });
            
		    return;
		}
		
	};
})(jQuery);
