﻿// JScript File
var map;
    
function buildmap() 
	{
	if (GBrowserIsCompatible()) 
		{
                gmapOptions = {
                mapTypes: [G_NORMAL_MAP,G_PHYSICAL_MAP,G_HYBRID_MAP]
                }
		map = new GMap2(document.getElementById("map"),gmapOptions);
		// 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 GSmallMapControl());
		//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;
        });
//        var point = new GLatLng(43.616046, -116.199181);
//        var my_info = "New IDL location";
//        map.addOverlay(createMarker(point, my_info));
        // 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 business
        //   image2.alt = selected business type
        //alert("Almost there");
        var my_element = document.getElementById("Image2");//the HTML element used for passing data
        //alert(my_element.alt);
        var file_name="http://gis.idaho.gov/IdahoCampgrounds/" + my_element.alt;

        //alert(file_name);
        var sel_index=0
        var sel_index = my_element.title //retrieve the selected index from the vb code
        
        // get the marker information from the xml file
        GDownloadUrl(file_name, function(data, responseCode) {
        var xml = GXml.parse(data);
        //alert("I get here ok - but the next line is the problem");
        var markers = xml.documentElement.getElementsByTagName("marker");
        //alert(sel_index);
        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 = markers[i].getAttribute("b_name");
                var my_info = "Campsite: " + markers[i].getAttribute("b_name") + "<br /> Phone: " + markers[i].getAttribute("phone") + "<br /> Sites: " + markers[i].getAttribute("sites") + "<br /> Rate: " + markers[i].getAttribute("rate") ;
                if (i==sel_index)
                {
                map.addOverlay(createMarker(point, my_info));
                //map.addOverlay(new GMarker(point, icon_red));
                }
                else
                {
                map.addOverlay(createMarker(point, my_info, icon_green));
                //map.addOverlay(new GMarker(point, icon_green));
                }
            }
        }
        // display a marker for the new IDL location
//        var point = new GLatLng(43.616046, -116.199181);
//        var my_info = "New IDL location";
//        map.addOverlay(createMarker(point, my_info));
        });
    }

// 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);
    //document.getElementById("Image2").src = marker.getTitle();
  });
  return marker;
}
