RegisterNamespace('NPR.Stations.MyAddress');

NPR.Stations.MyAddress.ResultsElement = null;
NPR.Stations.MyAddress.Pushpin = null;

NPR.Stations.MyAddress.Clear = function()
{
    // Clear results element
    if(NPR.Stations.MyAddress.ResultsElement)
    {
        NPR.Stations.MyAddress.ResultsElement.innerHTML = '';
        NPR.Stations.MyAddress.ResultsElement.onclick = null;
    }
    
    // Remove previous pushpin if it exists
    try
    {
        map.DeleteShape(NPR.Stations.MyAddress.Pushpin);
        NPR.Stations.MyAddress.Pushpin = null;
    }
    catch(ex)
    {
    }
}

NPR.Stations.MyAddress.HideInfoBox = function()
{
    map.HideInfoBox(NPR.Stations.MyAddress.Pushpin);
}

NPR.Stations.MyAddress.OnFindComplete = function(shapeLayer, findResults, places, moreResults, errorMessage)
{    
    NPR.Stations.MyAddress.Clear();

    if (!places || places.length == 0)
    {
        NPR.Stations.MyAddress.ResultsElement.innerHTML = 'Location not found.';
    }
    else if (places.length == 1)
    {
        NPR.Stations.MyAddress.Show(places[0]);
    }
    else if(places.length > 1)
    {
        // Build the ambiguous list
        var details = '<h2>Did you mean?</h2>';
        details += '<ul class="AmbiguousAddresses">';
        
        for(var i = 0; i < places.length; i++)
        {
            details += '<li><a href="#" onclick="NPR.Stations.MyAddress.Show({\'Name\':\'' + places[i].Name + '\', \'LatLong\':new VELatLong(' + places[i].LatLong.Latitude + ',' + places[i].LatLong.Longitude + ')}); return false;">' + places[i].Name + '</a></li>';
        }
        
        details += '</ul>';
        
        NPR.Stations.MyAddress.ResultsElement.innerHTML = details;
    }
}

NPR.Stations.MyAddress.Show = function(vePlace)
{
    // Add the puspin to the map
    NPR.Stations.MyAddress.Pushpin = new VEShape(VEShapeType.Pushpin, vePlace.LatLong);
    NPR.Stations.MyAddress.Pushpin.SetCustomIcon(NPR.Stations.Settings.MyAddressPushpinImageUri);
    NPR.Stations.MyAddress.Pushpin.SetDescription(vePlace.Name);
    NPR.Stations.MyAddress.Pushpin.SetTitle('My Address');
    map.AddShape(NPR.Stations.MyAddress.Pushpin);
    map.ZoomToFit();
    
    // Set the MyAddress results
    var details = '<img alt="" class="Icon" src="' + NPR.Stations.Settings.MyAddressPushpinImageUri + '" />';
    details += '<span class="Address"><h3>My Address</h3>' + vePlace.Name + '</span>';
    NPR.Stations.MyAddress.ResultsElement.innerHTML = details;
    
    // Set the mouse events for the results element
    NPR.Stations.MyAddress.ResultsElement.onclick = NPR.Stations.MyAddress.ShowInfoBox;
}

NPR.Stations.MyAddress.Search = function(value, resultsElement)
{
    if(value == '') 
    {
        alert('Please enter your address.');
    }
    else
    {
        value = NPR.Stations.Localize(value);
        map.Find(null, value, null, null, 0, 10, false, false, false, false, NPR.Stations.MyAddress.OnFindComplete);
    }
}

NPR.Stations.MyAddress.ShowInfoBox = function()
{
    map.ShowInfoBox(NPR.Stations.MyAddress.Pushpin);
}