
var setup = function () {
    this.asperaWeb = new AW.Connect({id:'aspera_web_transfers'});
 
    this.asperaWeb.initSession("AsperaDownload");
    this.asperaWeb.addEventListener('transfer', fileControls.handleTransferEvents);
};


// fileControls.downloadFile(this)
 
fileControls = {};
 
fileControls.handleTransferEvents = function (event, obj) {
    switch (event) {
        case 'transfer':
            //document.getElementById('progress_meter').innerHTML = JSON.stringify(obj, null, 4);
            if( obj.error_desc != "" ){
	            //alert(obj.transfer_spec.direction + " - " + obj.title + ": " + obj.error_desc);
	        }
            break; 
    }
};


fileControls.downloadFile = function (el, destPath) {
	transferSpec = el.transfer_specs[0].transfer_spec;

	if(destPath != ""){
	alert(transferSpec.destination_root);
		transferSpec.destination_root = destPath; // NG...
	alert(transferSpec.destination_root);
	}
	
	transferSpec.authentication = "token";

    /* sample
    transferSpecs = {"transfer_spec" : [{"paths" : [{"source" : "test.xml"}],
    				"source_root" : ""
    				,"destination_root" : ""
    				,"token" : "ATM2_ACsNg9YH7uiEzCdUsTC0HYQjyWnrJoYDiO698FkGguemgwAAFQW1ltjNhVJiOTsSWtCAPY_2MTA"
    				,"direction" : "receive"
    				,"cipher" : "aes-128"
    				,"rate_policy_allowed" : "fixed"
    				,"rate_policy" : "fair"
    				,"target_rate_kbps" : 300000
    				,"min_rate_kbps" : 0
    				,"remote_host" : "drive2.muratec.co.jp"
    				,"remote_user" : "root"
    				,"ssh_port" : 33001
    				,"fasp_port" : 33001
    				,"http_fallback" : true
    				,"http_fallback_port" : 8443
					}]};
	*/
     
    connectSettings = {
        "allow_dialogs": "no"
        ,"use_absolute_destination_path": "true"

    };

     response = asperaWeb.startTransfer(transferSpec, connectSettings);
     /*
     asperaWeb.startTransfer( transferSpec, connectSettings,
                          callbacks = {
                            error : function(obj) {
                              alert("Failed to start : " + JSON.stringify(obj, null, 4));
                            },
                            success:function () {
                              var container;
                              var toggleG;
                              var download = true;
                              if(transferSpec.direction === "send") {
                                download = false;
                              }
                              //alert("Started transfer : " + JSON.stringify(transferSpec, null, 4));
                            }
                           });
       */

};

fileControls.selectFolder = function (el) {
	if( asperaWeb == null ) {setup();}
    asperaWeb.showSelectFolderDialog(
       callbacks = {
              error : function(obj) {
                alert("Destination folder selection cancelled. Download cancelled.");
              },
              success:function (pathArray) {
                var destPath = null;
                if (!(pathArray == null || typeof pathArray === "undefined" || pathArray.length === 0)) {
                  destPath = pathArray[0];
                  // alert("Destination folder for download: " + destPath);
                    fileControls.downloadFile(el, destPath);
                }
             }
       },
      //disable the multiple selection.
      options = {
        allowMultipleSelection : false,
        title : "Select Destination Folder"
      });
};


