function getMouseXY(e) {
	ev = e || window.event;
	IE = document.all?true:false;
	if(IE) {
		x = ev.clientX;
		y = ev.clientY;
		if (window.ActiveXObject && document.getElementById){y += document.documentElement.scrollTop};
	} else {
		x = ev.pageX;
		y = ev.pageY;
	}
	
////	DISABLED 2010-03-11 John Sanders
//	scrolled = window.pageYOffset;
//  scrolledDown = document.documentElement.scrollTop;
//  x = (window.Event) ? e.pageX : event.clientX;
//  y = (window.Event) ? e.pageY : event.clientY;
//	if (window.ActiveXObject && document.getElementById){y += scrolledDown};

}

// grab the locations by id (id = state initial, such as CA for California)
function doImport(id) {
	var obj = id;
	if ( !document.getElementById ) return;
	var contentObj = document.getElementById("balloon_contents");
	if ( contentObj ) {
		//contentObj.innerHTML = '<a href="http://www.yahoo.com">My Stuff</a>';
		contentObj.innerHTML = addMapValue(obj);
	}
}

var currentBalloon = null;


////	DISABLED 2010-03-11 John Sanders
////	I simplified the window.onload event at the bottom, and we don't need this anymore.
//function addLoadEvent(func)
//{
//  var oldonload = window.onload;
//  if (typeof window.onload != 'function')
//  {
//    window.onload = func;
//  }
//  else
//  {
//    window.onload = function()
//    {
//      oldonload();
//      func();
//    }
//  }
//}

function hideBalloon() {
  if ( !document.getElementById ) return;
  var balloon = document.getElementById("balloon");
  if ( balloon ) {
    balloon.style.display = 'none';
    currentBalloon = null;
  }
  return false;
}

// pops open a balloon when the user clicks on an <area> in the worldpac location <map>
function displayBalloon(anImg)
{
  if ( !document.getElementById ) return;

  var imgID = anImg.getAttribute('id');
	// there are a few separate areas that should pop the same data into the info window, but rather than
	// use invalid HTML and give them the same ID, we are appending a tie breaker to the end, e.g. DC-1, DC-2.
	// We need to strip out the tie breaker to leave just the area name, such as DC
	var dupIDPattern = /([A-Z]+)(\-\d+)?/;
	imgID = dupIDPattern.exec(imgID)[1] || imgID;
	if ( imgID == currentBalloon ) return false;
  currentBalloon = imgID;
  var objX = x-8;
  var objY = y-25;

  var balloon = document.getElementById("balloon");
  if ( balloon ) {
    if ( balloon.childNodes ) {
      while ( balloon.childNodes.length > 0 ) {
        balloon.removeChild(balloon.childNodes[0]);
      }
    }
    var closeElt = document.createElement('img');
    closeElt.setAttribute("id","closebox");
    closeElt.setAttribute("src","http://www.worldpac.com/images/close_box.gif");
    closeElt.setAttribute("align","left");
    closeElt.setAttribute("width","14");
    closeElt.setAttribute("height","13");
    closeElt.onclick = function(){ return hideBalloon(); }
    balloon.appendChild(closeElt);

    var newElt = document.createElement('div');
    newElt.setAttribute("id","balloon_contents");
    closeElt.setAttribute("align","left");
//    newElt.setAttribute("style","overflow:auto;");
    balloon.appendChild(newElt);
		doImport(imgID);
//    balloon.style.bottom = (objY) + 'px';
    balloon.style.top = (objY-131) + 'px';
    balloon.style.left = (objX-294+40) + 'px';
    balloon.style.display = 'block';
  }
  return false;
}

//	Create onclick event handler for each <area> on the worldpac location <map>
function addBalloons() {
	if ( !document.getElementsByTagName && !document.getElementById ) return;
	var locMap = document.getElementById("locations-map");
	if ( !locMap ) return;
	var mapAreas = locMap.getElementsByTagName("area");  ///chg img to area
//	console.log(mapAreas.length);
	if ( mapAreas.length > 0 ) {
		for ( var i in mapAreas ) {
			var area = mapAreas[i];
			//curImg.onmouseover = function()
			area.onclick = function() { return displayBalloon(this) }
		}
	}
	var balloon = document.createElement('div');
	balloon.setAttribute("id", "balloon");
	document.getElementsByTagName("body")[0].appendChild(balloon);
}

function initialize() {
	addBalloons();
  document.onmousemove = getMouseXY;
}

//	Run these functions when the page has loaded
window.onload = initialize;

////	DISABLED 2010-03-11 John Sanders
//addLoadEvent(addBalloons);
//addLoadEvent(init);

