//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/Bargain_Shopper_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"],
//***Melbourne - FASHION
	[/^Melbourne Womens Fashion/,"/melbourne/womens_fashion.htm"],
	[/^Melbourne Maternity Wear/,"/melbourne/maternity_wear.htm"],
	[/^Melbourne Shoes/, "/melbourne/shoes.htm"],
	[/^Melbourne Accessories &amp; Jewellery/,"/melbourne/accessories_jewelry.htm"],
	[/^Melbourne Vintage &amp; Preloved Fashion/,"/melbourne/vintage_preloved_fashion.htm"],
	[/^Melbourne Mens Fashion/,"/melbourne/mens_fashion.htm"],
	[/^Kids Gear/,"/melbourne/kids_gear.htm"],
//***Melbourne - YOU
	[/^Melbourne Books and Music/,"/melbourne/books_music.htm"],
	[/^Melbourne Entertainment/,"/melbourne/entertainment.htm"],
	[/^Melbourne Food and Drink/,"/melbourne/food_drink.htm"],
	[/^Melbourne Gifts and Party Supplies/,"/melbourne/gifts_party_supplies.htm"],
	[/^Melbourne Health and Beauty/,"/melbourne/health_beauty.htm"], 
	[/^Melbourne Sport and Leisure/,"/melbourne/sport_leisure.htm"],
	[/^Melbourne Wine/,"/melbourne/wine.htm"],
	[/^Melbourne Travel Goods/,"/melbourne/travel_goods.htm"],
	//***Melbourne - FOR THE HOME
	[/^Melbourne Computers &amp; Office Supplies/,"/melbourne/computers_office_supplies.htm"],
	[/^Melbourne Fabric &amp; Craft/,"/melbourne/fabric_craft.htm"],
	[/^Melbourne Floor Coverings/,"/melbourne/floor_coverings.htm"],
	[/^Melbourne Furniture/,"/melbourne/furniture.htm"],
	[/^Melbourne Homewares &amp; Garden/,"/melbourne/homewares_garden.htm"],
	[/^Melbourne Manchester/,"/melbourne/manchester_soft_furnishings.htm"],
	[/^Melbourne Pets/,"/melbourne/pets.htm"],
	[/^Melbourne Renovators/,"/melbourne/renovators.htm"],
	[/^Melbourne Whitegoods &amp; Appliances/,"/melbourne/whitegoods_appliances.htm"],
	//***Melbourne - SAVVY
	[/^Melbourne Markets/,"/melbourne/markets.htm"],
	[/^Online Stores/,"/melbourne/online_stores.htm"],
	[/^Melbourne Auctions/,"/melbourne/red_hot_specials.htm"],
	[/^Shopping Tours/,"/melbourne/shopping_tours.htm"],
[/^Melbourne Advice/,"/melbourne/advice.htm"],
[/^Melbourne Online Shopping/,"/melbourne/online_shopping.htm"],
	//***Melbourne - ADVICE
	[/^Melbourne BUYING A USED CAR/,"/melbourne/advice.htm"],
	[/^Melbourne CHEAP BUT CHIC WEDDINGS/,"/melbourne/advice.htm"],
	[/^Melbourne STOP SPENDING MONEY AND START MAKING IT!/,"/melbourne/advice.htm"],
	[/^Melbourne BARGAIN BEAUTY SECRETS/,"/melbourne/advice.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));	
}
