//various globals:
var aLangCodes=[{"lang":"English","code":"en"},{"lang":"French","code":"fr"},{"lang":"Chinese","code":"zh"},{"lang":"Spanish","code":"es"}];
var countriesArray = new Array( ), mapArray = new Array(), startCountry, startLang;
var objOpened = null, objClosed = null, opened = false;

(window.attachEvent) ? window.attachEvent('onload', initMenu) : window.addEventListener('load', initMenu, false);

//===========================LOCATOR===========================
function initMenu() {
  if((location.pathname=='/' || location.pathname=='') && getCookie("TULang")=='fr')location.href="http:\/\/"+v_hostname+"/ca/home_fr.page"; //redirect no-path URL to French if French cookie set
  initLocator();
  updateLocator(); //on soft refresh read from selections from pulldowns
  document.getElementById('imageClosed').onclick = toggleMenu;
  document.getElementById('imageOpened').onclick = closeMenu;
  var oGo=document.getElementById('imageGo');
  oGo.onclick = locatorGo;
  //oGo.style.cursor="pointer"; //defined in css
  (window.attachEvent) ? document.attachEvent('onclick', possiblyClose) : window.addEventListener('click', possiblyClose, false);
  //don't close opened locator if click inside locator:
  document.getElementById('locatorWrap').onclick = stopBubble;  
  //see if lang stored in cookie, if yes point selector to that lang:
  var sLang=getCookie("TULang");
  if(sLang) {  //cookie exists
    var oLang=document.getElementById('selectLanguage');
	if(oLang) {
	  var sLang=oLang.options[oLang.selectedIndex].text;
	  //alert('sLang = '+sLang);
	} //if oCountry
	//alert('selected lang = '+oLang.options[oLang.selectedIndex].text);
  } //if sLang cookie

}  //initMenu()

function locatorGo() {
  updateLocator();
  closeMenu();
  flipLanguageWrap(document.locatorForm.selectLocator,document.locatorForm.selectLanguage);
} //locatorGo()

function stopBubble( oEvent ) {
  ( typeof oEvent == "undefined" ) ? event.cancelBubble = true : oEvent.stopPropagation();
} //stopBubble()

function closeMenu() {
  objOpened = document.getElementById('locatorOpened');
  objClosed = document.getElementById('locatorClosed');
  objOpened.style.display = 'none';
  objClosed.style.display = 'block';
  opened = false;
}  //closeMenu()

function possiblyClose() {
  if( opened ) closeMenu();
} //possiblyClose()

function toggleMenu() {
  objOpened = document.getElementById('locatorOpened');
  objClosed = document.getElementById('locatorClosed');
  if( opened ) {
    objOpened.style.display = 'none';
    objClosed.style.display = 'block';
    opened = false;
  } else {
    objClosed.style.display = 'none';
    objOpened.style.display = 'block';
    opened = true;
  } //if opened
}  //toggleMenu()

//update locator display based on selects:
function updateLocator() {
  var docForm = document.locatorForm;
  var loc = docForm.selectLocator.options[docForm.selectLocator.selectedIndex].text;
  var locLen = loc.length + 1;
  var objDispLoc = document.getElementById( 'locCountry' );
  var objDispLang = document.getElementById( 'locLanguage' );
  objDispLoc.innerHTML = loc + ':';
  //loop through languages, check which selected, and generate output:
  var langs = '', langsLen = 0;
  var selIndex = docForm.selectLanguage.selectedIndex;
  //langs += docForm.selectLanguage.options[selIndex].text;
  langs += '<a onclick="toggleMenu(); return false;" class="locLanguageLink">'+docForm.selectLanguage.options[selIndex].text+'</a>';
  for( var i = 0; i < docForm.selectLanguage.length; i++ ) {
	langsLen += docForm.selectLanguage.options[i].text.length;
    if(i==selIndex)continue; //skip selected, already printed
	langs += '&nbsp;<img class="localePipeAlign" src="/images/artifacts/localePipe.gif"/>&nbsp;';
	var sText = docForm.selectLanguage.options[i].text, sURL = docForm.selectLanguage.options[i].value;
	langs += '<a class="localeLink" onclick="updateLocator2('+i+');flipLanguage(\''+loc+'\',\''+sText+'\',\''+sURL+'\');return false;" href="' + docForm.selectLanguage.options[i].value + '">' + sText + '</a>';
  } //for langs
  adjustLocatorWidth(locLen, langsLen, objDispLang, docForm.selectLanguage.length);
  objDispLang.innerHTML = langs;
} //updateLocator()

//update locator display based on click:
function updateLocator2(sLangIndex) {
  var oSelLang=document.getElementById('selectLanguage');
  oSelLang.selectedIndex=sLangIndex;
  updateLocator();
} //updateLocator2()

