/* 
----------------------------------
jQuery Minimal Tabify:
----------------------------------
Set up tabs from the children of the selected element, with no bells or whistles. 
Each Child (Tab) needs its own ID
Author:Ben Hull
This Version: 0.1, Mar 1, 2010

*/

(function($) {

    $.fn.tabify = function(options) {
        debug(this);

        // build main options before element iteration
        var opts = $.extend({}, $.fn.tabify.defaults, options);
		  var container;
        // iterate and reformat each matched element
        return this.each(function() {
            container = $(this);	

            // build element specific options
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			
				$.fn.tabify.prepareTabs($(this), o);
				$.fn.tabify.createNavigation($(this), o);
				
        });
    };

    function debug($obj) {
        if (window.console && window.console.log)
        window.console.log('Tabifying: ' + $obj.size());
    };

	 $.fn.tabify.prepareTabs = function($tabGroup, o) {
		
		var firstTabSelected = false;
		
		$tabGroup
			.addClass(o.groupClasses)
			.bind('tabify.show', function(event, id){
				//Fix IE - which prepends the whole URL to the href
				if (id.split('#').length > 1) {
					id = '#' + id.split('#')[1]
				};
				$(id).addClass('tabPanelSelected').siblings().removeClass('tabPanelSelected');
			})
			.children()
				.each(function(i){
					var $this = $(this);
				
					$this.addClass(o.itemClasses);
					
					$this.find('h2:first, h3:first, h4:first, h5:first').addClass('tabPanelTitle');
					
					if (!$this.children(':not(.tabPanelTitle, :empty)').length) $this.addClass('empty');

				});
			$tabGroup.children(':not(.empty)').eq(0).addClass('tabPanelSelected');
    };

    $.fn.tabify.createNavigation = function($tabGroup, o) {
		var navCanvas = $('<ul class="' + o.navClasses + '"></ul>'),
			 firstTabSelected = false;
		
		$tabGroup
			.children().each(function(i){
				var addClass = $(this).is('.empty') ? ' empty' : '';
				var headingText = $(this).find('.tabPanelTitle').html(),
					 navItem = $('<li class="' + o.navItemClasses + addClass + '"><a href="#' + $(this).attr('id') + '"><span>' + headingText + '</span></a></li>')
					
					 navItem.find('a')
						.bind('click, focus', function(){
							var $this = $(this);
							//Select this item
							$this.closest('li').addClass('selected').siblings().removeClass('selected');
							
							//Show the corresponding tab:
							$tabGroup.trigger('tabify.show', [$(this).attr('href')]);
							return false;
						});
						
				navCanvas.append(navItem);
			});
			
		if ($tabGroup.children().length > 1) { //Only show the nav if there is more than one tab.
			$tabGroup.prepend(navCanvas).find('.' + o.navClasses + ' > li:not(.empty)').eq(0).addClass('selected');
		}
    };

    $.fn.tabify.defaults = {
        groupClasses: 'tabGroup',
        itemClasses: 'tabPanel',
		  navClasses: 'tabNavigation',
		  navItemClasses: 'tab'
    };


})(jQuery);