//CLG highlighting
var BC_CATALOGS_ON = 1;
var BC_BLOGS_ON = 2;

var BC_CUSTOM1_ON = 3;
//var CUSTOM1_REGEX = /BlogRetrieve/;
var CUSTOM1_REGEX = /(\/_blog\/)|BlogRetrieve/;
var CUSTOM1_URL = "/_blog/Apres_Velo_Blog";


var ELEMENTFINDER_HIGHLIGHT = 4;
var ELEMENTID = "webappidentifier";
var ELEMENTCONTENT_URL = [
	/****Sydney - FOR THE HOME ****
	[/Renovations/,"/sydney/renovations.htm"],[/Party Supplies/,"/sydney/party_supplies.htm"],
	[/Home Cleaning/,"/sydney/home_cleaning"], [/Outdoors/,"/sydney/outdoors.htm"],[/Manchester/,"/sydney/manchester.htm"],
	[/Homewares &amp; Appliances/,"/sydney/homewares_appliances.htm"],[/Home\/Office/, "/sydney/home_office.htm"], 
	[/Furniture/, "/sydney/furniture.htm"], [/Food &amp; Drink/, "/sydney/food_drink.htm"],
	[/Fabrics/, "/sydney/fabrics.htm"],
	//****Sydney - FASHION
	[/Women's Fashion/, "/sydney/womens_fashion.htm"], [/Shoes &amp; Accessories/,"/sydney/shoes_accessories.htm"],
	[/Designer Fashion/,"/sydney/designer_fashion.htm"], [/Vintage &amp; Recycled Fashion/,"/sydney/vintage_recycled.htm"],
	[/Men's Wear/,"/sydney/menswear.htm"], [/Bridal &amp; Formal Attire/, "/sydney/bridal_and_formal"],
	[/Children/,"/sydney/children.htm"], [/Youth/, "/sydney/youth.htm"],
	//****Sydney - YOU
	[/Arts &amp; Crafts/,"/sydney/arts_crafts.htm"], [/Entertainment/, "/sydney/entertainment.htm"],
	[/Health &amp; Beauty/, "/sydney/health_beauty.htm"], [/Services/,"/sydney/services.htm"],
	[/Sports &amp; Leisure/,"/sydney/sports_leisure.htm"],
	//****Sydney - SAVVY
	[/Auctions/, "/sydney/auctions.htm"],[/Clearance/, "/sydney/clearance.htm"],
	[/Markets/, "/sydney/markets.htm"],[/Festivals &amp; Fairs/, "/sydney/festivals_and_fairs.htm"],
	//****Sydney - ONLINE SHOPPING
	[/Online Shopping/, "/sydney/online_shopping.htm"],
	//***Sydney - ADVICE
	[/Advice/, "/sydney/advice.htm"]
	
	**/
  ];
  
var BC_CATALOGS_SEO = 5;

var DEFAULT_HIGHLIGHT = 0;


/*
	v2.0 patch
	Updated : 8/9/2010
	by: Warren Earl Ngo
	- added BC_CATALOGS_SEO (5) highlight mode to support BC SEO friendly catalog / page URLs
*/



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];


	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){
					BCProductpageHighlight();
					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
			case BC_CATALOGS_SEO:
				//if no highlight was done do the default highlight
				//a lot of trapping on BCProductPageHighlightSEO() if trapping failed returns 0 and continues with default highlight
				if(BCProductPageHighlightSEO() == 0) defaultHighlight();
				break;

			default:
				defaultHighlight();
				//highlightdone = true;
				
		
		}
	
	}

	

}

/*
   v2.0 patch
   Added: 8/7/2010
   by: Warren Ngo
   Works like:
   Given 2nd level tab menu link which is also the current link of the page:  /mens-gear/Apres_Velo_Sneakers
   Parent menu and corresponding submenu is highlighted, parent menu link: any link that starts with '/mens-gear' example: /mens-gear , mens-gear-gay, /mens-gear-1
   
   Note that only the first parent menu occurrence is considered the next occurrence will be ignored.
   
  
*/
function BCProductPageHighlightSEO(){
	var pageURL = window.location.href;      // http://apresvelo.com , https://apresvelo.worldsecuresystems.com
	var hostvar = window.location.hostname;  // apresvelo.com, apresvelo.worldsecuresystems.com
	var stringvar = pageURL.substring(pageURL.indexOf(hostvar) + hostvar.length);  // string after the hostname: '/mens-gear/Life_Behind_Bars'
	var matcharr = stringvar.match(/\//gi); //count number of  '/' occurrence on stringvar
	var highlightFlag = 0;
	

	//alert("pageURL: " + pageURL + " hostvar: " + hostvar + " stringvar: "+ stringvar + " matcharr: " + matcharr);
	
	
	//check first if link exists on either menu levels, if it does end function and do default highlighting immediately
	if(jQuery("#firstContainer ul li a[href="+ stringvar + "], #secondContainer ul li a[href="+ stringvar + "]").length > 0){
		return highlightFlag;
	}
	
	
	//if current url is in a subfolder/subcatalog i.e.: /mens-gear/Bading_shirt, find the FIRST link with that subfolder occurrence and highlight it
	if(matcharr.length > 1){
		jQuery(function($){
			var subfolderURL = stringvar.substring(0, stringvar.lastIndexOf("/")); //Given: /mens-gear/Life_Behind_Bars, obtain '/mens-gear'
			 //ends with '/mens-gear[any number of non '/' characters], Used to support catalog pages implemented as web pages.
			var tempRegex = new RegExp(subfolderURL + "[^\/]*$");
			
			//alert("tempRegex: " + tempRegex);
			
			
			$("#secondContainer ul li a").each(function(){

				if(tempRegex.test($(this).attr("href")) ){
					//make sure every second level ul is hidden
					$("#secondContainer ul").hide();
					//show 2nd level ul
					$(this).parent().parent("ul").show();
					//highlight link
					$(this).toggleClass("activated",true);
					//highlight top level link
					var parentIndex = -1;
					//make sure id format is correct 'sub_1'
					if((/^sub_[0-9]?/).test($(this).parent().parent("ul").attr("id")) )
						parentIndex = ($(this).parent().parent("ul").attr("id")).split("_")[1];
					
					if(parentIndex != -1)
						$("#firstContainer ul li a:eq(" + parentIndex + ")").toggleClass("activated", true);
					
					//terminate iteration and set Flag once match is found
					highlightFlag = 1;
					return false;
				}
					
			});
			
			if(highlightFlag == 0){
				$("#firstContainer ul li a").each(function(index){
					if(tempRegex.test($(this).attr("href")) ){
						$(this).toggleClass("activated",true);
						$("#secondContainer ul").hide();
						//show corresponding 2nd level tab menu
						$("secondContainer ul:eq(" + index + ")").show();
						//terminate iteration and set Flag once match is found
						highlightFlag = 1;
						return false;
					}
						
				});
			}
		
		});
	}
	return highlightFlag;

}


//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;
	var highlightFlag = 0;


	//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));	
}

//tracing function, where appendAfter is CSS identifier of the footer div where all the tracing results  will be placed
//ie: traceFunc("div.minifooter","var: " + 23);
function traceFunc(appendAfter, msg){
    jQuery(appendAfter).after("<div class='trace' style='display: none;'>" +msg + "</div>");

}
