function updateLocalDetails()
{
    if (!currentBoundary)
    {
        return;
    }
    $('.locationName').text(currentBoundary.BoundaryName);
    if (currentBoundary.Population)
    {
        $('.locationPopulation').text(FormatNumber(currentBoundary.Population,0,true,false,true));
    }
    if (currentBoundary.Households)
    {
        $('.locationHouseholds').text(FormatNumber(currentBoundary.Households,0,true,false,true));
    }
    if(currentBoundary.DetailsUri)
    {
        $('.locationLocalUri').attr('href',currentBoundary.DetailsUri);
    }
}

function getYAHData(shape,place){
    logAction('/YouAreHere.ashx?' + 'POINT(' + place.LatLong.Longitude + ' ' + place.LatLong.Latitude + ')');
    $.ajax(
        {
            type:"GET",
            url: "YouAreHere.ashx",
            data:{
                WKT:'POINT(' + place.LatLong.Longitude + ' ' + place.LatLong.Latitude + ')'
            },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(json)
            {
                if(!json.Boundary || json.Boundary==null)
                {
                    shape.SetDescription('Unable to find any information about this location');
                }
                else{
                    var b=json.Boundary;
                    currentBoundary=json.Boundary;
                    updateLocalDetails();
                    if(b.BeatcrimeEnabled){
                        
                        shape.SetDescription('<div>Your local policing area is <b>' + b.BoundaryName + '</b></div><div>To find out what is happening in ' + place.Name + ' please visit the <a href="' + b.DetailsUri + '" onclick="window.open(this.href);return false;">your local policing pages</a>.</div>');
                    }else{
                        shape.SetDescription('<div>This area is not covered by beatcrime.info</div><div>For crime mapping in ' + place.Name + ' please visit the crime mapping website for <a href="' + b.DetailsUri + '">' + b.BoundaryName +'</a>.</div>');    
                    }
                }
                map.ShowInfoBox(shape);
                
            }
        }
    );
}

function displayYAH(layer, resultsArray, places, hasMore, veErrorMessage)
{
    if(veErrorMessage)
    {
        alert(veErrorMessage);
    }
    if(hasMore)
    {
        throw new Error("Multiple address were returned.");
    }
    var place,youAreHere;
    if(!places || places.length==0)
        return;
        
    place=places[0];
    youAreHere=new VEShape(VEShapeType.Pushpin,place.LatLong);
    youAreHere.SetTitle(place.Name);
    youAreHereLayer.AddShape(youAreHere);
    getYAHData(youAreHere,place);
}

function addressSearch()
     {
     
        try
        {
           logAction('/addressSearch?' + $('#address').val());
           results = map.Find(null,
                              $('#address').val(),
                              null,
                              null,
                              0,
                              9,
                              true,
                              true,
                              true,
                              true,
                              displayYAH);
        }
        catch(e)
        {
           alert('An Error occurred finding your address.');
        }
     }