﻿
var recentLocationList;
var recentLocationHostControl;
var recentLocationPopup;

var recentLocationScrollOffset = 14;

var recentLocationRecords = new Array();

var MaxRecentLocations = 10;
var recLocations = new Array();


function addRecentLocation(loc, stat)
{
    if (!alreadyRecentLocation(loc, stat))
    {
        for (var i = (recLocations.length - 1); i >= 0; i--)
        {
            if ((i + 1) < MaxRecentLocations)
                recLocations[i + 1] = recLocations[i];
        }
        
        var locStat = new Array(2);
        locStat[0] = loc;
        locStat[1] = stat;
        recLocations[0] = locStat;
        
        populateRecentLocationRecords(recLocations);
    }
}

function alreadyRecentLocation(loc, stat)
{
    var already = false;
    for (var i = 0; i < recLocations.length; i++)
    {
        recLoc = recLocations[i][0];
        recStat = recLocations[i][1];
        if (loc.isUs == recLoc.isUs)
        {
            if (loc.isUs)
            {
                if (loc.zipCode == recLoc.zipCode)
                    already = true;  
            }
            else
            {
                if (loc.cityCode== recLoc.cityCode)
                    already = true;
            }
            
            already = already && (stat.id == recStat.id);
            if (already) break;
        }
    }
    return already;
}



function OnRecentLocationPopupLoaded(sender)
{
    recentLocationList = sender.findName("RecentLocationList");
    recentLocationHostControl = sender.getHost();
    recentLocationPopup = sender.findName("RecentLocationPopup");
    
    // Test code
//    var recentLocations = new Array();
//    recentLocations[0] = "Germantown";
//    recentLocations[1] = "Detroit";
//    recentLocations[2] = "Helsinki";
//    recentLocations[3] = "Los Angeles";
//    recentLocations[4] = "London";
//    recentLocations[5] = "New York";
//    recentLocations[6] = "Miami";
//    recentLocations[7] = "Savannah";
//    recentLocations[8] = "Santa Barbara";
//    populateRecentLocationRecords(recentLocations);
//    
//    // Do it twice to make sure the record clearing functionality works.
//    populateRecentLocationRecords(recentLocations);

}

function clearOldRecentLocationRecords()
{
    while(recentLocationRecords.length > 0)
    {
        recentLocationList.children.remove(recentLocationRecords[0]);
        recentLocationRecords.pop();
    }
}

function fixupRecentLocationDisplays()
{
    // Each element is 28 pixels high.
    // Adjust the elements so that they line up nicely.
    for(var i = 0; i < recentLocationRecords.length; ++i)
    {
        var element = recentLocationRecords[i];
        element["Canvas.Top"] = i * 28;
        recentLocationList.children.add(element);        
    }

}

function populateRecentLocationRecords(recentLocations)
{
    clearOldRecentLocationRecords();
    for(var i = 0; i < recentLocations.length; ++i)
    {
        addRecentLocationRecord(recentLocations[i]);
    }
    fixupRecentLocationDisplays();
}

function addRecentLocationRecord(recentLocation)
{
    var recentLocationRecord = recentLocationHostControl.content.createFromXaml(    
      '<Canvas MouseLeftButtonDown="OnRecentLocationSelected" 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="0" Height="0" TextWrapping="Wrap"  FontSize="12" Foreground="#FF0F2187" Text="" Canvas.Left="8" Canvas.Top="8" Visibility="Collapsed" />'
    + '</Canvas>'
    );
    
    
    var loc = recentLocation[0].city + ", ";
    loc += (recentLocation[0].isUs) ? recentLocation[0].state : recentLocation[0].country;
    loc += " (" +recentLocation[1].id + ")";
    var locstat = stringify(recentLocation);
    
    recentLocationRecord.children.getItem(1).text = loc;
    recentLocationRecord.children.getItem(2).text = locstat;

    recentLocationRecords[recentLocationRecords.length] = recentLocationRecord;
}

function OnRecentLocationSelected(sender, args)
{
    //alert(sender.children.getItem(1).text);
    //TODO: Justin, do something here.
    addRecentLocation(locat, station);
    var locStat = null;
    var locCode = sender.children.getItem(2).Text;
    try
    {
        locStat = parseJson(locCode);
        writeLine("locCode: " + locCode);
        locat = locStat[0];
        station = locStat[1];
    }
    catch(error)
    {
        writeLine(error.name + ": " + error.description);
    }
    init(sender);
    HideRecentLocationPopup(sender, args);
}

function OnRecentLocationScrollDown(sender, args)
{
    var maxHeight = 28 * recentLocationRecords.length;
    var canvasHeight = recentLocationList.findName("RecentLocationListMain").height;
    var xform = recentLocationList.renderTransform.children.getItem(3);
    var y = xform.y;
    
    if(y <= canvasHeight - maxHeight)
    {
        xform.y = canvasHeight - maxHeight;
    }
    else
    {
        xform.y = y - recentLocationScrollOffset;
    }
}

function OnRecentLocationScrollUp(sender, args)
{
    var xform = recentLocationList.renderTransform.children.getItem(3);
    var y = xform.y;
    
    if(y >= 0)
    {
        xform.y = 0;
    }
    else
    {
        xform.y = y + recentLocationScrollOffset;
    }
}

function ShowRecentLocationPopup(sender, args)
{
    recentLocationPopup.opacity = 1;
    recentLocationPopup.IsHitTestVisible = true;
}


function HideRecentLocationPopup(sender, args)
{
    recentLocationPopup.opacity = 0;
    recentLocationPopup.IsHitTestVisible = false;    
}