/* public-utils.js */

function promoteSubMenu () {
	var $activeSubMenu = $(".content-left-nav .menu .root-level-list .root-level-item.active > ul");	// ul:has(li.active)");
	
	// original selector: ".content-left-nav .menu .root-level-list .root-level-item.active ul:has(li.active)"
	//		- the li.active is redundant; the root-level-item.active is sufficient
	//		- ommission of the "> ul" meant TWO ul's were matching the selector:
	//			- the ul that immediately contains the li.active, ul.level-2-list
	//			- the top-level ul that contains the inner one, ul.level-1-list
		
	if ($activeSubMenu.length) {

	// $(".content-left-nav .menu ul.root-level-list").replaceWith($activeSubMenu.html());
	// that original code replaces ul.root-level-list with only the li's from
	// the $activeSubMenu, resulting in div.menu > li, with no containing ul!

		// but the existing CSS apparently relied on those promoted li's NOT being within
		// their 'regular' ul with the 'regular' classes in the menu hierarchy
		$activeSubMenu.attr('class', 'promoted-menu-list'); 
//		$activeSubMenu.addClass('promoted-menu-list'); 		this breaks the existing ECP CSS

		$(".content-left-nav .menu ul.root-level-list").replaceWith($activeSubMenu);
		if (typeof OCMS !== 'undefined') {
			$activeSubMenu.each(function () {
				OCMS.consoleMsg($(this).parent().find('a').eq(0).text() + ' is the new 1st item in the promoted menu');
			});
		}
	} else {
		if ($('.content-left-nav').length && typeof OCMS !== 'undefined') {
			OCMS.consoleMsg('left sidebar menu did not have a sub-menu marked as active');
		}
	}
}

$(document).ready(promoteSubMenu);
