var navigatorMap = null;
var imageMap = null;
var jsonObj = null;
var northAmericaCountries, southAmericaCountries, easternEuropeCountries = [];
var westernEuropeCountries, asiaCountries, africaCountries = [];
var middleEastCountries, australia = [];
var regionSpan = null;

//window.onload = init;
tii_callFunctionOnWindowLoad (init);

function init() {
   navigatorMap = document.getElementById('navigatorMap');
	document.getElementById('easternEurope').onmouseover = showEasternEurope;
	document.getElementById('southAmerica').onmouseover = showSouthAmerica;
	document.getElementById('northAmerica').onmouseover = showNorthAmerica;
	document.getElementById('middleEast').onmouseover = showMiddleEast;
	document.getElementById('westernEurope').onmouseover = showWesternEurope;
	document.getElementById('australia').onmouseover = showAustralia;
	document.getElementById('asia').onmouseover = showAsia;
   document.getElementById('africa').onmouseover = showAfrica;
	
   regionSpan = document.createElement('span');
   regionSpan.id = 'countryList';
	
   var loader = new net.ContentLoader('/time/global_business/country_locator.json', handleJson, null, 'GET');
}

function handleJson() {
	var jsonTxt = this.req.responseText;
	jsonObj = eval('('+jsonTxt+')');
}


function showNorthAmerica() {
	navigatorMap.style.backgroundPosition = '0 675px';
   createCountries(jsonObj.northAmericaCountries, '34px', '94px');
}

function showAustralia() {
	navigatorMap.style.backgroundPosition = '0 1350px';
	createCountries(jsonObj.australia, '150px', '370px');
}

function showAfrica() {
	navigatorMap.style.backgroundPosition = '0 1800px';
   createCountries(jsonObj.africaCountries, '80px', '210px');
}

function showAsia() {
	navigatorMap.style.backgroundPosition = '0 1575px';
   createCountries(jsonObj.asiaCountries, '50px', '256px');
}

function showMiddleEast() {
	navigatorMap.style.backgroundPosition = '0 900px';
   createCountries(jsonObj.middleEastCountries, '60px', '320px');
}

function showEasternEurope() {
   navigatorMap.style.backgroundPosition = '0 1125px';
   createCountries(jsonObj.easternEuropeCountries, '30px', '330px');
}

function showWesternEurope() {
	navigatorMap.style.backgroundPosition = '0 225px';
   createCountries(jsonObj.westernEuropeCountries, '7px', '225px');
}

function showSouthAmerica() {
	navigatorMap.style.backgroundPosition = '0 450px';
   createCountries(jsonObj.southAmericaCountries, '114px', '131px');
}

function createCountries(countries, topVal, leftVal) {
   	regionSpan.innerHTML = '';
   	for(var i = 0; i < countries.length; i++) {
      var aDiv = document.createElement('div');
      if(countries[i]) {
         aDiv.innerHTML = countries[i].name;
         aDiv.className = 'country';
         aDiv.onmouseover = highlightDiv;
         aDiv.onmouseout = removeHighlight;
         new Country(aDiv, '/time/global_business/' + countries[i].id);
         if(i == (countries.length - 1)) {
            aDiv.style.paddingBottom = '1px';
         }
         regionSpan.appendChild(aDiv);
      }
   }
   regionSpan.appendChild(aDiv);
   
   regionSpan.style.top = topVal;
   regionSpan.style.left = leftVal;
   navigatorMap.appendChild(regionSpan);
   
   regionSpan.style.display = 'inline';
	
}

function Country(domEl,url) {
   this.domEl = domEl;
   this.url = url;
   this.domEl.countryObj = this;
   this.domEl.onclick = this.clickHandler;
}

Country.prototype.clickHandler = function() {
   var countryObj = this.countryObj;
   var url = (countryObj && countryObj.url) ? countryObj.url : '';
   window.location = url;
}


function highlightDiv() {
   this.className = 'countryHighlighted';  
}

function removeHighlight() {
   this.className = 'country';
}

function goToCountry(countryId) {
   window.location = '/time/global_business/' + countryId;
}



/******************************** Ajax Functions *********************/
var net = {};
net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;

/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function(url,onload,onerror,method,params,contentType){
  this.req=null;
  net.currentLoader=this;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.loadXMLDoc(url,method,params,contentType);
};

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
  if (!method){
    method="GET";
  }
  if (!contentType && method=="POST"){
    contentType='application/x-www-form-urlencoded';
  }

  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){

    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }

  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.ContentLoader.onReadyState.call(loader);
      };

      this.req.open(method,url,true);
      if (contentType){
        this.req.setRequestHeader('Content-Type', contentType);
      }

      this.req.send(params);
    }catch (err){
      this.onerror.call(this);
    }
  }
};

net.ContentLoader.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  try{var httpStatus=req.status;
  if (ready==net.READY_STATE_COMPLETE){
    if (httpStatus==200 || httpStatus===0){
      this.onload.call(this);
    }else{
      this.onerror.call(this);
    }
  }
  }
  catch(e){}
};

net.ContentLoader.prototype.defaultError=function(){
  /*alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());*/
};



