// ------------------ FUNCTIONS ----------------------
// nextUntil() and wrapAll() courtesy John Resig: http://groups.google.com/group/jquery-en/browse_thread/thread/ace73bedf6741914/66c2fd1fdaadf67d?lnk=gst&q=%3C%2Fdiv%3E#66c2fd1fdaadf67d

	$.fn.nextUntil = function(expr) { 
	    var match = []; 
	    // We need to figure out which elements to push onto the array 
	    this.each(function(){ 
	        // Traverse through the sibling nodes 
	        for( var i = this.nextSibling; i; i = i.nextSibling ) { 
	            // Make sure that we're only dealing with elements 
	            if ( i.nodeType != 1 ) continue; 
	            // If we find a match then we need to stop 
	            if ( jQuery.filter( expr, [i] ).r.length ) break; 
	            // Otherwise, add it on to the stack 
	            match.push( i ); 
	        } 
	    }); 
	    return this.pushStack( match, arguments ); 
	};

	$.fn.wrapAll = function() { 
	    // There needs to be at least one matched element for this to work 
	    if ( !this.length ) return this; 
	    // Find the element that we're wrapping with 
	    var b = jQuery.clean(arguments)[0]; 
	    // Make sure that its in the right position in the DOM 
	    this[0].parentNode.insertBefore( b, this[0] ); 
	    // Find its lowest point 
	    while ( b.firstChild ) b = b.firstChild; 
	    // And add all the elements there 
	    return this.appendTo(b); 
	}; 

$(document).ready(function() {
// ------------------ NAVIGATION HIDE ----------------------
	$("#navbar h1").not($(".search")).addClass('plus').wrapInner("<a href=''></a>");
	$("#navbar ul").hide();
	$('#navbar h1').click(function(event){
		$(this).next('ul').toggle();
		$(this).toggleClass('minus');
		return false;
	});

// ------------------ TABS ----------------------
// Variables
	var navList = "";
	var headerID = 0;
	var id = 0;
//Generates the navigation for tabs from the text in the headers.
	$(".tabs h1").each(function(){
		headerID += 1;
		navList += String('<li><a href="' + '#' + headerID + '"><span>' + $(this).html() + '</span></a></li>')
	});
//Outputs the tab navigation after the #tabs
	$(".tabs").prepend('<ul>' + navList + '</ul>');

//Wraps DIV tags around the content that will be displayed in the tabs by finding the content between h1's	
	$(".tabs h1").each(function(){
		id += 1;
		$(this).nextUntil('h1').wrapAll('<div id="' + id + '"></div>')
	});
//Removes the H1's because they are unnecessary with the tabs
	$('.tabs h1').remove();

//Renders the tabs.
	$('.tabs > ul').tabs();
	
//Changes the Content Box so that it looks like the tabs are above the content box.
	if ($('#content').attr('class') == 'tabs') {
			$('#content').css('background-color', 'transparent').css('border', 'none').css('padding-top', '0px');
			$('.ui-tabs-panel').css('border', '1px solid #999');
			$('.ui-tabs-nav').css('border', 'none');
	};

// ------------------ SUBTABS ----------------------
// Variables
	var subNavList = "";
	var subHeaderID = 0;
	var subID = 0;
//Generates the navigation for tabs from the text in the headers.
	$(".subtabs h2").each(function(){
		subHeaderID += 1;
		subNavList += String('<li><a href="' + '#' + 's' + subHeaderID + '"><span>' + $(this).html() + '</span></a></li>')
	});
//Outputs the tab navigation after the #tabs
	$(".subtabs").prepend('<ul>' + subNavList + '</ul>');

//Wraps DIV tags around the content that will be displayed in the tabs by finding the content between h1's	
	$(".subtabs h2").each(function(){
		subID += 1;
		$(this).nextUntil('h2').wrapAll('<div id="' + 's' +  subID + '"></div>')
	});
//Removes the H1's because they are unnecessary with the tabs
	$('.subtabs h2').remove();

//Renders the tabs.
	$('.subtabs > ul').tabs();

// ------------------ TABLE ----------------------
	$('table tr:odd').addClass('odd');

// ----------------- MENUS --------------------


$(function() {
	$('ul.jd_menu').jdMenu('showDelay: 10');
});

	
	
});	//End Document Ready
