﻿
var stationList;
var stationHostControl;
var stationPopup;

var stationScrollOffset = 14;

var stationRecords = new Array();

function OnStationPopupLoaded(sender)
{
    stationList = sender.findName("StationList");
    stationHostControl = sender.getHost();
    stationPopup = sender.findName("StationPopup");
    
//    // Test code
//    var stations = new Array();
//    stations[0] = "Germantown";
//    stations[1] = "Detroit";
//    stations[2] = "Helsinki";
//    stations[3] = "Los Angeles";
//    stations[4] = "London";
//    stations[5] = "New York";
//    stations[6] = "Miami";
//    stations[7] = "Savannah";
//    stations[8] = "Santa Barbara";
//    populateStationRecords(stations);
//    
//    // Do it twice to make sure the record clearing functionality works.
//    populateStationRecords(stations);

}

function clearOldStationRecords()
{
    for(var i = 0; i < stationRecords.length; ++i)
    {
        stationList.children.remove(stationRecords[i]);
    }

    stationRecords = new Array();
}

function fixupStationDisplays()
{
    // Each element is 28 pixels high.
    // Adjust the elements so that they line up nicely.
    for(var i = 0; i < stationRecords.length; ++i)
    {
        var element = stationRecords[i];
        element["Canvas.Top"] = i * 28;
        stationList.children.add(element);        
    }

}

function populateStationRecords(stationData)
{
    clearOldStationRecords();
    for(var i = 0; i < stationData.stationList.length; i++)
    {
        addStationRecord(stationData.stationList[i]);
    }
    fixupStationDisplays();
}

function addStationRecord(station)
{
    var stationRecord = stationHostControl.content.createFromXaml(    
      '<Canvas MouseLeftButtonDown="OnStationSelected" Width="158" Height="28">'
    + '  <Rectangle Width="158" Height="28" Fill="#FFFFFFFF" Stroke="#FF94B6DC" RadiusX="0" RadiusY="0"/>'
    + '  <TextBlock Width="140" Height="20" TextWrapping="Wrap"  FontSize="12" Foreground="#FF0F2187" Text="" Canvas.Left="8" Canvas.Top="8"/>'
    + '  <TextBlock Width="1" Height="1" TextWrapping="Wrap"  FontSize="12" Foreground="#FF0F2187" Text="" Canvas.Left="8" Canvas.Top="8" Visibility="Collapsed" />'
    + '</Canvas>'
    );
    
    stationRecord.children.getItem(1).text = station.name;
    stationRecord.children.getItem(2).text = stringify(station);
    
    stationRecords[stationRecords.length] = stationRecord;
}

var newStation = null;
function OnStationSelected(sender, args)
{
    //alert(sender.children.getItem(1).text);
    writeLine("station selected: " + sender.children.getItem(2).text);
    newStation = parseJson(sender.children.getItem(2).text);
    var loc = newLocation.city + ", ";
    
    if (newLocation.isUs) 
        loc += newLocation.state ;
    else
        loc +=  newLocation.country;
        
    plugin.content.findName("CityText").Text = loc;
    plugin.content.findName("StationText").Text = newStation.name;
    HideStationPopup(sender, args);
    HideCityPopup(sender, args);
}

function OnStationScrollDown(sender, args)
{
    var maxHeight = 28 * stationRecords.length;
    var canvasHeight = stationList.findName("StationListMain").height;
    var xform = stationList.renderTransform.children.getItem(3);
    var y = xform.y;
    
    if(y <= canvasHeight - maxHeight)
    {
        xform.y = canvasHeight - maxHeight;
    }
    else
    {
        xform.y = y - stationScrollOffset;
    }
}

function OnStationScrollUp(sender, args)
{
    var xform = stationList.renderTransform.children.getItem(3);
    var y = xform.y;
    
    if(y >= 0)
    {
        xform.y = 0;
    }
    else
    {
        xform.y = y + stationScrollOffset;
    }
}

function ShowStationPopup(sender, args, locCode)
{
    writeLine("ShowStationPopup locCode: " + locCode);
    if (null != locCode)
        getStationData(locCode);
    stationPopup.opacity = 1;
    stationPopup.IsHitTestVisible = true;
}


function HideStationPopup(sender, args)
{
    stationPopup.opacity = 0;
    stationPopup.IsHitTestVisible = false;    
}

var stationUrl = WebRoot + "/DataService/GetStation.ashx?";

var ZipToken = "z:";
var CityCodeToken = "c:";

var newLocation = null;
function getStationData(locCode)
{
    var url = stationUrl;
    var loc = parseJson(locCode);
    newLocation = loc;
    if (loc.isUs)
    {   
        url = url + "zip=" + escape(loc.zipCode);
    }
    else 
    {
        url = url + "city=" + escape(loc.cityCode);       
    }
    var dataReq = new httpReq(url, parseStation);
    writeLine("loading url: " + url);
    dataReq.load();
}

function parseStation()
{
    var stationData = this.getJsonObj();

    if (null != stationData)
    {
        try
        {
            populateStationRecords(stationData);        
        }
        catch(error)
        {
            writeLine(error.name + ": " + error.description);
        }
    }
}