var strCurrId;

function openCenteredWindow(theURL,winName,features, myWidth, myHeight) { //v3.0
	var arrArguments=openCenteredWindow.arguments;
	var intArguments=arrArguments.length;
	//alert(intArguments);
	var objLinkNode;
	if(intArguments>5){
		objLinkNode=arrArguments[5];
		/*
		alert("--"+objLinkNode.nodeType);
		for (key in objLinkNode) {
			//alert(_page.leftNav[id].link)
			strValue=objLinkNode[key];
			alert(key+" - "+strValue);		
		}
		*/
	}
	var isCenter="true";
	var bolProductionSite=true;
	var strDomainName=location.hostname;
	if(strDomainName=="130.144.57.72" || strDomainName=="130.144.222.44" || strDomainName=="130.144.57.71" ||  strDomainName=="pww.teamsite.lighting.philips.com" ||  strDomainName=="161.85.125.39" ||  strDomainName=="www.qa.lighting.philips.com")bolProductionSite=false;
	
	// encode theURL
	theURL = escape( theURL );
	
	if(window.screen)if(isCenter)if(isCenter=="true"){
		var myLeft = (screen.width-myWidth)/2;
		var myTop = (screen.height-myHeight)/2-10;
		features+=(features!='')?',':'';
		features+=',left='+myLeft+',top='+myTop;
	}	
	
	if(regExpImages.test(theURL)){
		//alert("dymnamic popup");
		//encoding??
		theURL="/assets_global/dynamic_popup.php?src="+theURL;
		window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
	}else{
		if(regExpDownloads.test(theURL) && bolProductionSite){
			//sent a hit to the sage metrics server!!!
			sage_ev(1,theURL+location.search);
		}		
		
		/*
		Fix double escaped characters
		*/
		var regExpHttpLinks=/^(http|www|\d).*$/i;
		if(regExpHttpLinks.test(theURL)){
			theURL=theURL.replace(/%3A/gi,":");
		}
		
		if(theURL.indexOf('usehistoryforguess')){
			theURL=theURL.replace(/%3F/gi,"?");
			theURL=theURL.replace(/%3D/gi,"=");
			theURL=theURL.replace(/%26/gi,"&");
		}
		
		//alert(theURL);
		if(winName=='microsite'){
			var objNewWindow=window.open(theURL,winName);
		}else{	
			var objNewWindow=window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
		}
	}
}

function linkLookup() {
	//if(bolSpeedTest)createSplitTimeMessage("in lookup");
	var strCurrentPath=document.location.pathname;
	var strSearchString=document.location.search;
	if(strCurrentPath.indexOf('performance_measure_any_page.php')>1){
		var cookievalue = getCookie("measurementurl");
		if(cookievalue){
			strCurrentPath=cookievalue;
			strCurrentPath=strCurrentPath.replace(/^http:.*(\.com|\.nl|\.de|\.net)(\/.*)$/,"$2");
			if(strCurrentPath.indexOf('?')>1){
				strSearchString=strCurrentPath.replace(/^(.*)(\?.*)$/,"$2");
				strCurrentPath=strCurrentPath.replace(/^(.*)(\?.*)$/,"$1");
			}
			//alert(strCurrentPath);
		}		
	}
	var strTempId="";
	var bolCheckNext=false, bolReturnValue=false;
	
	var strMainId=strSearchString.replace(/^.*main=(.*)&parent.*$/,"$1");
	var strParentId=strSearchString.replace(/^.*parent=(.*)&id.*$/,"$1");
	var strId=strSearchString.replace(/^.*id=(.*)&lang.*$/,"$1");
	var strGetUrl="";
	if(strSearchString.indexOf("geturl=")>1){
		strGetUrl=strSearchString.replace(/^.*geturl=(.*)$/,"$1");
		if(strGetUrl.indexOf("&")>1)strGetUrl=strGetUrl.replace(/^(.*)&(.*)$/,"$1");
	}
	
	//if(document.location.pathname.indexOf('performance_measure_any_page.php')>1)alert(strMainId+" - "+strParentId+" - "+strId);
	
	//check if current item is the home page
	var strUniqueIdentifier;
	if(strMainId==strParentId && strMainId==strId){
		strUniqueIdentifier="parent="+strGetUrl;
	}else{
		strUniqueIdentifier="parent="+strParentId+strGetUrl;
	}
	
	strCurrentPath=strCurrentPath+strUniqueIdentifier;
	
	var strLeftNavPath;
	if(bolDebug)strLookupDebug+="<b>Searching: "+strCurrentPath+"</b><br/>";
	//alert(strCurrentPath);
	
	for (id in _page.leftNav) {
		if(bolCheckNext)bolReturnValue=true;
		//alert(_page.leftNav[id].link)
		strLeftNavPath=_page.leftNav[id].link;		
		
		//retrieve information from the querystring to build a unique tring that will match the current page
		strUniqueIdentifier=strLeftNavPath.replace(/^.*(parent=.*)&amp;id.*$/,"$1");
		strGetUrl="";
		if(strLeftNavPath.indexOf("geturl=")>1){
			strGetUrl=strLeftNavPath.replace(/^.*geturl=(.*)$/,"$1");
			if(strGetUrl.indexOf("&")>1)strGetUrl=strGetUrl.replace(/^(.*)&(.*)$/,"$1");
		}		
		strLeftNavPath=strLeftNavPath.replace(/^(.*)\?.*$/,"$1");
		strLeftNavPath=strLeftNavPath+strUniqueIdentifier+strGetUrl;
		
		if(bolDebug){
			if(bolCheckNext){
				strLookupDebug+="-"+strLeftNavPath+" (check for next possible child having the same path as the parent)<br/>";
			}else{
				strLookupDebug+="-"+strLeftNavPath+"<br/>";
			}
		}
		
		if (strCurrentPath == strLeftNavPath) {
			strTempId=id;
			bolCheckNext=true;			
		}
		
		if(bolReturnValue){
			//if(bolSpeedTest)createSplitTimeMessage("uit lookup (id="+strTempId+")");
			return(strTempId);
			break;
		}
	}
	//if(bolSpeedTest)createSplitTimeMessage("uit lookup (id="+strTempId+")");
	return strTempId
}

function displayLangSwitchMessage(){
	if(strLanguageSwitchMessage!=""){
		var bolContinueLangSwitch=confirm(strLanguageSwitchMessage);
		if(!bolContinueLangSwitch)history.go(-1);
	}
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

//on load time
var strLookupDebug="";
var bolDebug=false;
if(document.location.search.indexOf('debug')>0 && document.location.search.indexOf('cachedebug')<1 && document.location.search.indexOf('crtdebug')<1)bolDebug=true;
//var bolSpeedTest=false;
//if(document.location.search.indexOf('speedtest')>0)bolSpeedTest=true;

//let the user confirm to continue with a language switch if that occurs
displayLangSwitchMessage();

//get the current page id
strCurrId=linkLookup();
//alert(strCurrId);

//change the window title if we are using the view_page utility
if(document.location.href.indexOf('view_page.php')>0)document.title=_page.leftNav[strCurrId].text;

//show debug window
if(bolDebug)openDebugWindow(strLookupDebug,false);
