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());
	}
});

