var svgns = 'http://www.w3.org/2000/svg';
var http_root = 'maps/';
var currentView = null;
var parentStack = new Array();
var back_button = null;
var map = null;
var bounds = null;


function startOff() {
  bounds = document.getElementById('bounding');
  map = document.getElementById('map');
  loadGeography('world');
}

function activateSelection() {
  map.setAttribute("onmouseup", "selectGeography(evt)");
  map.setAttribute("onmouseover", "enterGeography(evt)");
  map.setAttribute("onmouseout", "leaveGeography(evt)");
}

function deactivateSelection() {
  map.removeAttribute("onmouseup");
  map.removeAttribute("onmouseover");
  map.removeAttribute("onmouseout");
}

// Load a geography (set of maps) from the server
function loadGeography(geoid) {
  httpSVG( http_root+geoid+'.svg', loadedGeography, rejectedGeography, [geoid] );
}

// Called when our map loading is sucessful
function loadedGeography(xmltree, args) {
  var geoid = args[0];
  activateGeography(geoid)
  empty_element(map);
  var len = xmltree.childNodes.length;
  var count = 0;
  for( var x = 0; x < len; x++ ) {
    // This always adds the first node because they are removed as it goes.
    var child = xmltree.childNodes[0];
    if(child.localName=='g') {
      child.setAttribute('opacity', 0.5);
      map.appendChild(child);
      fade(child, false);
      count++;
    } else {
      xmltree.removeChild(child);
    }
  }
  if(count==1) {
    // Make sure the selection process is deactivated.
    deactivateSelection();
  } else {
    // Activate the selection process.
    activateSelection();
  }
}

function activateGeography(geoid) {
  if(parentStack[parentStack.length-1]==geoid) {
    // Going backwads!
    parentStack.pop();
  } else if(currentView) {
    // Forwards!
    parentStack.push(currentView);
  }
  currentView = geoid;
  if(parentStack.length>0 && back_button) {
    document.documentElement.appendChild(back_button);
    back_button = null;
  } else if(parentStack.length==0 && !back_button) {
    back_button = pop_element('back_button');
    back_button.setAttribute('onclick', "previousGeography()");
  }
}

function rejectedGeography(status, args) {
  geoid = args[0];
  activateGeography(geoid);
  alert("Unable to load map "+geoid+": "+status);
}

function previousGeography() {
  // We want to load the previous map
  if(parentStack.length>0) {
    loadGeography(parentStack[parentStack.length-1]);
  }
}

/* Called when ever we enter a new object on map */
function enterGeography(evt) {
  var geo = getGeography(evt.target);
  if(geo) {
    geo.setAttribute('class', 'hover');
  }
}

/* Called when the mouse leaves each object on the map */
function leaveGeography(evt) {
  var geo = getGeography(evt.target);
  if(geo) {
    geo.removeAttribute('class');
  }
}

/* Called when we need to select an object on the map */
function selectGeography(evt) {
  var geo = getGeography(evt.target);
  if(geo) {
    for( var x = 0; x < map.childNodes.length; x++ ) {
      var child = map.childNodes[x];
      if(child!=geo && child.localName=='g') {
        fade(child, true);
      }
    }
    setTimeout("loadGeography('"+geo.getAttribute('id')+"', false)", 2000);
    scaleUp(geo);
  }
}

function getGeography(target) {
  ele = element(target);
  if(ele.localName!='g') {
    ele = ele.parentNode;
  }
  if(ele.localName=='g' && ele.getAttribute('class')!='ignore') {
    return ele;
  }
  return null;
}

window.addEventListener("load", startOff, false);
