//CLG highlighting
var BC_CATALOGS_ON = 1;
var BC_BLOGS_ON = 2;

var BC_CUSTOM1_ON = 3;
//var CUSTOM1_REGEX = /BlogRetrieve/;
var CUSTOM1_REGEX = /index/;
var CUSTOM1_URL = "/index";


var ELEMENTFINDER_HIGHLIGHT = 4;
var ELEMENTID = "webappidentifier";
var ELEMENTCONTENT_URL = [
	//****Sydney - FOR THE HOME ****
	[/^Travel Stories/,"/travel-stories.htm"],[/^SPECIAL FEATURES/,"/special_features"], [/^BIKES/, "/bikereviews.htm"],
        [/^GEAR/,'/gearreviews.htm'], [/^Bikes/, '/cruiser_bikes.htm'], [/^Trikes/, '/cruiser_trikes.htm'], [/^Travel/,'/cruiser_travel.htm']


  ];

var DEFAULT_HIGHLIGHT = 0;


function checkActive(modeParam) {
	var und;
	// li array
	var a = document.getElementsByTagName("li");

	var loc = window.location.href;
	var i;

	//if modeParam undefined make it default
	


	if(!modeParam || modeParam.length == 0)
		modeParam = [DEFAULT_HIGHLIGHT];

/*
	//if _product found on url	
	if( loc.split("/_product_").length > 1 ){
		for(i =0; i < modeParam.length; i++){
			if(modeParam[i] == BC_CATALOGS_ON){
				BCProductpageHighlight();
				break;
			}
		}
		
		if( i == modeParam.length)
			defaultHighlight();	

	}
	//if /_blog/ found on url
	else if( loc.split("/_blog/").length > 1){
		for(i =0; i < modeParam.length; i++){
			if(modeParam[i] == BC_BLOGS_ON){
				BCblogSecondlayerHighlight();
				break;
			}
		}

		if( i == modeParam.length)
			defaultHighlight();	


	}
	//custom1 regex highlight
	else if(CUSTOM1_REGEX.test(loc)){

		for(i =0; i < modeParam.length; i++){
			if(modeParam[i] == BC_CUSTOM1_ON){
				BCCustom1Highlight(CUSTOM1_REGEX, CUSTOM1_URL);
				break;
			}
		}

		if( i == modeParam.length)
			defaultHighlight();	
		
	
	}
	else{
		
		defaultHighlight();	
	}
*/

var highlightdone = false;


	for(i =0; i < modeParam.length && !highlightdone; i++){
		//alert("mode: " + modeParam[i] + ", loc: " + loc);
		
		switch(modeParam[i]){
			case BC_CATALOGS_ON:
				if(loc.split("/_product_").length > 1){
					BCblogSecondlayerHighlight();
					highlightdone = true;
					break;
				}
			case BC_BLOGS_ON:
				if( loc.split("/_blog/").length > 1){
					BCblogSecondlayerHighlight();
					highlightdone = true;
					break;
				}
			case BC_CUSTOM1_ON:
				if(CUSTOM1_REGEX.test(loc)){
					BCCustom1Highlight(CUSTOM1_REGEX, CUSTOM1_URL);
					highlightdone = true;
					break;
				}
			case ELEMENTFINDER_HIGHLIGHT:
	
				//if highlight is successful
				if(ElementFinderHighlight(ELEMENTID, ELEMENTCONTENT_URL)){
					highlightdone = true;
					break;
				}
				//otherwise continue default highlight
	
			
			
			default:
				defaultHighlight();
				//highlightdone = true;
				
		
		}
	
	}

	

}



//Element finder
function ElementFinderHighlight(objid, contenturl_arr){

	var a = document.getElementsByTagName("li");
	var elcontainer = document.getElementById(objid);
	var highlightsuccess = false;
	var activateurl = "/";
	var j;
	var menuurl;	
	//first checking
	
	

	if(elcontainer && elcontainer.innerHTML.length > 0){
		
	
		//second checking if obj innerHTML matches regex on contenturl_arr[][0]
		for(j=0; j<contenturl_arr.length; j++){
			if(contenturl_arr[j][0].test(elcontainer.innerHTML) ){
				//menu item to activate
				menuurl = contenturl_arr[j][1];
				break;
			}
		}
		
		//3rd checking: if regex on contenturl_arr[] matches with objid.innerHTML
		if(j < contenturl_arr.length){ 

			for(var i=0; i < a.length; i++) {
	
				if(a[i].getElementsByTagName("a")[0] && a[i].getElementsByTagName("a")[0].href.indexOf(menuurl) >= 0 && 
					a[i].getElementsByTagName("a")[0].href.indexOf(menuurl) + menuurl.length == a[i].getElementsByTagName("a")[0].href.length){
			
					found = a[i];
					//activate link
					a[i].getElementsByTagName("a")[0].className = "activated";
					//if id of parent node of li contains 'sub_'
					//firstContainer struct: <ul> <li><a id=[parent menu label]></li></ul>
					// secondContainer struct: <ul id="sub_[parent menu label]> <li class=subLists_[submenu label] /> <li /> 
					if(a[i].parentNode.id.split("sub_").length > 1){
						//Warren 07/07 - update
						//change first level highlight condition based on li index
						if(a[i].parentNode.parentNode.id == "secondContainer"){ 				
							var firstindex = a[i].parentNode.id.split("sub_")[1];
							//	alert(firstindex + "  " + document.getElementById("firstContainer").getElementsByTagName("li").length);
							document.getElementById("firstContainer").getElementsByTagName("li")[firstindex].childNodes[0].className = "activated";
						}
		
						//make 2nd layer visible
						a[i].parentNode.style.display = "block";
					}
					
					//highlight successful
					highlightsuccess = true;
						
				}
	
			}
		
		
		}
	
	}
	
	return highlightsuccess;
}

