/*
ahaMenuSelection
by John Kepler (jkepler@aha.org)
dependent on behavior.js
	// Looking for a ul element that matches the path of the current page in order to expand it
	// CSS ref might look like: body#issues .SideNav .subnav .issues_subnav .issues_awards_and_recognition_level2
	// Process works something like this
	// 1. Navigation is created by user
	// 2. Templating adds a classname to each UL element that matches name entered by user
	//		"Awards and Recognition" inside "Issues" becomes ".issues_awards_and_recognition_level2"
	//		The characters " " and "-" are converted to underscores
	//		and the characters ",'\"" are deleted
	// 3. Appropriate navigation element is included in an HTML page when it is generated
	// 4. This function is called onload, using behavior.js addLoadEvent, 
	//      compares path and classes using behaviour.js getElementsBySelector and expands the  UL that matches
*/
ahaMenuSelection = function() {
	var myUrl = "";
	var myOrigUrl = "";
	var myCurrentPage = "";
	var elements = "";
	// Get current URL
	// Look for Meta Name HTMLPath first and use if it exists (written to page in templating)
	if (document.getElementsByName) {
		var metaArray = document.getElementsByName('HTMLPath');
			if (metaArray.length > 0) {
				myUrl = metaArray[0].content;
			}
	}
	// If myUrl still not populated try using current URL
	if (myUrl.length == 0) {
		myUrl = document.URL;
		myUrl=myUrl.replace("http://","");
		// remove Interwoven virtualization from URL if it exists
		if (myUrl.match = "iw") {
			myUrl=myUrl.replace(new RegExp("iw/cci/.*WORKAREA/[^/]*/"), "");
			myUrl=myUrl.replace(new RegExp("iw-mount/.*WORKAREA/[^/]*/"), "");
			myUrl=myUrl.replace(new RegExp("iw/cci/.*STAGING/"), "");
			myUrl=myUrl.replace(new RegExp("iw-mount/.*STAGING/"), "");
			myUrl=myUrl.replace(new RegExp("iw/cci/.*EDITION/[^/]*/"), "");
			myUrl=myUrl.replace(new RegExp("iw-mount/.*EDITION/[^/]*/"), "");
		}	
	}
	myOrigUrl=myUrl;
	// manual fix for folders - the names of the menu items must match the folder names
	// non matching names could be put here. Should not have to do this.
	myUrl=myUrl.replace("/awards/","/awards-and-recognition/");
	myUrl=myUrl.replace("/legal/","/legal-resource-library/");
	
	var myUrlArray=myUrl.split("/");
	//var myOrigUrlArray=myOrigUrl.split("/");
	
	if ((!document.body.className.match("^inside_final_page$")) && (!document.body.className.match("^main_home$"))) {
		// Only go further if #group5 (left red navigation) is displayed
		// 	end of line content (body.inside_final_page) does not display this menu
		// Find element and expand.  Even though we are searching by classname, there should
		// 	only be one element that matches this selector	
		// construct class name to look for
		// if page is more then two levels of folders don't worry - we only care about the 1st two
		// that is all that the left navigation is designed to handle
		var myLevelOneClassName =  nameToCssId(myUrlArray[2]) + "_" +  nameToCssId(myUrlArray[3]) + "_level1";
		var myLevelTwoClassName =  nameToCssId(myUrlArray[2]) + "_" +  nameToCssId(myUrlArray[3]) + "_level2";
		// Ignore case
		myLevelOneClassName =  "." + myLevelOneClassName.toLowerCase();
		myLevelTwoClassName =  "." + myLevelTwoClassName.toLowerCase();
		myLevelOneClassName =  "#SideNav " + myLevelOneClassName;
		myLevelTwoClassName =  "#SideNav " + myLevelTwoClassName;
		
		// Expand Block we are in
		elements = document.getElementsBySelector(myLevelTwoClassName)
		var subelements;
		for (var i=0; i<elements.length; i++) {
			elements[i].style.display="block";
		}
		// Highlight page we are on
		elements = document.getElementsBySelector("#SideNav .subnav ul");
		highlightelem : for (var i=0; i<elements.length; i++) {
			subelements = elements[i].getElementsByTagName("a");
			if (subelements) {
				for (var i=0; i<subelements.length; i++) {
					var thisURL = subelements[i].getAttribute("href");
					thisURL=thisURL.replace("http://","");
					// remove Interwoven virtualization from URL if it exists
					if (thisURL.match = "iw") {
						thisURL=thisURL.replace(new RegExp("iw/cci/.*WORKAREA/[^/]*/"), "");
						thisURL=thisURL.replace(new RegExp("iw-mount/.*WORKAREA/[^/]*/"), "");
						thisURL=thisURL.replace(new RegExp("iw/cci/.*STAGING/"), "");
						thisURL=thisURL.replace(new RegExp("iw-mount/.*STAGING/"), "");
						thisURL=thisURL.replace(new RegExp("iw/cci/.*EDITION/[^/]*/"), "");
						thisURL=thisURL.replace(new RegExp("iw-mount/.*EDITION/[^/]*/"), "");
					}
					thisURL=thisURL.replace("^.*/aha/","/aha/");
					thisURL=thisURL.replace("^.*/aha_app/","/aha_app/");
					thisURL=thisURL.replace("ahacontent01.aha.cec.qwest.net","");
					thisURL=thisURL.replace("content.hospitalconnect.com","");
					thisURL=thisURL.replace("(ahacontent.com","");
					thisURL=thisURL.replace("(ahacontent.org","");
					thisURL=thisURL.replace("dev.aha.org","");
					thisURL=thisURL.replace("staging.aha.org","");
					thisURL=thisURL.replace("www.aha.org","");
					thisURL=thisURL.replace("aha.org","");
					//var thisURLarr=thisURL.split("/");
						//alert("thisURL: " + thisURL + " - myOrigUrl: " + myOrigUrl);
					if (thisURL == myOrigUrl) {
						//alert("match");
						// Ideally we would change the classname, but it doesn't appear to work in IE
						// subelements[i].className="currPage";
						// Manually change style - sigh
						subelements[i].style.color="#000";
						subelements[i].style.backgroundColor="#E4E8F1";
						subelements[i].style.border="1px #E4E8F1 solid";
						subelements[i].style.backgroundRepeat="no-repeat";
						subelements[i].style.backgroundPosition="3px center";
						if (subelements[i].parentNode.parentNode.className.match(/_level2$/)) {
							//alert(subelements[i].parentNode.parentNode.className);
							subelements[i].style.backgroundImage="url(/aha/images/bullets/black-arrow-on-lt-blue.gif)";
						}
						break highlightelem;
					}
				}
				var myOrigUrlArray=myOrigUrl.split("/");
				myOrigUrlArray.length=myOrigUrlArray.length-1;
				myOrigUrl=myOrigUrlArray.join("/");				
				subelements.reverse;
				for (var i=0; i<subelements.length; i++) {
					var thisURL = subelements[i].getAttribute("href");
					thisURL=thisURL.replace("http://","");
					// remove Interwoven virtualization from URL if it exists
					if (thisURL.match = "iw") {
						thisURL=thisURL.replace(new RegExp("iw/cci/.*WORKAREA/[^/]*/"), "");
						thisURL=thisURL.replace(new RegExp("iw-mount/.*WORKAREA/[^/]*/"), "");
						thisURL=thisURL.replace(new RegExp("iw/cci/.*STAGING/"), "");
						thisURL=thisURL.replace(new RegExp("iw-mount/.*STAGING/"), "");
						thisURL=thisURL.replace(new RegExp("iw/cci/.*EDITION/[^/]*/"), "");
						thisURL=thisURL.replace(new RegExp("iw-mount/.*EDITION/[^/]*/"), "");
					}
					thisURL=thisURL.replace("^.*/aha/","/aha/");
					thisURL=thisURL.replace("^.*/aha_app/","/aha_app/");
					thisURL=thisURL.replace("ahacontent01.aha.cec.qwest.net","");
					thisURL=thisURL.replace("dev.aha.org","");
					thisURL=thisURL.replace("staging.aha.org","");
					thisURL=thisURL.replace("www.aha.org","");
					thisURL=thisURL.replace("aha.org","");
					
					var thisUrlArray=thisURL.split("/");
					thisUrlArray.length=thisUrlArray.length-1;
					thisURL=thisUrlArray.join("/");
					
					//var debugtxt="thisURL: " + thisURL + " - myOrigUrl: " + myOrigUrl;
					//alert(debugtxt);
					if (thisURL == myOrigUrl) {
						//alert("match");
						// Ideally we would change the classname, but I can't get it to work in IE
						// subelements[i].className="currPage";
						// Manually change style - sigh
						subelements[i].style.color="#000";
						subelements[i].style.backgroundColor="#E4E8F1";
						subelements[i].style.border="1px #E4E8F1 solid";
						subelements[i].style.backgroundRepeat="no-repeat";
						subelements[i].style.backgroundPosition="3px center";
						if (subelements[i].parentNode.parentNode.className.match(/_level2$/)) {
							//alert(subelements[i].parentNode.parentNode.className);
							subelements[i].style.backgroundImage="url(/aha/images/bullets/black-arrow-on-lt-blue.gif)";
						}
						break highlightelem;
					}					
				}
				
				
				
			}
		}
	}
	
	
}
Behaviour.addLoadEvent(ahaMenuSelection);
