String.prototype.replaceAll = function(pcFrom, pcTo){	
var i = this.indexOf(pcFrom);
    var c = this;
 	
    while (i > -1){
      	c = c.replace(pcFrom, pcTo);
  		i = c.indexOf(pcFrom);
  	}
      return c;
}

String.prototype.beginsWith = function(t, i) { if (i==false) { return
	(t == this.substring(0, t.length)); } else { return (t.toLowerCase()
	== this.substring(0, t.length).toLowerCase()); } }


function navTo(node){
	window.location.href= "/web/"+jQuery(node).attr("id").replaceAll("_","/");
}

function currentPage(curPages){
	//clean off any unwanted URL garbage
	for (i=0;i<curPages.length;i++){
		
		if(curPages[i].beginsWith("#", true)==true  || ''+curPages[i] == ""){
			curPages.splice(i, 1);
		}else if(curPages[i].indexOf("#") >-1){
			curPages[i] = curPages[i].substring(0,curPages[i].indexOf("#"));
		}
	}
	
	//render current url into id
	var uriPath = "";
	for (i=0;i<curPages.length;i++){
		curval = curPages[i].replaceAll("#","");
		
		if(i <curPages.length-1){
			uriPath += curval +"_";
		}else{
			uriPath +=curval;
		}
	}
	
	return jQuery('#'+uriPath);
}

function getURI(currLoc){
	if(currLoc.indexOf("#") >-1){
		return currLoc.substring(currLoc.indexOf("web/")+4,currLoc.indexOf("#"));
	}else if(currLoc.indexOf("?") >-1){
		return currLoc.substring(currLoc.indexOf("web/")+4,currLoc.indexOf("?"));
	}else{
		return currLoc.substring(currLoc.indexOf("web/")+4,currLoc.length);
	}
}

function expandHeaders(curPages){
	var prodHdr = jQuery('#'+"products").parent();
	jQuery(prodHdr).next("div.menu_body").slideDown(100);
	jQuery(prodHdr).css('border-bottom', '1px solid #ddd9c6');
	var toolHdr = jQuery('#'+"tools").parent();
	jQuery(toolHdr).next("div.menu_body").slideDown(100);
	jQuery(toolHdr).css('border-bottom', '1px solid #ddd9c6');
}