//BC custom highlight 1
function BCCustom1Highlight(expr, menuurl){

	var a = document.getElementsByTagName("li");
	var loc = window.location.href;
    var urlval = loc.substring( loc.indexOf("/") );


//	alert(a[i].getElementsByTagName("a")[0].href.indexOf(menuurl) );

	for(var i=0; i < a.length; i++) {
	
		// if current link has occurrence of menuurl 
		if(a[i].getElementsByTagName("a")[0] && a[i].getElementsByTagName("a")[0].href.indexOf(menuurl) >= 0 && 
			a[i].getElementsByTagName("a")[0].href.indexOf(menuurl) + menuurl.length == a[i].getElementsByTagName("a")[0].href.length){
	
			found = a[i];
			//activate link
			a[i].getElementsByTagName("a")[0].className = "activated";
			//if id of parent node of li contains 'sub_'
			//firstContainer struct: <ul> <li><a id=[parent menu label]></li></ul>
			// secondContainer struct: <ul id="sub_[parent menu label]> <li class=subLists_[submenu label] /> <li /> 
			if(a[i].parentNode.id.split("sub_").length > 1){
				//Warren 07/07 - update
				//change first level highlight condition based on li index
				if(a[i].parentNode.parentNode.id == "secondContainer"){ 				
					var firstindex = a[i].parentNode.id.split("sub_")[1];
					//	alert(firstindex + "  " + document.getElementById("firstContainer").getElementsByTagName("li").length);
					document.getElementById("firstContainer").getElementsByTagName("li")[firstindex].childNodes[0].className = "activated";
				}

	
				//make 2nd layer visible
				a[i].parentNode.style.display = "block";
			}
				
		}

	}
	
	
}


//BC blogs on 2nd layer
function BCblogSecondlayerHighlight(){
	
	var und;
	// li array
	var a = document.getElementsByTagName("li");
	var loc = window.location.href;
	var found;
		
	//capture xxxx value from /_blog/xxxx/ on window.location value
	var fid = getSuffix(loc, "_blog/");
		
	//alert("fid: "+ fid);
	for(var i=0; i < a.length; i++) {

		// if current link has occurrence of /_blog/xxxx (fid) 
		if(a[i].getElementsByTagName("a")[0] && a[i].getElementsByTagName("a")[0].href.split("/_blog/" + fid).length > 1){

			found = a[i];
			//activate link
			a[i].getElementsByTagName("a")[0].className = "activated";
			//if id of parent node of li contains 'sub_'
			//firstContainer struct: <ul> <li><a id=[parent menu label]></li></ul>
			// secondContainer struct: <ul id="sub_[parent menu label]> <li class=subLists_[submenu label] /> <li /> 
			if(a[i].parentNode.id.split("sub_").length > 1){
				
				//Warren 07/07 - update
				//change first level highlight condition based on li index
				if(a[i].parentNode.parentNode.id == "secondContainer"){ 				
					var firstindex = a[i].parentNode.id.split("sub_")[1];
				//	alert(firstindex + "  " + document.getElementById("firstContainer").getElementsByTagName("li").length);
					document.getElementById("firstContainer").getElementsByTagName("li")[firstindex].childNodes[0].className = "activated";
				}
				

				//make 2nd layer visible
				a[i].parentNode.style.display = "block";
			}
			
		}
	}
	
}



