// Global Biz Tools
tii_addEventHandlerOnElementLoad ('tabChange3', 'click', function (event){tabChange(3);}, false);
tii_addEventHandlerOnElementLoad ('tabChange4', 'click', function (event){tabChange(4);}, false);
tii_addEventHandlerOnElementLoad ('tabChange5', 'click', function (event){tabChange(5);}, false);

var keyevent = tii_issafari || tii_isie ? 'keydown' : 'keypress';
tii_addEventHandlerOnElementLoad ('tabChange3', keyevent , function (event){tabChange(3);}, false);
tii_addEventHandlerOnElementLoad ('tabChange4', keyevent , function (event){tabChange(4);}, false);
tii_addEventHandlerOnElementLoad ('tabChange5', keyevent , function (event){tabChange(5);}, false);

function tabChange(num) {
	tList = "tab"+num;
	tContent = "tabContent"+num;
	var ref = document.getElementById(tList).parentNode;
	for (var i=3; i<6; i++) {
		document.getElementById("tab"+i).className = "";
		document.getElementById("tabContent"+i).className = "off";
	}
	document.getElementById(tList).className = "on";
	document.getElementById(tContent).className = "";
}


// Attaching event handlers to elements 
var clickToggle = true; 
tii_addEventHandlerOnElementLoad ('bubbleLink2', 'mouseover', function (event){displayBubble(2);}, false);
tii_addEventHandlerOnElementLoad ('bubbleLink2', 'mouseout', function (event){hideBubble(2);}, false);

// Explanation Bubble for What's GCI
function displayBubble(num){

	bubLink='bubbleLink'+num;
	bub = document.getElementById('bubble'+num);
	if (!bub) return true;
	var bubPos = new Array(2);
	bubPos = findPos(bubLink);	
	
		bub.style.left = (parseInt(bubPos[0]) + 66).toString() + 'px';
		bub.style.top = (parseInt(bubPos[1]) + 213).toString() + 'px';	
	// Display the bubble
	bub.style.display = 'block';
	clickToggle = false;
}

// Hides the bubble
function hideBubble(num){
	var bub=document.getElementById('bubble'+num);	
	if (!bub) return true;	
	bub.style.display = 'none';
	clickToggle = true;
}

// Finds the position of an element on the page
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

var countryNavigatorSmall = null;
var regionSpan = null;

//window.onload = init;
tii_callFunctionOnWindowLoad (init);

function init() {
   countryNavigatorSmall = document.getElementById('navigatorMapSmall');
   document.getElementById('sm_africa').onmouseover = showSmAfrica;
   document.getElementById('sm_asia').onmouseover = showSmAsia;
   document.getElementById('sm_australia').onmouseover = showSmAustralia;
   document.getElementById('sm_westerneurope').onmouseover = showSmWesternEurope;
   document.getElementById('sm_middleeast').onmouseover = showSmMiddleEast;
   document.getElementById('sm_northamerica').onmouseover = showSmNorthAmerica;
   document.getElementById('sm_southamerica').onmouseover = showSmSouthAmerica;
   document.getElementById('sm_easterneurope').onmouseover = showSmEasternEurope;
   
   regionSpan = document.createElement('span');
   regionSpan.id = 'countryList';
	
   var loader = new net.ContentLoader('/time/rd/trunk/www/web/feds/x/countryLocator.json', handleJson, null, 'GET');
}

function handleJson() {
	var jsonTxt = this.req.responseText;
	jsonObj = eval('('+jsonTxt+')');
}

function showSmNorthAmerica() {
	countryNavigatorSmall.style.backgroundPosition = '0 -1350px';
   createCountries(jsonObj.northAmericaCountries, '38px', '54px');
}

function showSmAustralia() {
	countryNavigatorSmall.style.backgroundPosition = '0 -675px';
	createCountries(jsonObj.australia, '116px', '230px');
}

function showSmAfrica() {
	countryNavigatorSmall.style.backgroundPosition = '0 -225px';
   createCountries(jsonObj.africaCountries, '70px', '170px');
}

function showSmAsia() {
	countryNavigatorSmall.style.backgroundPosition = '0 -450px';
   createCountries(jsonObj.asiaCountries, '50px', '156px');
}

function showSmMiddleEast() {
	countryNavigatorSmall.style.backgroundPosition = '0 -1125px';
   createCountries(jsonObj.middleEastCountries, '50px', '70px');
}

function showSmEasternEurope() {
   countryNavigatorSmall.style.backgroundPosition = '0 -900px';
   createCountries(jsonObj.easternEuropeCountries, '32px', '210px');
}

function showSmWesternEurope() {
	countryNavigatorSmall.style.backgroundPosition = '0 -1800px';
   createCountries(jsonObj.westernEuropeCountries, '10px', '155px');
}

function showSmSouthAmerica() {
	countryNavigatorSmall.style.backgroundPosition = '0 -1575px';
   createCountries(jsonObj.southAmericaCountries, '95px', '90px');
}

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.style.top = topVal;
   regionSpan.style.left = leftVal;
   countryNavigatorSmall.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());*/
};
