﻿// Multi-resource Download Manager
// Last build: 31-3-2008
// Terry Hibbert

function MultiDownloader(slRootObject, slControl ,aResources, allComplete, onChange) {

    var that = this;
    
    
    this.init = function() {
        that.resources = [];
        that.nGoalsTotal = aResources.length;
        that.nGoalsReached = 0;
        
        // Start the downloads
        that.startDownload();
    }
    
    // Define downloader instance object
    var DlObject = function(sGoalTitle) {
        
        // Handler of progress change
        this.onChange = function(sender, eventArgs) {
            // Call progress change master
            onChangeMaster();            
        }
        
        // Handler of download completion
        this.onComplete = function(sender, eventArgs) {
            console.log("Goal " + that.nGoalsReached + ": " + sGoalTitle + " complete");
            that.resources[sGoalTitle] = sender;
            that.nGoalsReached++;
            
            if (that.nGoalsReached == that.nGoalsTotal) {
                console.log("All goals complete, executing <i>allComplete()</i>");
                fEvent = Silverlight.createDelegate(slRootObject, allComplete);
                fEvent();
            }
        }
    }
    
    
    // Compile sum of all progress
    onChangeMaster = function() {
        
        if (typeof(skip) == "undefined") {
            var skip = that.nGoalsTotal*-2;
        }
        
        if (skip > 0) {
            var nDone = 0;
            for (var i=0; i<that.nGoalsTotal; i++) {
                nDone = aDownloaders[i].downloadProgress;
            }
            nDone = nDone / (that.nGoalsTotal -1);
            
            onChange(slRootObject, 0, 0, nDone);
            skip = that.nGoalsTotal*-2;
        } else {
            skip++;
        }
    }
    
    
    // Begin the downloads
    this.startDownload = function() {
        
        var aDownloaders = [];
        var aOnCompleteds = [];
        
        var skip = 1;
        
        // Loop through all resources creating Downloader instances
        for (var i=0; i<that.nGoalsTotal; i++) {
            
            // Add an instance of the onComplete object
            aOnCompleteds[i] = new DlObject(aResources[i].title);
            
            console.log("Completed: " +  aResources[i].location);
            
            // Create the downloader instance
            aDownloaders[i] = slControl.createObject("downloader");
            
            // Add DownloadProgressChanged and Completed events.
            aDownloaders[i].addEventListener("downloadProgressChanged", aOnCompleteds[i].onChange);
            
            // Add onCompleted object's action function as the completed event handler
            aDownloaders[i].addEventListener("completed", aOnCompleteds[i].onComplete);

            // Initialize the Downloader request.
            // NOTE: downloader APIs disallow file:\\ scheme.
            // You must run this sample over localhost: or off a server, or the following call will fail.

            // open the downloader location
            aDownloaders[i].open("GET", aResources[i].location);

            // Execute the Downloader request.
            aDownloaders[i].send();
        }
    }
    
    this.init();
    return this;
}