(function() {
    "use strict";

    var lastKeys = "";
    function fetchRedlines() {
        var baseUrl = $('#baseUrl').val();
        var artworkUrl = $('#artworkUrl').val();
        $.get(baseUrl + 'index').done(function(keys) {
            if (lastKeys === keys) {
                return;
            }
            lastKeys = keys;

            var items = [];
            var requests = $.map(keys.split('\n'), function(key) {
                return $.get(baseUrl + key).done(function(data) {
                    var json = JSON.parse(data);
                    var feedback = $('<div/>').text(json.feedback).addClass('feedback');
                    var artwork = $('<div/>').append($('<img/>').attr('src', artworkUrl))
                                             .append($('<img/>').attr('src', json.image).addClass('redline'))
                                             .addClass('drawing');
                    var item = $('<div/>').append(feedback)
                                          .append(artwork)
                                          .attr('id', key);
                    items.push(item);
                }).fail(function() {
                    console.log('Failed to fetch data for key ' + key);
                });
            });

            $.when.apply($, requests).done(function () {
                $('#items').empty();
                $.each(items, function(i, item) {
                    $('#items').append(item);
                });
                $('#submissions').text(items.length);
            });

        }).fail(function() {
            console.log('Failed to fetch index file');
        });
    }

    $(function() {
        fetchRedlines();
        setInterval(fetchRedlines, 5000);
    });
})();

@@ -352,18 +352,81 @@
 var 
-items = %5B%5D
+parts = keys.split('%5Cn');%0A            var items = new Array(parts.length)
 ;%0A  
@@ -456,32 +456,21 @@
 = $.map(
-keys.split('%5Cn')
+parts
 , functi
@@ -475,16 +475,19 @@
 tion(key
+, i
 ) %7B%0A    
@@ -1161,19 +1161,18 @@
 tems
-.push(
+%5Bi%5D = 
 item
-)
 ;%0A  
@@ -1455,24 +1455,60 @@
 (i, item) %7B%0A
+                    if (item) %7B%0A    
             
@@ -1541,16 +1541,38 @@
 (item);%0A
+                    %7D%0A
         

Add ordering of submissions to view page