﻿function isDefined(object) {
    return (typeof object == "undefined") ? false : true;
}

function onLoaded(sender, args) {
    // Retrieve a reference to the plug-in.
    slPlugin = sender.getHost();

    // Set the event handler function to the OnResize event.
    slPlugin.content.onResize = onResized;

    // Do initial layout of the application based on initial size.
    updateLayout(slPlugin.content, slPlugin.content.actualWidth, slPlugin.content.actualHeight);
}

function onResized(sender, eventArgs) {
    var root = sender.getHost().content;
    var gridMain = root.findName("gridMain");
    if (gridMain != null) {
        gridMain.Height = root.actualHeight;
        gridMain.Width = root.actualWidth;
    }
}

function onSourceDownloadProgressChanged(sender, eventArgs) {
    var root = sender.getHost().content;
    var progress = Math.round((eventArgs.progress * 100));
    updateDownloadStatus(root, progress);
}

function onSourceDownloadComplete(sender, eventArgs) {
    var root = sender.getHost().content;
    updateDownloadStatus(root, 100);
}

function updateDownloadStatus(root, progress) {
    if (root != null) {
        var txtProgress = root.findName("txtStatus");
        if (txtProgress != null) txtProgress.Text = progress.toString() + "%";
        var uxProgressBar = root.findName("uxProgressBar");
        if (uxProgressBar != null) {
            progress = Math.min(Math.max(0, progress), 100);
            uxProgressBar.ScaleX = progress / 100;
        }
    }
}

function updateLayout(root, width, height) {
    var gridMain = root.findName("gridMain");
    if (gridMain != null) {
        gridMain.Height = root.actualHeight;
        gridMain.Width = root.actualWidth;
    }
}

