var gdir;
var map;
var icon;
var icon_active;
var icon_photo;
function initIcon(src) {
    var i = new GIcon();
    i.image = src;
    i.iconSize = new GSize(16, 16);
    i.iconAnchor = new GPoint(8, 8);
    i.infoWindowAnchor = new GPoint(8, 8);
    return i;
}
function createMarker(point, icon, text, showInfo) {
    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(text);
    });
    if (showInfo)
        marker.openInfoWindowHtml(text);
    return marker;
}
function initMap(id) {
    if (GBrowserIsCompatible()) {
        icon = initIcon("/images/house.gif");
        icon_active = initIcon("/images/house_active.gif");
        icon_photo = initIcon("/images/house_photo.gif");

        map = new GMap2($(".map")[0]);
        gdir = new GDirections(map, $(".directions")[0]);
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        $.getJSON('/data/mapData2.ashx?id=' + id,
        function(data) {
            info = data;
            setCenter(info.P.ID, info.P.LA, info.P.LO, info.P.N, info.P.A, info.P.C, info.P.S, info.P.Z, info.P.T);
            $.each(info.N, function() {
                addPoint(this.LA, this.LO, this.N, this.A, this.C, this.S, this.Z, this.U, this.T);
            });
        });
    }
}
function getDirections() {
  gdir.load("from: " + $("#from").val() + " to: " + $("#to").val(),
            { "locale": "en-US" });
}
function onGDirectionsLoad() {
}
function addPoint(lat, lon, name, address, city, state, zip, url, src) {
    if (src != "")
        src = "<img src=\"" + src + "\" style='padding-right: 5px;width:110px;height:82px;' />";
    map.addOverlay(createMarker(new GLatLng(lat, lon), (src != "" ? icon_photo : icon), '<br /><table><tr><td style="border: 0px">' + src + '</td><td style="border: 0px; text-align: left"><b>' + name + '</b><br />' + address + "<br />" + city + "&nbsp;," + state + "&nbsp;" + zip + '<br /><br /><br /><a href="' + url + '">View Property Info</a></td></tr></table>', false));
}
function setCenter(id, lat, lon, name, address, city, state, zip, src) {
    var point;
    var txt;
    if (src != "")
        src = "<img src=\"" + src + "\" style='float: left;padding-right: 5px;width:110px;height:82px;' />";
    txt = '<br /><table><tr><td style="border: 0px">' + src + '</td><td style="border: 0px; text-align: left"><b>' + name + '</b><br />' + address + "<br />" + city + ",&nbsp;" + state + "&nbsp;" + zip + '</td></tr></table>';
    if (lat != 0 && lon != 0) {
        point = new GLatLng(lat, lon);
        map.setCenter(point, 13);
        var selProp = createMarker(point, icon_active, "<b>" + name + "</b><br />" + address + "<br />" + city + ",&nbsp;" + state + "&nbsp;" + zip, false);
        map.addOverlay(selProp);
        selProp.openInfoWindowHtml(txt);
    }
    else {
        var geocoder = new GClientGeocoder();
        geocoder.getLatLng(
          address + ", " + city + ", " + state + " " + zip,
          function(pnt) {
              if (pnt) {
                  map.setCenter(pnt, 13);
                  $.getJSON('/data/SetPropertyCoordinates.ashx?id=' + id + '&lat=' + pnt.lat() + '&lon=' + pnt.lng(), function(data) { });
                  var marker = createMarker(pnt, icon_active, "<b>" + name + "</b><br />" + address + "<br />" + city + ",&nbsp;" + state + "&nbsp;" + zip, false);
                  map.addOverlay(marker);
                  marker.openInfoWindowHtml(txt);
              }
          });
    }
    $('#to').val(address + " " + city + " ," + state + " " + zip);
}
function handleErrors() {
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    else alert("An unknown error occurred.");
}