﻿/// <reference name="MicrosoftAjax.js" />

var geocoder;
var address 
var gpoint;
var gplace;

function btnAddress_onclick() {
    address = $get("txtAddress").value + " " + $get("communities").value;
    
    
    if ( address.length>0 && address.trim()!="Anchorage, AK" ) 
        geocode(address);
    else
         map.setCenter(new GLatLng(61.186553, -149.890659), 11);
}


function geocode(input) {
    geocoder = new GClientGeocoder();
    geocoder.getLocations(input, onGeocodeResult);
}

//This response will call a map center which will trigger a moveend event and populate the map with crime points and centered address icon based on global gplace and gpoint
function onGeocodeResult(response) {
    if (response.Status.code !=601 &&  response.Status.code !=602)
    {
        gplace = response.Placemark[0];
        //Validate that point is in MOA:
        var lat=gplace.Point.coordinates[1];
        var lng=gplace.Point.coordinates[0];
        
        if(lat>60.9043 && lat<61.48666 && lng>-150.1392 && lng<-148.93204){
             gpoint = new GLatLng(gplace.Point.coordinates[1], gplace.Point.coordinates[0]);
             centerMap();
          }
         else {
            gpoint = new GLatLng(61.216553, -149.890659);
            map.setCenter(new GLatLng(61.216553, -149.890659), 10);
            alert("Unable to locate this address.  Please try again. ");
        }
          
      
     } 
     else {
         map.setCenter(new GLatLng(61.216553, -149.890659), 10);
         alert("Unable to locate this address.  Please try again. ");
    }
}


function centerMap(){
   // Center the map on this point
    map.setCenter(gpoint, 13);
    }
    
function addAddressMarker(){
   var mIcon= new GIcon(baseIcon);
    mIcon.image= "markers/map-pin.png" ;
    mIcon.shadow= "" ;
    
    // Create a marker and add listener
     marker = new GMarker(gpoint, mIcon);
     GEvent.addListener(marker,"click", function(){ marker.openInfoWindowHtml('<div style="">' + address + '</div>')});

     // Add the marker to map
     map.addOverlay(marker);

     }
