var map_obj = "";
var map_mm = "";
var map_icon_red = "";
var map_icon_green = "";
var map_icon_skull = "";
var map_icon_skullflag = "";
var map_icon_star = "";
var map_icon_fairy = "";
var map_max_height = 500;

function load_google_map(url, name, site, addr, lat, lng, theme) {
    var width = update_page_size();
    if (GBrowserIsCompatible()) {
      var label = "<div style=\"padding-right: 10px; font-size:12px; font-family: arial;\">" + name + "<small>";
      if (site != "") {
      	label += "<br />" + site;
      }
      label += "<br />" + addr;
      label += "<br /><a target=\"_blank\" href=\"/cgi-bin/redir.cgi?" + url + "\">Web Site</a></small></div>";
      var mapdiv = document.getElementById("map");
      map_obj = new GMap2(mapdiv);
      var loc = new GLatLng(lat, lng);
      map_obj.setCenter(loc, 13);
      map_obj.addControl(new GMapTypeControl());
      map_obj.addControl(new RenControl());

      map_add_marker(map_obj, loc, label, name, false, true, theme);
    }
}

function load_states_map(selected_state) {
    var width = update_page_size();
    if (GBrowserIsCompatible()) {
      var mapdiv = document.getElementById("map");
      map_obj = new GMap2(mapdiv);
      var loc = new GLatLng(42.9378, -110.2549);
      map_obj.setCenter(loc, 3);
      map_obj.addControl(new GMapTypeControl());
      map_obj.addControl(new RenControl());
      //map_obj.addControl(new GSmallMapControl());

//var nlist = mapdiv.childNodes;
//for (var i = 0; i < nlist.length; i++) { alert(nlist[i].innerHTML); }

      var gen_geo_url = "xml_gen_geo.cgi";
      //if (selected_state) {
      //  gen_geo_url += "?state=" + selected_state;
      //}

      GDownloadUrl(gen_geo_url, function(data, responseCode) {
        var xml = GXml.parse(data);
        var markers = xml.documentElement.getElementsByTagName("marker");
        var bounds = new GLatLngBounds();
	// Now load all of the elements.
        for (var i = 0; i < markers.length; i++) {
          var id = parseInt(markers[i].getAttribute("id"));
          var title = markers[i].getAttribute("title");
          var state = markers[i].getAttribute("state");
          var theme = markers[i].getAttribute("theme");
          var lat = parseFloat(markers[i].getAttribute("lat"));
          var lng = parseFloat(markers[i].getAttribute("lng"));
          var loc = new GLatLng(lat, lng);
          var active = parseInt(markers[i].getAttribute("active"));
          map_add_marker(map_obj, loc, "<a href=\"site_format.cgi?db_id=" + id + "\">" + title + "</a>", title, active, false, theme);
	  if (!selected_state || state == selected_state) {
            bounds.extend(loc);
          }
        }
	// XXX - need to set bounds to something reasonable if they're unset
	// XXX - eg: no faire for the selected_state.
        var loc = bounds.getCenter();
	var zoom = map_obj.getBoundsZoomLevel(bounds);
	if (zoom > 5) { zoom--; }
	if (zoom > 10) { zoom = 10; }
        map_obj.setCenter(loc, zoom);
	//map_mm.refresh();
      });
    }
}

function map_add_marker(map, loc, text, title, active, info_open, theme) {
    if (map_icon_red == "") {
      map_init_icon();
    }
    var marker;
	if (theme == "Pirates") {
		if (active) {
			marker = new GMarker(loc, { icon: map_icon_skullflag, title: title });
		} else {
			marker = new GMarker(loc, { icon: map_icon_skull, title: title });
		}
	} else if (theme == "Fairies") {
		if (active) {
			marker = new GMarker(loc, { icon: map_icon_fairy, title: title });
		} else {
			marker = new GMarker(loc, { icon: map_icon_star, title: title });
		}
	} else if (active) {
		marker = new GMarker(loc, { icon: map_icon_green, title: title });
	} else {
		marker = new GMarker(loc, { icon: map_icon_red, title: title });
	}
    map.addOverlay(marker);
    //map_mm.addMarker(marker, 0, 17);
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(text);
    });
    if (info_open) {
      marker.openInfoWindowHtml(text);
    }
}

