var slCtl = null;

//DO NOT FORGET TO REGISTER THIS FUNCTION WITH THE SILVERIGHT CONTROL
// OnPluginLoaded="pluginLoaded"
function pluginLoaded(sender) {
    slCtl = $('#UploaderControlSilverlight')[0];

//    cms3.Log(slCtl);
//    cms3.Log(slCtl.Content);
//    cms3.Log(slCtl.Content.Files);
    //if (!slCtl.Content)
    //    return;
    //Register All Files Finished Uploading event
    slCtl.Content.Files.AllFilesFinished = AllFilesFinished;

    //Register single file finished event
    slCtl.Content.Files.SingleFileUploadFinished = SingleFileFinished;

    //Register Error occurred during uploading event
    slCtl.Content.Files.ErrorOccurred = ShowErrorDiv;
    slCtl.Content.Files.FileAdded = function() {
        isUploading = true;
    }
    
}


function ShowNumberOfFilesUploaded() {
    if (slCtl != null) {
        alert("Total Files Uploaded: " + slCtl.Content.Files.TotalUploadedFiles);
    }
}

function ShowTotalNumberOfFilesSelected() {
    if (slCtl != null) {
        alert("Total Files Selected: " + slCtl.Content.Files.TotalFilesSelected);
    }
}

function ShowUploadProgress() {
    if (slCtl != null) {
        alert("Progress: " + slCtl.Content.Files.Percentage);
    }
}



//This function is registred in the pluginLoaded function (slCtl.Content.Files.AllFilesFinished = AllFilesFinished;)
function AllFilesFinished() {
    // document.getElementById('AllFinishedDiv').style.display = 'block';
    isUploading = false;
}

//This function is registred in the pluginLoaded function (slCtl.Content.Files.SingleFileUploadFinished = SingleFileFinished;)
function SingleFileFinished(userfile) {

    //$.jGrowl('File uploaded');
    tag.loadAssets();
}

//This function is registred in the pluginLoaded function (slCtl.Content.Files.ErrorOccurred = ShowErrorDiv;)
function ShowErrorDiv() {
    //  document.getElementById('ErrorDiv').style.display = 'block';
    cms3.Message("An error occured");
}

var isUploading = false;
//Actions
function StartUpload() {
    if (slCtl != null) {
        isUploading = true;
        slCtl.Content.Control.StartUpload();
    }
}

function ClearList() {
    if (slCtl != null) {
        slCtl.Content.Control.ClearList();
    }
}

function SelectFiles() {
    if (slCtl != null) {
        slCtl.Content.Control.SelectFiles();
    }
}

function OnLoadError(sender, args) {

    alert(args);
    Cms3.Log(args);
}

function UploadComplete() {
    $.jGrowl("File(s) Uploaded");
    isUploading = false;
}


window.onbeforeunload = function() {
    if (isUploading)
        return "If you leave this page now, your uploads will not complete. Are you sure you want to leave this page?";
};
