﻿// JScript File
var map;

function atest(lats,longs,mydata)
	{
	document.write("Points:");
	document.write(lats.length);
	}
	
function buildmap(lats,longs,mydata,selitem) 
	{
	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(lats,longs,mydata,selitem);
        }
    else
		{
		alert("This page is not compatible with your browser.")
		};
    }
    
function addpoints(lats,longs,mydata,selitem)
    {
        // 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 = "pics/mm_20_3d_green.png";
        icon_green.shadow = "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

		//if an item was selected - calculate its index
		if (selitem > 0){
			var sel_index=selitem-1;
		}
		else{
			var sel_index=-1;
		}
        //sel_index = my_element.title; //retrieve the selected index passed from the vb code through Image2.title
        // display the point of interest
		//var point = new GLatLng(lats[1], longs[1]);
		//map.addOverlay(createMarker(point));
        // 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");
		//document.write(sel_index);
		var my_info="";
        if (lats.length > 0){
            for (var i = 0; i < lats.length; i++) {
                var point = new GLatLng(lats[i],longs[i]);
				my_info = mydata[i];
				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;
}
