RegisterNamespace('NPR.Stations');

var map = null;
var stations = [];
var zIndex = 1001;
NPR.Stations.MapDisplayed = true;


// Clears all content from the map and any displayed lists of stations, directions, or MyAddress values
NPR.Stations.Clear = function()
{
    // Clear any coverage areas from the map
    NPR.Stations.Coverage.Clear();
    
    // Clear any directions from the map
    NPR.Stations.Directions.Clear();
    
    // Clear any 'My Address' values displayed
    NPR.Stations.MyAddress.Clear();
}

// Returns a count of all the stations hashed by Id
NPR.Stations.GetStationCount = function()
{
    var count = 0;
    
    for(var stationId in stations)
    {
        count++;
    }
    
    return count;
}

// Hides all the coverage areas from the map
NPR.Stations.HideAllCoverage = function()
{
    for(var stationId in stations)
    {
        stations[stationId].HideCoverage();
    }
}

NPR.Stations.Localize = function(address)
{
    var localizedAddress = address;
    
    if(!localizedAddress.match(/\, United States$/i))
    {
        localizedAddress += ", United States";
    }
    
    return localizedAddress;
}

// Hides all the pushpins from the map
NPR.Stations.HideAllPushpins = function()
{
    for(var stationId in stations)
    {        
        try
        {
            stations[stationId].HidePushpin();
        }
        catch(e){}
    }
}

// Loads the Microsoft Virtual Earth map         
function InitializeMap()
{
	try
	{
	    map = new VEMap('Map');
	    map.onLoadMap = OnMapLoaded;
	    map.SetDashboardSize(VEDashboardSize.Small);
	    map.LoadMap();
	    
	    map.AttachEvent("onclick", OnClick);
	    map.AttachEvent("onmouseout", OnMouseOver);
	    map.AttachEvent("onmouseover", OnMouseOver);
    }
    catch(e)
    {
    	// if we get an error initializing the map
    	$('#Map').html('Your browser does not support the map software NPR uses to display station coverage and road trip information.');
    	$('#nprRoadTripTab').hide();
    	$('#myAddressTab').hide();
    	NPR.Stations.MapDisplayed = false;
    }
}

function OnMapLoaded()
{    
    // Load the stations
    NPR.Stations.Coverage.ParseQueryString();
    map.AddShapeLayer(NPR.Stations.Coverage.Layer);
}

function OnClick(mapEvent)
{
    if(mapEvent && mapEvent.elementID)
    {
        var station = GetStationByElementId(mapEvent.elementID);
        
        if(station)
        {
            station.ShowCoverage(true);
            station.ShowInfoBox();
            return true;
        }
    }
}

function OnMouseOut(mapEvent)
{
    if(mapEvent && mapEvent.elementID)
    {
        var station = GetStationByElementId(mapEvent.elementID);
        
        if(station)
        {
            return true;
        }
    }
}

function OnMouseOver(mapEvent)
{
    if(mapEvent && mapEvent.elementID)
    {
        var station = GetStationByElementId(mapEvent.elementID);
        
        if(station)
        {
            station.ShowCoverage(false);
            return true;
        }
    }
}

function GetDistanceUnitAbbreviation(distanceUnit)
{
    switch(distanceUnit)
    {
        case VERouteDistanceUnit.Mile:
            return "mi";
            break;
            
        case VERouteDistanceUnit.Kilometer:
            return "km";
            break;
    }
}

function GetStationByElementId(elementId)
{
    for(var stationId in stations)
    {        
        if((stations[stationId].Pushpin) && (stations[stationId].Pushpin.GetID() == map.GetShapeByID(elementId).GetID()))
        {
            return stations[stationId];
        }
    }

    return null;
}

// Override the VEMap.ShowMessage method to do nothing.  This prevents Virtual Earth messages from being displayed on the map.
VEMap.prototype.ShowMessage = function(message) { }

VEMap.prototype.ZoomToFit = function()
{
    var items = new Array();

    for (var i = 0; i < this.GetShapeLayerCount(); i++)
    {
        items.push(this.GetShapeLayerByIndex(i).GetBoundingRectangle());
    }
    
    if(items.length > 0)
    {
        this.SetMapView(items);
    }
}

VEShape.prototype.Clone = function()
{
    var shape = new VEShape(this.GetType(), this.GetPoints());
    
    shape.SetFillColor(this.GetFillColor());
    shape.SetLineColor(this.GetLineColor());
    shape.SetLineWidth(this.GetLineWidth());
    
    return shape;
}

AddEvent(window, 'load', InitializeMap);
