// Manage stock updates
reminderfox.network.DownloadManager = function(callback, url, downloadedReminders, downloadedTodos) {

    this.reminderFox_subscriptionCallback = callback;
        this.reminderFox_subscriptionReminders = downloadedReminders;
		this.url = url;
    
//    this.download = function(headlessLevel, callback, url, downloadedReminders, downloadedTodos){
this.download = function() {
        reminderFox_download_statustxt(reminderFox_getBundle().getString("rf.upload.ready.label"), 0);
//        this.reminderFox_subscriptionCallback = callback;
//        this.reminderFox_subscriptionReminders = downloadedReminders;
//		this.url = url;
        
        var _ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
        dump( "\n\n@@@@@@@@@@@@@@@@@@@@@@@@@@@\nDOWNLOADING2a.... " + this.url  + "\n");
		dump( "CALLBACK: " + this.reminderFox_subscriptionCallback + "\n")
		dump( "sub_reminders = " +  this.reminderFox_subscriptionReminders+ "\n")
        var downloadURI = _ioService.newURI(this.url, null, null);
        if (gDownloadService.start(downloadURI, this.downloadCallback, this)) 
            reminderFox_download_statustxt(reminderFox_getBundle().getString("rf.upload.request.label"), 0);
        else {
            //statustxt(reminderFox_getBundle().getString("rf.net.done"),0);
            this.downloadCallback(reminderFox_getBundle().getString("rf.net.done"), -1);
        }
        
    }
	
//	 this.download2 = function(headlessLevel, callback, url, downloadedReminders, downloadedTodos){
//        reminderFox_download_statustxt(reminderFox_getBundle().getString("rf.upload.ready.label"), 0);
//        this.reminderFox_subscriptionCallback = callback;
//        this.reminderFox_subscriptionReminders = downloadedReminders;
//		this.url = url;
//        
//        var _ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
//        dump( "\n\n@@@@@@@@@@@@@@@@@@@@@@@@@@@\nDOWNLOADING2b.... " + url  + "\n");
//		dump( "CALLBACK: " + this.reminderFox_subscriptionCallback + "\n")
//		dump( "sub_reminders = " +  this.reminderFox_subscriptionReminders+ "\n")
//        var downloadURI = _ioService.newURI(url, null, null);
//        if (gDownloadService.start(downloadURI, this.downloadCallback, this)) 
//            reminderFox_download_statustxt(reminderFox_getBundle().getString("rf.upload.request.label"), 0);
//        else {
//            //statustxt(reminderFox_getBundle().getString("rf.net.done"),0);
//            this.downloadCallback(reminderFox_getBundle().getString("rf.net.done"), -1);
//        }
//        
//    }
    
    
    this.downloadCallback = function(aStatus, aError, mgr){
		dump( "MGR: " + mgr );
		dump( "DL Callback2: " + aStatus + ";" + aError + "\n" );
		dump( "THIS.callbacK: " +mgr.reminderFox_subscriptionCallback + "\n" )
        if (aStatus != reminderFox_getBundle().getString("rf.net.done") && aError == 0) {
            reminderFox_download_statustxt(aStatus, aError);
            return;
        }
        
        switch (aError) {
            case -2:
                reminderFox_download_statutxt(aStatus, aError);
                if (mgr.reminderFox_subscriptionCallback != null) {
                    var statustxt = (aError) ? reminderFox_download_getErrorMsg(aError) : aStatus;
                    mgr.reminderFox_subscriptionCallback(statustxt, 0);
                }
                return;                break;
            case 0:
                
                reminderFox_logMessageLevel("Downloading subscribed reminders...", REMINDER_FOX_LOG_LEVEL_FINE);
                
                if (mgr.reminderFox_subscriptionCallback != null) {
                    mgr.reminderFox_subscriptionCallback(reminderFox_getBundle().getString("rf.add.network.status.download.label"), 0);
                }
                var processtime = 0;
                if (reminderFox_download_headless == REMINDER_FOX_UI_MODE_HEADLESS_SHOW_ALL_UI) {
                    processtime = document.getElementById("processtime");
                }
                var time = gDownloadService.time;
                processtime.value = gDownloadService.length.toString() +
                " bytes  " +
                (time / 1000).toString() +
                " sec  ";
                var start = new Date();
                
                reminderFox_download_statustxt(reminderFox_getBundle().getString("rf.upload.process.label"), 0);
                try {
                    reminderFox_download_statustxt(reminderFox_getBundle().getString("rf.download.importing.label"), 0);
                    
                    var is = Components.classes["@mozilla.org/io/string-input-stream;1"].createInstance(Components.interfaces.nsIStringInputStream);
                    is.setData(gDownloadService.data, -1); // -1 ... length should be computed
                    var downloadedTodos = new Array();
					
					
					dump( "READ IN NOW11: " + mgr.reminderFox_subscriptionReminders + "\n")
                    reminderFox_readInRemindersAndTodosICSFromStream(mgr.reminderFox_subscriptionReminders, downloadedTodos, is, true);
                    
					
					dump( "\n\n\n***********************************\nREAD IN: " + mgr.url + ":" +  mgr.reminderFox_subscriptionReminders.length );
					
                    // if successful, close immediately
                    if (window.arguments != null && window.arguments[0] != null && window.arguments[0].closeOnNoErrors == 1) {
						dump( "INSIDE HERE\n" );
                        close();
                        return;
                    }
                    
                    var end = new Date();
                    processtime.value += ((end.getTime() - start.getTime()) / 1000).toString() + " sec";
                    reminderFox_download_statustxt(reminderFox_getBundle().getString("rf.net.done"), 0);
                    dump( "CALLBACK: " + mgr.reminderFox_subscriptionCallback + "\n" );
                    if (mgr.reminderFox_subscriptionCallback != null) {
						dump( "CALLBACKd\n")
                        mgr.reminderFox_subscriptionCallback(null, 2, mgr.reminderFox_subscriptionReminders);
                    }
                    
                } 
                catch (e) {
                    reminderFox_download_statustxt(e.toString(), 0);
                    if (e && e.toString().match(/^rem\:(.+)$/)) 
                        reminderFox_download_statustxt(RegExp.$1, 0);
                    return;
                }
                
                
                break;
            default:
                var status = aError % 0x804b0000;
                
                reminderFox_download_statustxt(aStatus, aError);
                
                if (mgr.reminderFox_subscriptionCallback != null) {
                    var statustxt = (aError) ? reminderFox_download_getErrorMsg(aError) : aStatus;
                    mgr.reminderFox_subscriptionCallback(statustxt, 0);
                }
                break;
        }
        
        if (aError == 0) {
            if (reminderFox_download_headless == REMINDER_FOX_UI_MODE_HEADLESS_SHOW_ALL_UI) {
                reminderFox_closeWindow();
            }
        }
    }
    
    
    
    
    
    
    
    
}
