
// todo: change these four variables as necessary
window.image_icon_url = 'bullet.gif';
window.image_size = 7;
window.zoom_dropoff = 3;
window.released = true;
window.map_type = 1; // 0 for normal, 1 for terrain, 2 for satellite

window._max = function(a,b) {
  return a > b ? a : b;
}

window.initialize = function() {
  var map_types = [G_NORMAL_MAP, G_PHYSICAL_MAP, G_SATELLITE_MAP];
  // create the map for centering and zooming for the country
  window.map = new google.maps.Map2(document.getElementById("map"));
  map.setMapType(map_types[map_type]);
  window.mapMarker = new GMarkerManager(map);
  map.addControl(new GSmallMapControl());
  
  // draw the overlays
  drawOverlays();
  
  if (released) {
    // pan and zoom the map
    selectCountryByLocation(country);
  } else {
    // generate a select box for selecting the country (which changes the hash
    // so we can use the url update code)
    var select = document.getElementById('options');
    var option = null;
    for (var value in center_zoom) {
      option = document.createElement('option');
      option.value = value;
      option.innerHTML = value;
      select.appendChild(option);
    }
    // force an url change
    select.selectedIndex = 0;
    selectChange(select);
  }
};

window.selectCountryByLocation = function(opt_loc) {
  // check the url to choose the proper zoom and centering of the map
  var loc = window.location.toString();
  //alert(opt_loc);
  if (opt_loc) {
    loc = center_zoom[opt_loc];
  } else {
    var index = loc.lastIndexOf('/');
    index = _max(loc.lastIndexOf('#'), index);
    loc = loc.slice(index+1, loc.length);
    loc = center_zoom[loc];
  }
  map.setCenter(new google.maps.LatLng(loc[0], loc[1]), loc[2]);
}

window.drawOverlays = function() {
  // draw all of the map markers
  var marker = null;
  var icon = new GIcon(G_DEFAULT_ICON);
  icon.shadow = icon.image = image_icon_url;
  icon.iconSize = new GSize(image_size,image_size);
  icon.shadowSize = new GSize(image_size,image_size);
  icon.iconAnchor = new GPoint(parseInt(image_size/2),parseInt(image_size/2));
  var idm1 = image_size-1;
  icon.imageMap = [0,0, idm1,0, idm1,idm1, 0,idm1];
  for (var option in capitals_gps) {
    marker = new GMarker(new GLatLng(capitals_gps[option][0], capitals_gps[option][1]), {'icon':icon});
    GEvent.addListener(marker, 'click', _onclick(option));
    mapMarker.addMarker(marker, _max(center_zoom[option][2]-zoom_dropoff, 0));
  }
};

window._onclick = function(option) {
  return function() {selectCountry(option);};
};

window.selectCounry = function(country) {
  // this function is overridden below for testing/development
  window.location = 'http://countrycode.org/' + country;
};

if (!released) {
  // these are helpers for the unreleased version
  window.selectCountry = function(country) {
    // selects the country based on an overlay click.
    var sel = document.getElementById('options');
    for (var i=0; i<sel.options.length; i++) {
      if (sel.options[i].value == country) {
        sel.selectedIndex = i;
        selectChange(sel);
        break;
      }
    }
  };

  window.selectChange = function(sel) {
    // re-center the left map based on the country
    var option = sel.options[sel.selectedIndex].value;
    window.location.hash = option;
    selectCountryByLocation();
  };
}
