﻿// JScript File
var map;
    
function buildmap() 
	{
	if (GBrowserIsCompatible()) 
		{
		map = new GMap2(document.getElementById("map"));
		// locate the map center near the Boise capitol building
		map.setCenter(new GLatLng(45.5, -116.1992), 6);
		// add the zoom control
		map.addControl(new GLargeMapControl());
		//add the map type control
        map.addControl(new GMapTypeControl());
        
        // display the mouse lat/long coords when mouse is moved
        GEvent.addListener(map, "mousemove", function(point) {
          var latlng_str = point.y.toFixed(6)+"  "+point.x.toFixed(6);
          document.getElementById("centercoords").innerHTML = "Mouse position: " + latlng_str;
        });

        // display all of the points of interest
        addpoints(0);
        }
    else
		{
		alert("This page is not compatible with your browser.")
		};
    }
    
function addpoints(selected_index)
    {
        // Create our "tiny" marker icons
        var icon_red = new GIcon();
        icon_red.image = "http://gis.idaho.gov/GNIS/pics/mm_20_3d_red.png";
        icon_red.shadow = "http://gis.idaho.gov/GNIS/pics/mm_20_shadow.png";
        icon_red.iconSize = new GSize(12, 20);
        icon_red.shadowSize = new GSize(22, 20);
        icon_red.iconAnchor = new GPoint(6, 20);
        icon_red.infoWindowAnchor = new GPoint(5, 1);

        var icon_green = new GIcon();
        icon_green.image = "http://gis.idaho.gov/GNIS/pics/mm_20_3d_green.png";
        icon_green.shadow = "http://gis.idaho.gov/GNIS/pics/mm_20_shadow.png";
        icon_green.iconSize = new GSize(12, 20);
        icon_green.shadowSize = new GSize(22, 20);
        icon_green.iconAnchor = new GPoint(6, 20);
        icon_green.infoWindowAnchor = new GPoint(5, 1);
        
        var icon_blinker = new GIcon();
        icon_blinker.image = "http://gis.idaho.gov/GNIS/pics/pointer_blinker.gif";
        icon_blinker.shadow = "http://gis.idaho.gov/GNIS/pics/mm_20_shadow.png";
        icon_blinker.iconSize = new GSize(12, 20);
        icon_blinker.shadowSize = new GSize(22, 20);
        icon_blinker.iconAnchor = new GPoint(6, 20);
        icon_blinker.infoWindowAnchor = new GPoint(5, 1); 
        
        // get the data passed from vb (stored in image2 parameters)
        //   image2.title = number of the selected county seat from list
        //   image2.alt = the path of the xml file constructed by VB
        var my_element = document.getElementById("Image2");//the HTML element used for passing data
        var file_name="http://gis.idaho.gov/IdahoCountySeats/" + my_element.alt; //the xml file path

        var sel_index=0;
        sel_index = my_element.title; //retrieve the selected index passed from the vb code through Image2.title
        
        // get the marker information from the xml file
        GDownloadUrl(file_name, function(data, responseCode) {
        var xml = GXml.parse(data);
        var markers = xml.documentElement.getElementsByTagName("marker");
        if (markers.length > 0){
            for (var i = 0; i < markers.length; i++) {
                var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
                var my_info = "<b>County: " + markers[i].getAttribute("county") + "</b><br /> County Seat: " + markers[i].getAttribute("b_name") + "<br /> Address: " + markers[i].getAttribute("address") + "<br /> Hours: " + markers[i].getAttribute("hours") ;
                var my_website = markers[i].getAttribute("website")                
                if (my_website != ""){ //show web link if available
                    my_info = my_info + "<br /> Website: <a href=http://" + my_website + " target=_blank>" + my_website + "</a>";
                }
                
                if (i==sel_index){
                    map.addOverlay(createMarker(point, my_info));
                }
                else {
                    map.addOverlay(createMarker(point, my_info, icon_green));
                }
            }
        }
        }); //end of GDownloadUrl
    } //end of function addpoints

// Creates a marker at the given point with the given number label
function createMarker(point, my_info, my_icon) {
  var marker = new GMarker(point, my_icon);
  
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(my_info);
  });
  
  GEvent.addListener(marker, "dblclick", function(my_url) {
    map.setZoom(15);
    map.panTo(point);
  });
  
  return marker;
}
