RegisterNamespace('NPR.Stations.RouteSegment');

NPR.Stations.RouteSegment = function(veRouteSegment)
{
    // Private Members
    var self = this;

    // Public Properties
    this.ArtificialDistance = 0;
    this.IsArtificial = false;
    this.Stations = [];
    this.VERouteSegment = veRouteSegment;
    
    // Public Methods
    this.LoadStations = function(callback)
    {
    	var stationFinderUri = '/templates/xanadu/view/api/stations.php';
    	stationFinderUri += '?lat=' + Math.abs(parseFloat(veRouteSegment.LatLong.Latitude));
		stationFinderUri += '&lon=' + Math.abs(parseFloat(veRouteSegment.LatLong.Longitude));
		    
        var xmlHttpRequest = GetXMLHttpRequest();

        xmlHttpRequest.onreadystatechange = function()
        {
            if(xmlHttpRequest.readyState == 4)
            {
                if(xmlHttpRequest.status == 200)
                {
                    var stations = xmlHttpRequest.responseXML.getElementsByTagName('station');
                    
                    for(var i = 0; i < stations.length; i++)
                    {
                        try
                        {
                            var station = NPR.Stations.Station.FromDom(stations[i]);
                            
                            // For route segments, we're only interested in medium and strong signal strenth stations
                            if(station.SignalStrength > 1)
                            {
                                self.Stations[station.Id] = station;
                            }
                        }
                        catch(ex)
                        {
                        }          
                    }
                }
                
                callback();
            }      
        }
        
        // TODO: No need for proxy once in production @ NPR. Remove this line. 
        //uri = 'Proxy.ashx?uri=' + encodeURIComponent(uri);
        
        xmlHttpRequest.open("GET", stationFinderUri);
        xmlHttpRequest.send(null);
    }
}