/*
geoquiz_googlemap.js
Author: Larry Ching
Date Created: Mar 11, 2007
Creates and updates map for geoquiz using Google Maps api.
History:
  April 18, 2007: 
*/

function showWorldMap(){
// Shows initial map of the world.
// Initializes net.map.
  if (GBrowserIsCompatible()) {
    net.mapzoomptr = null;
    net.maptypeptr = null;
    net.map = new GMap2(document.getElementById("mapBlock"));
    net.map.setCenter(new GLatLng(37.4419, -122.1419), 1);
    GEvent.addListener(net.map, "zoomend", zoomChanged);
    GEvent.addListener(net.map, "maptypechanged", typeChanged);
  }else{
  // browser not compatible
  // Do nothing.
  }
}

function updateMap(countryindex){
// Given the index of the country in the XML file, displays the map 
// of that country.
  if (GBrowserIsCompatible()) {
    var countryArray = net.geoquiz_xmlDoc.getElementsByTagName("Country");
    var zoomstr = countryArray[countryindex].getAttribute("zoom");
    document.getElementById("zoomlevel").firstChild.data = zoomstr;
    net.map.clearOverlays();
    net.map.setCenter( new GLatLng(countryArray[countryindex].getAttribute("latitude"), countryArray[countryindex].getAttribute("longitude")), parseInt(zoomstr) );
  }else{
    // browser not compatible
    // Do nothing.
  }
}

function showMarker(markertext){
   if (GBrowserIsCompatible()) {
      var geocoder = new GClientGeocoder();
      var address = (markertext + ", " + net.countrystring);
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
            alert(address + " not found");
          }else{
            var WINDOW_HTML = '<div style="font-size: 0.8em; padding-right: 10px">' + markertext + '.<\/div>';
            var marker = new GMarker(point);
            var presentzoom = net.map.getZoom();
            net.map.setCenter(point);
            net.map.setZoom(presentzoom + 2);
            net.map.addOverlay(marker);
            // marker.openInfoWindowHtml(WINDOW_HTML);
          }
        });
    }else{
      // browser not compatible, do nothing
    }

}

function toggleZoom(action){
  var tbutton = document.getElementById("togglezoomBtn");
  if (action == "show") {
    net.mapzoomptr = new GSmallMapControl();
    net.map.addControl(net.mapzoomptr);
    tbutton.setAttribute("value","Hide Zoom");
    tbutton.onclick = function() { toggleZoom('hide'); };
  }else if (action == "hide"){
    net.map.removeControl(net.mapzoomptr);
    tbutton.setAttribute("value","Show Zoom");
    tbutton.onclick = function() { toggleZoom('show'); };
  } 
}


function toggleType(action){
  var tbutton = document.getElementById("toggletypeBtn");
  if (action == "show") {
    net.maptypeptr = new GMapTypeControl();
    net.map.addControl(net.maptypeptr);
    tbutton.setAttribute("value","Hide Map Type");
    tbutton.onclick = function() { toggleType('hide'); };
  }else if (action == "hide"){
    net.map.removeControl(net.maptypeptr);
    tbutton.setAttribute("value","Show Map Type");
    tbutton.onclick = function() { toggleType('show'); };
  } 
}

function zoomChanged() {
  var themap = this ;
  var newzoom = themap.getZoom() ;
  document.getElementById("zoomlevel").firstChild.data = newzoom;
}

function typeChanged() {
  var newtype = this.getCurrentMapType();      
  if (newtype == G_NORMAL_MAP){
    document.getElementById("maptype").firstChild.data = "Map";
  }else if (newtype == G_SATELLITE_MAP){
    document.getElementById("maptype").firstChild.data = "Satellite";
  }else if (newtype == G_HYBRID_MAP){
    document.getElementById("maptype").firstChild.data = "Hybrid";
  };
}


