//various globals:
var countriesArray = new Array( ), mapArray = new Array();
var objOpened = null, objClosed = null, opened = false;

(window.attachEvent) ? window.attachEvent('onload', initMenu) : window.addEventListener('load', initMenu, false);

//===========================LOCATOR===========================
function initMenu () {
  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;
}  //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="transunion/localePipe.gif"/>&nbsp;';
	var sText = docForm.selectLanguage.options[i].text, sURL = docForm.selectLanguage.options[i].value;
	// temporary patch
	//sText = sText.replace("ç","&ccedil;");
	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 defaultSel = false, sel = false;
    if( i == 0 ) { defaultSel = true; sel = true; }  //select 1st option as default
    else { defaultSel = false; sel = false; }
    countrySelect.options[i] = new Option( countriesArray[i], i, defaultSel, sel );
  }  //for countries
  populateLanguages( countrySelect.selectedIndex );  //populate languages based on country selection
  updateLocator();  //write to "closed" div current selection
} //initLocator()*/

function initLocator() {
  //populate countries:
  var countrySelect=document.getElementById('selectLocator');
  for(var i=0; i<countriesArray.length; i++) {
	 // if(i<5) alert(countriesArray[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 );
  }  //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
function flipLanguage(sCountry,sLang,sURL) {
  //FIX: FUTURE MULTI-LANGUAGE FUNCTIONALITY HERE,
  //for now just go to url:
  //alert( 'will go to: ' + sURL );
  top.location = sURL;
} //flipLanguage()
