/**
 * Version 2.0 
 * Author: Rob Perez
 * Date: August 20, 2010
 * 
 * Usage:
 * 
 * 	Set a flag at the beginning of generateLeftNav to modify expansion behavior of the nav if desired.
 * 
 *  Somewhere prior to including the nav, set either noLeftNavExpansion or parentUrl var. 
 *  
  			<script type="text/javascript">
					
		-->					var noLeftNavExpansion = true; // really, this can be any value. We are just checking to see if it's defined at all. 
					
					In the above example, we don't want the left nav to expand. 
					Set that flag = left nav stays closed.
					
					OR
					
		-->						var parentUrl = "/ssl/ssl-information-center/";   // Trailing slash, please.
					
					In the above example, we are pretending the current page is a sub-category of the SSL Information Center.
					As such, the SSL Information Center category will be expanded and its children exposed.
					So, the current page will appear to "belong" under that sub-heading no matter where it actually lives.				
					
					Note that the nav will not expand at all if you just give it a "dummy" parentUrl that does not actually exist in the nav.
					However, you should use the noLeftNav expansion flag to accomplish this so your intent is clear to whomever shows up next to 
					modify this page.
					
				</script>
 * 
 * 
 * 
 */

generateLefNav = function(){
	generateLeftNav();
}

generateLeftNav = function(){

var hasParentUrl = false;	
var urlMatch = false;
	
	if (typeof (noLeftNavExpansion) != 'undefined'){
		return false; // the no left nav expansion flag has been set. No expansion. We are done.
	}	
		
	if (typeof (parentUrl) != 'undefined'){
		parentUrl = window.location.protocol + "//" + window.location.host + parentUrl;  // goes where you go, works where you work!
		hasParentUrl = true;  //there is a  parent url set. We'll attempt to expand the parent category of this page.
	}
	
	
	$("div#leftnav ul a").each(function(i){
		if (hasParentUrl) {
			if (this.href == parentUrl || this.href == parentUrl+"index.html") {
				$(this).parents("li:eq(0):not([class='main'])").addClass("activechild");
				return true; // a match to the  parent. Menu expanded.
			}
		}
		else 		
		var myLocation = "" + window.location;
			if (this.href == myLocation || this.href + "index.html" == myLocation) {
				if ($(this).parents().length > 1) {
					$(this).parents("li:eq(1):not([class='main'])").addClass("activechild");
				}
				$(this).parents("li:eq(0):not([class='main'])").addClass("active");
				$(this).css("cursor", "text");
				$(this).css("textDecoration", "none");
				$(this).click(function(){
					return false;
				});
				urlMatch = true;
				return true; // a match with this page's URL; menu expanded. And...we're done.
			}
	});
	if(hasParentUrl){
		return false;  // if we've tried to match a parent and failed, pull the ripcord to avoid lots of unwanted recursion.
	}
	
	if(!urlMatch){ // if there's no match for the current location, try to derive and set a parentUrl and go once more.
		parentUrl = deriveParent();
		generateLefNav();
	}
}

deriveParent = function(){
	pathArray = window.location.pathname.split( '/' );
	newPathname = "";
	for ( i = 0; i < pathArray.length-3; i++ ) {
		if (i != 0) {
			newPathname += "/";
		}
		newPathname += pathArray[i];
	}	
	return newPathname + "/";	
}

