﻿var officeLocationClass = "officeLocation";
var OfficeLocationMainDiv = "officeLocation";
var officeLocationIntroText = "introduction";
var officeLocationHeaderText = "OfficeLocationHeader";
var officeLocationHeadOfficeText = "HeadOffice";

function OnloadOfficeLocation()
{
    //Get parameters from url
    var region = getURLParam("region");
    var country = getURLParam("country");
    var city = getURLParam("city");

    //show all if browser isn't flash installed
    var LocationDiv = document.getElementById('A-FeatureFlash');
    if(LocationDiv.innerHTML.indexOf('IMG') > -1 || LocationDiv.innerHTML.indexOf('img') > -1)
    {
    }
    else
    {
        //No request parameters: display head office
        if (region == "" && country == "" && city == "") {
            displayHeadOffice();
        } 
        //Display by city
        else if (region != "" && country != "" && city != "") {
            displayByCity(region, country, city, false);
        }
        //Display by country
        else if (country != "" && city == "") {
            displayByCountry(region, country, false);
    
        }
    }
}

function hideAllOfficeLocations()
{
    //Get all div elements
    var divs = getElement(OfficeLocationMainDiv).getElementsByTagName("div");
    
    // Hide all divs for office locations
    for(var i = 0; i < divs.length; i++) { 
        var divId = divs[i].id;
        if (divId && divId != officeLocationIntroText)
            displayOfficeLocation( divId, false);
    }
}
function showAllOfficeLocation()
{
    //Get all div elements
    var divs = getElement(OfficeLocationMainDiv).getElementsByTagName("div");
    
    // Hide all divs for office locations
    for(var i = 0; i < divs.length; i++) { 
        var divId = divs[i].id;
        if (divId && divId != officeLocationIntroText)
            displayOfficeLocation( divId, True);
    }
}

function sIFRReplace()
{
    if(typeof sIFR == "function"){
        getElement(officeLocationHeaderText).className = "no-neg-position";
        sIFR.replaceElement("h1.no-neg-position", named({sFlashSrc: sIFRPath + "HelveticaNeueLight.swf", sWmode: "transparent", sColor:"#75af28"}));
    }
}

function displayHeadOffice()
{
    hideAllOfficeLocations();
    getElement(officeLocationHeaderText).innerHTML = officeLocationPageTitle;
    sIFRReplace();
    displayOfficeLocation(officeLocationHeadOfficeText, "true");
}

function displayByCountry(region, country, isFlashRequest)
{
    hideAllOfficeLocations();
    getElement(officeLocationHeaderText).innerHTML = officeLocationPageTitle + ": " + country;
    sIFRReplace();
    
    //Get all div elements
    var divs = getElement(OfficeLocationMainDiv).getElementsByTagName("div");
    
    // Hide all divs for office locations
    for(var i = 0; i < divs.length; i++) { 
        var divId = divs[i].id;
        var searchText = country.replace(" ", "").toLowerCase();
        if (divId && divId.toLowerCase().indexOf(searchText) == 0) //the div id starts with the country name
            displayOfficeLocation( divId, true);
    }
    
    if (!isFlashRequest)
        ZoomIn(region, country, "-");
}

function displayByCity(region, country, city, isFlashRequest)
{
    hideAllOfficeLocations();
    getElement(officeLocationHeaderText).innerHTML = officeLocationPageTitle + ": " + city;
    sIFRReplace();
    
    //Get all div elements
    var divs = getElement(OfficeLocationMainDiv).getElementsByTagName("div");
    
    // Hide all divs for office locations
    for(var i = 0; i < divs.length; i++) { 
        var divId = divs[i].id;
        var searchText = "-" + city.replace(" ", "").toLowerCase();
        if (divId && divId.toLowerCase().indexOf(searchText) > -1) //the div id in "-[city name with no space]" format
            displayOfficeLocation( divId, true);
    }
    
    if (!isFlashRequest)
        ZoomIn(region, country, city);
}

function getElement ( whichElement ) 
{
  var elem;
  
  if( document.getElementById ) // this is the way the standards work    
    elem = document.getElementById( whichElement );  
  else if( document.all ) // this is the way old msie versions work      
    elem = document.all[whichElement];  
  else if( document.layers ) // this is the way nn4 works    
    elem = document.layers[whichElement];
    
  return elem;
}    

function displayOfficeLocation( whichLayer, isDisplayed ) 
{
    var oElement = getElement(whichLayer);
    if(oElement)
    {
        oElement.style.display = (isDisplayed) ? "block" : "none";
    }
}

function getURLParam(strParamName) 
{
  var strReturn = "";
  var strHref = window.location.href;
  
  if ( strHref.indexOf("?") > -1 ){
  
    //Get query string
    var strQueryString = strHref.substr(strHref.indexOf("?"));
    
    //Remove anchor text from the string
    if ( strQueryString.indexOf("#") > -1 )
      strQueryString = strQueryString.substr(0, strQueryString.indexOf("#"));
      
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( aQueryString[iParam].toLowerCase().indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}  

//Function called by flash
//old (location, zone) 
function DisplayLocation(region, country, city) 
{
    //alert('DisplayLocation region=' + region + ' country=' + country + ' city=' + city);
    if (city != null && city != "")
        displayByCity(region, country, city, true)
    else
        displayByCountry(region, country, true)
}

//function displayFlash(region, country, city) 
function ZoomIn(region, country, city) 
{
    var flash = getElement("locationSWF");
    region = region.replace(" ", "").toLowerCase();
    country = country.replace(" ", "").toLowerCase();
    city = city.replace(" ", "").toLowerCase();
    //alert('Calling doZoomIn(region=' + region + ',country=' + country + ',city=' + city);
    flash.doZoomIn(region,country,city);
}