function initMenus() {
	try{
	
	var currURI = getURI(window.location+"");
	
	//decorate the root header
	var curPages =currURI.replaceAll("/","_").split("_",3);
	
	var rootNode;
	if(curPages[0] == "info"){
		rootNode = jQuery('#'+curPages[0]+"_"+curPages[1]).parent();
	}else{
		rootNode = jQuery('#'+curPages[0]).parent();
	}

	var currPage = currentPage(curPages);

	//changing of quick guides temp... need to refactor
	if(rootNode.length > 0 && curPages[0] != 'guest' && rootNode.attr("class")!= "menu_block" && currPage.attr("id")!= "products_quick-guides"){
		//obtain the root and style
		jQuery(rootNode).next("div.menu_body").slideToggle(100).siblings("div.menu_body").slideUp("normal");
		jQuery(rootNode).css('background','');
		//remove down arrows until accordian feature come back
		//jQuery(rootNode).css('background-image',"url('/portlet-mqmenu/images/Arrow1-downBrown.png')");
		jQuery(rootNode).css('background-repeat','no-repeat');
		jQuery(rootNode).css('background-position', 'center');
		jQuery(rootNode).css('background-position', 'left');
		jQuery(rootNode).css('background-position','13px 10px');
		jQuery(rootNode).css('border-bottom', '1px solid #ddd9c6');
		
		//omit following 3 lines
		//jQuery(rootNode).css('background-color','#f9f7ef');
		//var rootAnchor = jQuery(rootNode).children();
		//jQuery(rootAnchor[0]).css('background-color','#f9f7ef');
		
		//change background for current node	
		if(jQuery(currPage).parent().attr("class")== "menu_head" ){
			//clear then set
			jQuery(currPage).css('background','');
			jQuery(currPage).css('background-color','#f9f7ef');	
			jQuery(currPage).parent().css('background-color','#f9f7ef');
		}else if(jQuery(currPage).attr("class")!= "menu_head"){
			//clear then set
			jQuery(currPage).css('background','');
			jQuery(currPage).css('color','#ffffff');
			jQuery(currPage).css('background-color','#77BAD2');	
		}
		//look for children
		var children = jQuery(currPage).next("div.sub_menu_body").children();
		
		//if has children style
		if(children.length > 0){
			jQuery.each(jQuery(children), function(){
				//clear then set
				jQuery(this).css('background','');
				jQuery(this).css('color', '#434343');
				jQuery(this).css('background','#d7eef4');			
			});
		}
		
		//expand the node
		jQuery(currPage).next("div.sub_menu_body").slideToggle(100).siblings("div.sub_menu_body").slideUp("normal");
	
		//change the background for all the siblings
		if(jQuery(currPage).attr("class")== "menu_link"){
			jQuery(currPage).siblings().css('color', '#434343');
			jQuery(currPage).siblings().css('background','#CDEBF6');
		}
	
		var currPageParent =currURI.replaceAll("/","_").split("_",2);
		if(currPageParent != null){
			var parentNode = jQuery('#'+currPageParent[0]+"_"+currPageParent[1]);
			if(parentNode.length > 0){
				
				if(jQuery(currPage).parent().attr("class")!= "menu_head"){
					//change parent if menu link
					if(jQuery(currPage).attr("class")== "menu_link"){
						jQuery(parentNode).css('color','#434343');
						jQuery(parentNode).css('background','#CDEBF6');
						jQuery(parentNode).css('background-image',"url('/portlet-mqmenu/images/Arrow2-downBrown.png')");
						jQuery(parentNode).css('background-repeat','no-repeat');
						jQuery(parentNode).css('background-position', 'center');
						jQuery(parentNode).css('background-position', 'left');
						jQuery(parentNode).css('background-position','25px 10px');		
					}else if(jQuery(currPage).attr("class")!= "sub_menu_head"){
						jQuery(parentNode).css('background-image',"url('/portlet-mqmenu/images/Arrow2-downWhite.png')");
						jQuery(parentNode).css('background-repeat','no-repeat');
						jQuery(parentNode).css('background-position', 'center');
						jQuery(parentNode).css('background-position', 'left');
						jQuery(parentNode).css('background-position','25px 10px');
					}
				}
			}
		}
		
		var parents =  jQuery(currPage).parents();
	
		jQuery.each(jQuery(parents), function(){
			if(jQuery(this).attr("class")== "sub_menu_body"){
				jQuery(this).slideToggle(100);
			}
		});
		
		expandHeaders(curPages);
		//mq js code removing css images need to replace
		if(jQuery(currPage).attr("id") == "tools_resources_lat-long-finder"){
			
			jQuery('#menupane a').each(function() {
				if(jQuery(this).attr("class")== "sub_menu_head_chld" && jQuery(this).attr("id")!= jQuery(parentNode).attr("id")){
					jQuery(this).css('background-image',"url('/portlet-mqmenu/images/Arrow2-rightBrown.png')");
					jQuery(parentNode).css('background-repeat','no-repeat');
					jQuery(parentNode).css('background-position', 'center');
					jQuery(parentNode).css('background-position', 'left');
					jQuery(parentNode).css('background-position','25px 10px');
				}
			});
		}
		
	}else if(jQuery('#'+currURI.replaceAll("/","_")).length > 0){
	
		//if current page is a menu block link with no children
		var curNode = jQuery('#'+currURI.replaceAll("/","_"));
		if(jQuery(curNode).parent().attr("class")== "menu_block"){
			jQuery(curNode).parent().css('background-color','#f9f7ef');
		}
		
		expandHeaders(curPages);
		
	}else{
		jQuery('#products').next("div.menu_body").slideToggle(100);
		jQuery('#products').css('border-bottom', '1px solid #ddd9c6');
		
		expandHeaders(curPages);
	}
	
	
	jQuery("#menupane div.menu_head").click(function()
	{
		var rootAnchor = jQuery(this).children();
		navTo(rootAnchor);
	});	
	
	jQuery("#menupane div.menu_block").click(function()
	{
		var rootAnchor = jQuery(this).children();
		navTo(rootAnchor);
	});	
	
	
	
	jQuery('#menupane a.sub_menu_head_chld').hover(function() {   
		var currPage = jQuery('#'+currURI.replaceAll("/","_"));
		if(jQuery(this).attr('id')==currPage.attr('id')){
			return;
		}else if(jQuery(this).css("background-image").indexOf("Arrow2-downBrown")>-1){
			jQuery(this).css('background-image',"url('/portlet-mqmenu/images/Arrow2-downWhite.png')");
		}
      }, function() {   
    	  if(jQuery(this).attr('id')==currPage.attr('id')){
  			return;
  		}else if(jQuery(this).css("background-image").indexOf("Arrow2-downWhite")>-1){
			jQuery(this).css('background-image',"url('/portlet-mqmenu/images/Arrow2-downBrown.png')");
		}
      });
	}catch(err){
		
	}
}