function map_init_icon() {
  //map_mm = new GMarkerManager(map_obj);

  map_icon_red = new GIcon();
  map_icon_red.image = "/images/mapcontrols/icon_15x21_rose.png";
  map_icon_red.shadow = "/images/mapcontrols/mm_20_shadow.png";
  map_icon_red.iconSize = new GSize(15, 21);
  map_icon_red.shadowSize = new GSize(22, 20);
  map_icon_red.iconAnchor = new GPoint(6, 21);
  map_icon_red.infoWindowAnchor = new GPoint(7, 1);

  map_icon_green = new GIcon();
  map_icon_green.image = "/images/mapcontrols/icon_30x35_rose.png";
  //map_icon_green.shadow = "/images/mapcontrols/mm_20_shadow.png";
  map_icon_green.iconSize = new GSize(30, 35);
  //map_icon_green.shadowSize = new GSize(22, 20);
  map_icon_green.iconAnchor = new GPoint(15, 35);
  map_icon_green.infoWindowAnchor = new GPoint(15, 1);

  map_icon_skull = new GIcon();
  map_icon_skull.image = "/images/mapcontrols/icon_13x20_skull.png";
  map_icon_skull.shadow = "/images/mapcontrols/mm_20_shadow.png";
  map_icon_skull.iconSize = new GSize(13, 20);
  map_icon_skull.shadowSize = new GSize(22, 20);
  map_icon_skull.iconAnchor = new GPoint(6, 20);
  map_icon_skull.infoWindowAnchor = new GPoint(7, 1);

  map_icon_skullflag = new GIcon();
  map_icon_skullflag.image = "/images/mapcontrols/icon_31x30_skullflag.png";
  //map_icon_skullflag.shadow = "/images/mapcontrols/mm_20_shadow.png";
  map_icon_skullflag.iconSize = new GSize(31, 30);
  //map_icon_skullflag.shadowSize = new GSize(22, 20);
  map_icon_skullflag.iconAnchor = new GPoint(2, 30);
  map_icon_skullflag.infoWindowAnchor = new GPoint(15, 1);

  map_icon_star = new GIcon();
  map_icon_star.image = "/images/mapcontrols/icon_14x20_yellowstar.png";
  map_icon_star.shadow = "/images/mapcontrols/mm_20_shadow.png";
  map_icon_star.iconSize = new GSize(14, 20);
  map_icon_star.shadowSize = new GSize(22, 20);
  map_icon_star.iconAnchor = new GPoint(6, 20);
  map_icon_star.infoWindowAnchor = new GPoint(7, 1);

  map_icon_fairy = new GIcon();
  map_icon_fairy.image = "/images/mapcontrols/icon_36x42_fairy.png";
  //map_icon_fairy.shadow = "/images/mapcontrols/mm_20_shadow.png";
  map_icon_fairy.iconSize = new GSize(36, 42);
  //map_icon_fairy.shadowSize = new GSize(22, 20);
  map_icon_fairy.iconAnchor = new GPoint(28, 42);
  map_icon_fairy.infoWindowAnchor = new GPoint(15, 1);
}

function RenControl() { }
if (typeof(GControl) == "function") {
  RenControl.prototype = new GControl();
}

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
RenControl.prototype.initialize = function(map) {
  var is_ie = /MSIE [56]/.test(navigator.userAgent) && navigator.platform == "Win32";
  var container = document.createElement("div");
  if (is_ie) {
    var trans_div = document.createElement("div");
    trans_div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/mapcontrols/uber_rose3.png', sizingMethod='crop')";
    trans_div.src = "/images/mapcontrols/uber_rose3.png";
    trans_div.style.border = "0px none";
    trans_div.style.padding = "0px";
    trans_div.style.width = "75px";
    trans_div.style.height = "119px";
    var Img = document.createElement("img");
    Img.src = "/images/mapcontrols/uber_rose3.png";
    Img.style.visibility = "hidden";
    trans_div.appendChild(Img);
    container.appendChild(trans_div);
  } else {
    var Img = document.createElement("img");
    Img.src = "/images/mapcontrols/uber_rose3.png";
    Img.style.border = "0px none";
    Img.style.padding = "0px";
    Img.style.position = "absolute";
    Img.style.left = "0px";
    Img.style.right = "0px";
    Img.style.width = "75px";
    Img.style.height = "119px";
    Img.style.MozUserSelect = "none";
    container.appendChild(Img);
  }

  this.createPan(map, container, is_ie, "Pan NW", 0+8, 0+35);
  this.createPan(map, container, is_ie, "Pan N", 20+8, 0+30);
  this.createPan(map, container, is_ie, "Pan NE", 40+8, 0+35);

  this.createPan(map, container, is_ie, "Pan W", 0+2, 20+35);

  this.createPan(map, container, is_ie, "Pan E", 40+13, 20+35);

  this.createPan(map, container, is_ie, "Pan SW", 0+8, 40+35);
  this.createPan(map, container, is_ie, "Pan S", 20+8, 40+40);
  this.createPan(map, container, is_ie, "Pan SE", 40+8, 40+35);

  this.createZoom(map, container, is_ie, "Out", 0+3, 40+60);
  this.createZoom(map, container, is_ie, "In", 40+8, 40+60);

  map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
RenControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}

RenControl.prototype.createPan = function(map, container, is_ie, title, L, T) {
  var div = document.createElement("div");
  container.appendChild(div); 
  var panx = 0; if (L < 20) { panx = 1; } else if (L > 40) { panx = -1; }
  var pany = 0; if (T < 40) { pany = 1; } else if (T > 60) { pany = -1; }
  GEvent.addDomListener(div, "click", function() {
    map.panDirection(panx, pany);
  });
  div.title = title;
  div.style.position = "absolute";
  div.style.left = L + "px";
  div.style.top = T + "px";
  div.style.width = "18px";
  div.style.height = "18px";
  div.style.cursor = "pointer";
  //div.style.border = "1px white solid";
  //div.appendChild(document.createTextNode("X"));
  if (is_ie) {
    div.style.backgroundColor = "white";
    div.style.filter = "alpha(opacity=1)";
  }
}

RenControl.prototype.createZoom = function(map, container, is_ie, title, L, T) {
  var div = document.createElement("div");
  container.appendChild(div); 
  GEvent.addDomListener(div, "click", function() {
    if (title == "In") {
      map.zoomIn();
    } else {
      map.zoomOut();
    }
  });
  div.title = title;
  div.style.position = "absolute";
  div.style.left = L + "px";
  div.style.top = T + "px";
  div.style.width = "18px";
  div.style.height = "18px";
  div.style.cursor = "pointer";
  //div.style.border = "1px white solid";
  //div.appendChild(document.createTextNode("X"));
  if (is_ie) {
    div.style.backgroundColor = "white";
    div.style.filter = "alpha(opacity=1)";
  }
}
