var personaArguments = {
    siteName: 'Where Is My Fox?',
};

function renderDeviceTable(devices) {
    if (devices.length) {
        devices[0].first = true;
    }

    var table = Mustache.render($('#device-list-template').html(), {
        devices: devices,
        mapsURL: "https://maps.google.com/maps?q="
    });

    $("#devices").html(table);
    $("#devices button").click(function() {
        var select = $(this).parent().prev().children("select"),
        options = select.children("option"),
        idx = select.get(0).selectedIndex,
        trigger = $(options[idx]).attr("data-trigger");

        $.post(trigger);
    });
}

function updateDevices() {
    $("#devices").html("Fetching list...");

    function failedToFetchDevices() {
        $("#devices").html("Failed to fetch your devices!");
    }

    $.getJSON('/device').then(function(list) {
        var deviceRequests = list.map(function(url) {
            return $.getJSON(url);
        });

        $.when.apply(null, deviceRequests).then(function() {
            var devices = [];

            if (deviceRequests.length == 1) {
                devices.push(arguments[0]);
            } else {
                for (var i = 0; i < arguments.length; i++) {
                    devices.push(arguments[i][0]);
                }
            }

            var commandRequests = devices.map(function(device) {
                return $.getJSON('/device/' + device.Id + '/command');
            });

            $.when.apply(null, commandRequests).then(function() {
                for (var i = 0; i < devices.length; i++) {
                    devices[i].commands = arguments[i];
                    devices[i].id = i;
                }

                renderDeviceTable(devices);
            });

        }, failedToFetchDevices);
    }, failedToFetchDevices);
}

$("document").ready(function(){

    $("#persona-logout").hide();
    $("#devices").hide();

    function loggedIn(email){
        $("#persona-login").hide();
        $("#persona-logout").show();

        $("#devices").show();
        updateDevices();
    }

    function loggedOut(){
        $("#persona-logout").hide();
        $("#persona-login").show();
        $("#devices").hide();
    }

    $("#persona-login").on("click", function(e) {
        e.preventDefault();
        navigator.id.get(mailVerified, personaArguments);
    });

    $("#persona-logout").on("click", function(e) {
        e.preventDefault();
        $.get('/auth/logout', loggedOut);
    });

    function mailVerified(assertion){
        $.ajax({
            type: 'POST',
            url: '/auth/login',
            data: {assertion: assertion},
            success: function(res, status, xhr) {
                loggedIn(JSON.parse(res).email);
            },
            error: function(xhr, status, err) {
                alert("Login failure: " + err);
            }
        });
    }

    $.get('/auth/check', function (res) {
        if (res === "") {
            loggedOut();
        } else {
            loggedIn(res);
        }
    });
});


@@ -640,342 +640,603 @@
 %0A%7D%0A%0A
-function updateDevices() %7B%0A    $(%22#devices%22).html(%22Fetching list...%22);%0A%0A    function failedToFetchDevices() %7B%0A        $(%22#devices%22).html(%22Failed to fetch your devices!%22);%0A    %7D%0A%0A    $.getJSON('/device').then(function(list) %7B%0A        var deviceRequests = list.map(function(url) %7B%0A            return $.getJSON(url);%0A        %7D);%0A%0A       
+/*%0A * Takes an array of jQuery XHR requests, and returns a Promise that resolves%0A * when all of then succeed and rejects when any of them fails. The Promise%0A * resolves to an array, each element being the result of each request in the%0A * order they were passed.%0A *%0A * NOTE: jQuery.when() when called with just one promise (e.g. in the case%0A * where the user has only one device registered), passes the result of that%0A * promise to the associated callback, while for multiple devices, it passes%0A * a list.  This is stupid behaviour for our case.%0A *%0A */%0Afunction URLs_every(deferreds) %7B%0A    return
  $.w
@@ -1253,27 +1253,22 @@
 null, de
-viceRequest
+ferred
 s).then(
@@ -1292,60 +1292,20 @@
     
-    var devices = %5B%5D;%0A%0A            if (deviceRequest
+if (deferred
 s.le
@@ -1329,33 +1329,24 @@
         
-    devices.push(
+return %5B
 argument
@@ -1349,23 +1349,22 @@
 ments%5B0%5D
-);%0A    
+%5B0%5D%5D;%0A
         
@@ -1388,140 +1388,515 @@
     
-    for (var i = 0; i %3C arguments.length; i++) %7B%0A            
+return Array.prototype.slice.call(arguments).map(function(jqXHRResult) %7B return jqXHRResult%5B0%5D; %7D);%0A        %7D%0A    %7D);%0A%7D%0A%0Afunction updateDevices() %7B%0A    $(%22#devices%22).html(%22Fetching list...%22);%0A%0A    function failedToFetchDevices() %7B%0A        $(%22#devices%22).html(%22Failed to fetch your devices!%22);%0A
     
+%7D%0A%0A
     
-devices.push(arguments%5Bi%5D%5B0%5D);%0A                %7D%0A            %7D%0A
+$.getJSON('/device').then(function(list) %7B%0A        var deviceRequests = list.map(function(url) %7B%0A            return $.getJSON(url);%0A        %7D);%0A%0A        URLs_every(deviceRequests).then(function(devices) %7B
 %0A   
@@ -2057,35 +2057,27 @@
         
-$.when.apply(null, 
+URLs_every(
 commandR
@@ -2091,32 +2091,40 @@
 ).then(function(
+commands
 ) %7B%0A            
@@ -2212,24 +2212,23 @@
 mands = 
-argument
+command
 s%5Bi%5D;%0A  
@@ -2278,24 +2278,61 @@
           %7D%0A
+                console.log(devices);
 %0A           

Fix action menu issue. Fixes #18 properly.