function adjustLocatorWidth(sLocLen, sLangsLen, oDispLang, sLength) {
  var objLocWrap = document.getElementById('locatorWrap');
  var objLocClosed = document.getElementById('locatorClosed');
  var objLocOpened = document.getElementById('locatorOpened');
  var langsWidth = 40, width = 175;  //defaults
  if(sLength > 1) {  //calculated width if more than 1 language
    langsWidth=6.0*sLangsLen+10*(sLength-1);
    width = 6.5*sLocLen + langsWidth + 50;
  }  //if selectLanguage.length
  oDispLang.style.width = langsWidth + 'px';
  objLocWrap.style.width = width + 'px';
  objLocClosed.style.width = width + 'px';
  objLocOpened.style.width = width + 'px';
} //adjustLocatorWidth()

//initialize locator selects:
function initLocator() {
  //populate countries:
  var countrySelect=document.getElementById('selectLocator');  
  for(var i=0; i<countriesArray.length; i++) {  	
	var sText=countriesArray[i].substr(0,countriesArray[i].indexOf('|'));
	var sSelected=countriesArray[i].substr(countriesArray[i].lastIndexOf('|')+1)=="Yes"?true:false;
    countrySelect.options[i]=new Option(sText,i,sSelected,sSelected);
	if(sSelected){
	  countrySelect.selectedIndex=i; //IE Bug workaround
	  startCountry=sText;
	} //if sSelected
  }  //for countries
  populateLanguages(countrySelect.selectedIndex);  //populate languages based on country selection
  updateLocator();  //write to "closed" div current selection
} //initLocator()

//populate languages based on country selection:
function populateLanguages(countryIndex) {
  var languageSelect=document.getElementById('selectLanguage');
  languageSelect.length=0; //nullify first
  if( countryIndex<0)countryIndex=0;  //make sure there is a default
  //extract option values from mapArray:
  for(var i=0; i<mapArray[countryIndex].length; i++) {
	var sMap=mapArray[countryIndex][i];
	var sText=sMap.substr(0,sMap.indexOf('|'));
    var sURL=sMap.substring(sMap.indexOf('|')+1, sMap.lastIndexOf('|'));
	var sSelected=sMap.substr(sMap.lastIndexOf('|')+1)=="Yes"?true:false;
	languageSelect.options[i]=new Option(sText,sURL,sSelected,sSelected );
	if(sSelected) {
	  languageSelect.selectedIndex=i; //IE Bug workaround
	  startLang=sText;
	} //if sSelected
  }  //for languages
}  //populateLanguages()

//2006/11/22 pmackie@transunion.com
//get country, language, and url from selects, and pass along to flipLanguage()
function flipLanguageWrap(oLocSelect,oLangSelect) {
  flipLanguage(oLocSelect.options[oLocSelect.selectedIndex].text,oLangSelect.options[oLangSelect.selectedIndex].text,oLangSelect.options[oLangSelect.selectedIndex].value);
} //flipLanguage()

//2006/11/22 pmackie@transunion.com
//flip language on current page based on clicked oLink
//if both current and destination pages have lang in URL (_<lang>.suffix) use current filename with flipped lang
//otherwise goto sURL. If lang can be defined bake cookie with proper lang code.
function flipLanguage(sCountry,sLang,sURL) {
  if(sCountry==startCountry) {
    var sCurrLang, sDestLang;
    sCurrLang=getLang(location.pathname);
    if(sCurrLang)sDestLang=getLang(sURL);
    if(sDestLang) { //lang is flipped, so change sURL and bake 1yr cookie
	  sURL=flipLangInURL(location.pathname,sCurrLang,sDestLang);
	  saveLangInCookie(sDestLang);
    } else { //no sDestLang, but still need to bake cookie, try to retrieve it from aLangCodes object
      var sLangCode=getLangCode(sLang);
	  if(sLangCode)saveLangInCookie(sLangCode);
    } //if sDestLang
  } //if same country
  //alert( 'will go to: ' + sURL );
  window.location = sURL;
} //flipLanguage()

//2007/02/23 pmackie@transunion.com
//extract lang from path, for example: blah_fr.page will return "fr"
//return undefined if not found
function getLang(sPath) {
  var sNoSuffix=sPath.substr(0,sPath.lastIndexOf('.'));
  return sNoSuffix.indexOf('_')!=-1?sNoSuffix.substr(sNoSuffix.lastIndexOf('_')+1):undefined;
} //getLang()

function getLangCode(sLang) {
  for(var i=0; i<aLangCodes.length; i++)if(aLangCodes[i].lang==sLang)return aLangCodes[i].code;
  return undefined;
} //getLangCode()

//2007/02/23 pmackie@transunion.com
//flip lang in sURL from sCurrLang to sDestLang
function flipLangInURL(sURL,sCurrLang,sDestLang) {
  var sStr="_";
  var sOld=sStr.concat(sCurrLang).concat(".");
  var sNew=sStr.concat(sDestLang).concat(".");
  return sURL.replace(sOld,sNew);
} //flipLangInURL()

//2007/02/23 pmackie@transunion.com
//bake cookie with sLang
function saveLangInCookie(sLang) {
  var expires=new Date(); expires.setFullYear(expires.getFullYear()+1); setCookie("TULang", sLang, expires, '/');
} //saveLangInCookie()
//===========================END OF LOCATOR===========================
