var $j = jQuery.noConflict();

$j(document).ready(function() {
	/* Table striping */
	$j("tr:nth-child(even)").addClass("odd");

	/* Menu */
	var menuId = '#engrmenu ';
	var menuClass = 'img.emenu';
	var openImage = '<img class="emenu" alt="-" src="/ui/images/minus.gif">';
	var closedImage = '<img class="emenu" alt="+" src="/ui/images/plus.gif">';

	var setup = function(items) {
		items.children('ul').show();
		items.children(menuClass).replaceWith(openImage);
		$j(menuId + menuClass).click(function() { toggle($j(this)); });
	}

	var toggle = function(item) {
		if ( item.parent().children('ul').is(':hidden') ) {
			item.parent().children('ul').show(); 
			item.replaceWith( openImage );
		} else {
			item.parent().children('ul').hide(); 
			item.replaceWith(closedImage);
		} 
		$j(menuId + menuClass).click(function() { toggle($j(this)); }); 
	};

	/* The setup, URL agnostic */
	$j(menuId + 'li ul').hide(); 
	$j(menuId + 'li ul').parent().toggleClass('parent');
	$j(menuId + 'li ul').parent().prepend(closedImage);
	$j(menuId + menuClass).click(function() { toggle($j(this)); });

	/* Expand current url in menu. */
	var path = location.pathname + "/";
	setup($j(menuId + "a[href='" + path + "']").parents());
	
	for ( i = 0 ; i <= path.split("/").length; i++ ) {
		path = path.replace(/(\w|[-.])+$/, "");
		setup($j(menuId + "a[href='" + path + "']").parents());
		path = path.replace(/\/$/, "");
		setup($j(menuId + "a[href='" + path + "']").parents());
	}
	
    //Setup tab nav to highlight current
    var tabID = '#engrtabnav';
    var path = location.pathname;
    var tabs = $j(tabID);
    $j(tabs).find("a[href='" + path + "']").parents("li").each(function() {
		$j(this).addClass("here");
    });

    // Search swoop nav against pathname and breadcrumbs
	// to highlight the current area
	var swoopID = "#engr_swoop_nav";
	var crumbID = "#breadcrumb";
	var path = location.pathname;
	var swoop = $j(swoopID);
	var crumb = $j(crumbID);

	$j(swoop).find("a").each(function() {
		// Search against path
		if( path.indexOf($j(this).attr('href')) > -1 )
		{
			$j(this).addClass("here");
		}

	    // Search against crumbs
		var curr_swoop = this
		$j(crumb).find("a").each(function() {
			if( $j(this).attr('href')==$j(curr_swoop).attr('href') )
			{
				$j(curr_swoop).addClass("here");
			}
		});
	});

	// Because of legacy folder location we'll search the breadcrumbs
	// for a path that is in the swoop nav.  If it is, we'll
	// highlight the swoop nav link.
	
});

