﻿//<![CDATA[

// Global variables
var map; 
var ctlZoom; 
var ctlMapType;   
var ctlOverview;

var aMarkers = new Array();

// Object contructor function
function siteMarker(id, lat, lng)
{
    var point = new GLatLng(lat, lng);
    var marker = new GMarker(point,{draggable: true});

    oTab1 = document.getElementById('site_' + id)
    oTab2 = document.getElementById('description_' + id)
    
    var infoTabs = [
        new GInfoWindowTab("Site", oTab1),
        new GInfoWindowTab("Description", oTab2)
        ];

    this.id = id;
    this.lat = lat;
    this.lng = lng;
    this.point = point;
    this.marker = marker;
    this.infoTabs = infoTabs;
}

// Places a marker on the map and setup the onClick event handler.
function PlaceMarker(i) {
    
    var marker = aMarkers[i].marker;
    
    // make sure we have a valid marker
    if (marker == null)
        return;
        
    GEvent.addListener(marker, "dragstart", function() {
      map.closeInfoWindow();
      });

    GEvent.addListener(marker, "dragend", function() {
        var point = marker.getPoint()
        
        window.document.all('xLatitude').innerText = point.y;
        window.document.all('xLongitude').innerText = point.x;
        
        window.document.all('ctl00$ContentPlaceHolder1$Site_FormView$Latitude_TextBox').innerText = point.y;
        window.document.all('ctl00$ContentPlaceHolder1$Site_FormView$Longitude_TextBox').innerText = point.x;
      });
    
    // place the marker on the map
    map.addOverlay(marker);
    
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowTabs(aMarkers[i].infoTabs);
    });
}

function load_map()
{
    ctlZoom = new GLargeMapControl();
    ctlMapType = new GMapTypeControl();
    ctlOverview = new GOverviewMapControl(new GSize(200, 150));    
    
    if (map == null) 
    {
        var oMap = document.getElementById("map")
        if (oMap == null) 
            return;
            
        map = new GMap2(oMap);
        map.addControl(ctlZoom);
        map.addControl(ctlMapType);
        map.addControl(ctlOverview);

        // hide the mini-map
        ctlOverview.hide();
        
        var o = window.document.all('ctl00_ContentPlaceHolder1_MapInfo_Input')
        point = new GLatLng(o.Latitude, o.Longitude);
        map.setCenter(point, 16);

        aMarkers[o.SiteID] = new siteMarker(o.SiteID, o.Latitude, o.Longitude);     
        PlaceMarker(o.SiteID);
    }
}

function select_site(id)
{
    var marker = aMarkers[id].marker
    var point = aMarkers[id].point;
    var infoTabs = aMarkers[id].infoTabs;
    
    map.clearOverlays();
    map.addOverlay(marker);
    map.setCenter(point, 16);

    marker.openInfoWindowTabs(infoTabs);
}

function set_value(s)
{
    var o = window.document.all('ctl00_Main_ctl00_FormView2_' + s)
    o.value = window.document.all('x' + s).innerText;
}

function locate_address() 
{
    var address = window.document.all('ctl00_ContentPlaceHolder1_Site_FormView_Address_TextBox').value
    var city = window.document.all('ctl00_ContentPlaceHolder1_Site_FormView_City_TextBox').value
    var oState = window.document.all('ctl00_ContentPlaceHolder1_Site_FormView_State_DropDownList')
    var state = oState.options(oState.selectedIndex).value
    var full_address = address + ' ' + city + ', ' + state 
    
    var geocoder = new GClientGeocoder();
    geocoder.getLatLng
    (
        full_address,
        function(point) 
        {
          if (!point) 
          {
            alert("Address not found");
          } 
          else 
          {
            map.setCenter(point, map.getZoom());
            var marker = new GMarker(point);
          }
        }
    );
    geocoder.getLocations(full_address, addAddressToMap)        
}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
                        place.Point.coordinates[0]);

    marker = new GMarker(point,{draggable: true});

    var zip = '';
    
    try {
        //check to make sure which element exists... it varies based upon input
        if (place.AddressDetails.Country.AdministrativeArea.Locality != undefined)
            if (eval(place.AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber))
                zip = place.AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber;

        if (place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality != undefined)
            if (eval(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber))
                zip = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
    }
    catch (err) {
    
    }
    
    var div = "<div style='width:200px;padding:10px'>Position the marker directly over your desired location. <br/><br/>Click here to <a href='javascript:SetMarker()'>Set Marker</a> when done. </div>'"

    GEvent.addListener(marker, "dragstart", function() {
      map.closeInfoWindow();
      });

    GEvent.addListener(marker, "dragend", function() {
        var point = marker.getPoint()
        
        window.document.all('xLatitude').innerText = point.y;
        window.document.all('xLongitude').innerText = point.x;
        window.document.all('xZip').innerText = zip;
        
        window.document.all('ctl00$ContentPlaceHolder1$Site_FormView$Latitude_TextBox').innerText = point.y;
        window.document.all('ctl00$ContentPlaceHolder1$Site_FormView$Longitude_TextBox').innerText = point.x;
        window.document.all('ctl00$ContentPlaceHolder1$Site_FormView$Zip_TextBox').innerText = zip;
      });
      
      
    var o = window.document.all('ctl00_ContentPlaceHolder1_MapInfo_Input')
    o.Latitude = point.y
    o.Longitude = point.x

    window.document.all('xLatitude').innerText = point.y;
    window.document.all('xLongitude').innerText = point.x;
    window.document.all('xZip').innerText = zip;
    
    window.document.all('ctl00$ContentPlaceHolder1$Site_FormView$Latitude_TextBox').innerText = point.y;
    window.document.all('ctl00$ContentPlaceHolder1$Site_FormView$Longitude_TextBox').innerText = point.x;
    window.document.all('ctl00$ContentPlaceHolder1$Site_FormView$Zip_TextBox').innerText = zip;
    
    map.addOverlay(marker);
  }
}

function SetMarker()
{
  map.clearOverlays();
}
//]]>
