"use strict";
define(['jquery'], function($){
    /*
        scanData has 'current' field for sure,
        and may also have 'prev', next fields.
    */
    var scanData = $.parseJSON($('#scanData').text());
    /*
        We add scanData.fields, which will contain an array of ordered
        Fields included in scanData:
    */
    scanData.fields = [];
    ['prev','current','next'].forEach(function(f){
        if(f in scanData){
            scanData.fields.push(f);
        }
    });
    /*
        We always preload images:
    */
    var proms = [];
    scanData.fields.forEach(function(key){
        var scan = scanData[key]
          , def  = $.Deferred()
          , img  = new Image();
        //Handling promise for this scan:
        img.onload = function(){ def.resolve(); };
        proms.push(def.promise());
        //We exchange scan.img from String to Image:
        img.src = scan.img;
        scan.img = img;
    });
    /* Adding scanData.whenLoaded: */
    var def = $.Deferred();
    $.when.apply($, proms).done(function(){
        def.resolve();
    });
    scanData.whenLoaded = function(func){
        def.done(func);
    };
    /*
        scanData now has a structure like this:
        {
            ['prev','current','next']: {
                    urn: String,
                    img: Image,
                    aois: ?
                },
            fields: [String], ordered subset of {'prev','current','next'},
            whenLoaded: function(callback) //Adds a callback to be called when images are loaded.
        }
    */
    return scanData;
});

@@ -7,16 +7,166 @@
 trict%22;%0A
+/**%0A    This module is concerned with presenting a single point to access data about scans and their AOIs%0A    as it is presented by singleFile.php%0A*/%0A
 define(%5B

Add initial comment to scanData.js