//default tab menu highlighting
function defaultHighlight(){

	var und;
	// li array
	var a = document.getElementsByTagName("li");
	
	var firstlis = document.getElementById("firstContainer").getElementsByTagName("li");
	var secondlis = document.getElementById("secondContainer").getElementsByTagName("li");
	
	var loc = window.location.href;




	//if first layer link clicked

	for(var i=0; i < firstlis.length; i++){
		
		if(firstlis[i].getElementsByTagName("a")[0] && firstlis[i].getElementsByTagName("a")[0].href == loc){		
			firstlis[i].getElementsByTagName("a")[0].className="activated";		
		
			if(document.getElementById("sub_" + i)){
				document.getElementById("sub_" + i).style.display = "block";
				
			}
	
		}
	}
	
	


	//traverse all lis with <a> child defined
	for(var i=0; i < a.length; i++) {
		// <li> <a> </a>	</li>
		// if <a> child is defined
		if(a[i].getElementsByTagName("a")[0] != und){
			// <li> <a href=VALUE></a></li>
			//if href matches document.location.href
			if (a[i].getElementsByTagName("a")[0].href == loc) {
				a[i].getElementsByTagName("a")[0].className="activated";
		
		
				//alert("href: " + a[i].getElementsByTagName("a")[0].href +"\n loc: " + loc);
				//<li id="VALUE"><a></a></li>
				//if li id is empty or undef
				//if(a[i].id.length == 0){
				//alert("a[i] id: " + a[i].id);
				if(a[i].getAttribute("id") == null || a[i].getAttribute("id").length == 0){
					//if <ul parent> id contains "sib_"
					if((document.getElementById(a[i].parentNode.id.split("sib_")[1]) != null))
						document.getElementById(a[i].parentNode.id.split("sib_")[1]).getElementsByTagName("a")[0].className = "activated";
					
					// warren bug-fix 06/02 
					// add firstContainer parentnode check condition, in case submenu label has the same name with ul id. Bug occurred on CLG
					//if element with id: "sub_[a innerhtml]" exists and it's a child of firstContainer

					if(document.getElementById("sub_"+a[i].getElementsByTagName("a")[0].innerHTML) && 
					    a[i].parentNode.parentNode.getAttribute("id") == "firstContainer"){
					

						document.getElementById("sub_"+a[i].getElementsByTagName("a")[0].innerHTML).style.display="block";
					}

					
				}
									//alert("line 80:" + a[i].parentNode.id.split("sub_"));
			    //<ul style="display: block;" id="sub_BABY"><li><a></a></li></ul>
				if(a[i].parentNode.id.split("sub_").length > 1){

					//alert("in a[i].parentNode.id.split(sub_).length > 1 condition");
					//highlight when 2nd layer is clicked
					if(document.getElementById(a[i].parentNode.id.split("sub_")[1]) != null){
						document.getElementById(a[i].parentNode.id.split("sub_")[1]).className = "activated";
					}


					a[i].parentNode.style.display = "block";
					
					var subindex = 	a[i].parentNode.id.split("sub_")[1];	
	
					//add first layer highlight
					if(document.getElementById("firstContainer").getElementsByTagName("li")[subindex]){
						document.getElementById("firstContainer").getElementsByTagName("li")[subindex].getElementsByTagName("a")[0].className = "activated";
					}
					
				}
				
	
			}
		}
	}

	
}


//BC_CATALOGS_ON highlighting if page is a product page
function BCProductpageHighlight(){

	var und;
	// li array
	var a = document.getElementsByTagName("li");
	var loc = window.location.href;
	var found;
		
	//capture xxxx value from /_product_xxxx/ on window.location value
	var fid = delineate(loc);
		
	//alert("fid: "+ fid);
	for(var i=0; i < a.length; i++) {

		// if current link has occurrence of xxxx (fid) 
		if(a[i].getElementsByTagName("a")[0] && a[i].getElementsByTagName("a")[0].href.split(fid).length > 1){

			found = a[i];
			a[i].getElementsByTagName("a")[0].className = "activated";
			//if id of parent node of li contains 'sub_'
			//firstContainer struct: <ul> <li><a id=[parent menu label]></li></ul>
			// secondContainer struct: <ul id="sub_[parent menu label]> <li class=subLists_[submenu label] /> <li /> 
			if(a[i].parentNode.id.split("sub_").length > 1){
				

				/*
				if(document.getElementById(a[i].parentNode.id.split("sub_")[1]) != null){
					//make parent menu active
					document.getElementById(a[i].parentNode.id.split("sub_")[1]).className = "activated";
				}
				*/
				
				//Warren 07/07 - update
				//change first level highlight condition based on li index
				if(a[i].parentNode.parentNode.id == "secondContainer"){ 				
					var firstindex = a[i].parentNode.id.split("sub_")[1];
				//	alert(firstindex + "  " + document.getElementById("firstContainer").getElementsByTagName("li").length);
					document.getElementById("firstContainer").getElementsByTagName("li")[firstindex].childNodes[0].className = "activated";
				}
				

				//make 2nd layer visible
				a[i].parentNode.style.display = "block";
			}
			
		}
	}
}

//Utility functions
//obtain xxxx from url: /_protuct_xxxx/pagename...
function delineate(str)
{
theleft = str.indexOf("_product_") + 9;
theright = str.lastIndexOf("/");
return(str.substring(theleft, theright));
}


function getSuffix(str,suff)
{
	var leftside = str.indexOf(suff) + suff.length;
	var rightside;


	if(str.substring(leftside).indexOf("/") < 0 )
	    rightside = str.length;
	else
		rightside = str.substring(leftside).indexOf("/") + leftside;
	
	return(str.substring(leftside, rightside));	
}
