// FileLinker.js

function createFileLink(ftbClientID, fileUrl, mpeClientID) {
    // Validate parameters
    var e = Function._validateParams(arguments, [
        { name: "ftbClientID", type: String, mayBeNull: false },
        { name: "fileUrl", type: String, mayBeNull: false },
        { name: "mpeClientID", type: String, mayBeNull: false}]);
    if (e)
        throw e;
    else {
        if (ftbClientID.length == 0) throw Error.argument("ftbClientID", "Parameter cannot contain an empty string");
        if (fileUrl.length == 0) throw Error.argument("fileUrl", "Parameter cannot contain an empty string");
        if (mpeClientID.length == 0) throw Error.argument("mpeClientID", "Parameter cannot contain an empty string");
    }

    // Validate that text is still selected to create a link on
    var text = getFTBSelectedText(ftbClientID);
    if (text == '') {
        alert('Text selection was lost - please try again.');
        return;
    }

    var ftb = FTB_API[ftbClientID];

    ftb.ExecuteCommand('createlink', null, fileUrl);

    $find(mpeClientID).hide();
}

function launchModalPopup(ftbClientID, btnClientID) {
    // Validate parameters
    var e = Function._validateParams(arguments, [
        { name: "ftbClientID", type: String, mayBeNull: false },
        { name: "btnClientID", type: String, mayBeNull: false}]);
    if (e)
        throw e;
    else {
        if (ftbClientID.length == 0) throw Error.argument("ftbClientID", "Parameter cannot contain an empty string");
        if (btnClientID.length == 0) throw Error.argument("btnClientID", "Parameter cannot contain an empty string");
    }

    var ftb = FTB_API[ftbClientID];

    var text = getFTBSelectedText(ftbClientID);
    if (text == '') {
        alert('Please select some text on which to create a link.');
        return;
    }

    $get(btnClientID).click();
}

function getFTBSelectedText(ftbClientID) {
    // Validate parameters
    var e = Function._validateParams(arguments, [
        { name: "ftbClientID", type: String, mayBeNull: false}]);
    if (e)
        throw e;
    else {
        if (ftbClientID.length == 0) throw Error.argument("ftbClientID", "Parameter cannot contain an empty string");
    }

    var ftb = FTB_API[ftbClientID];
    var sel = ftb.GetSelection();
    var text = '';

    if (FTB_Browser.isIE) {
        text = sel.createRange().text;
    } else {
        text = new String(sel);
    }

    return text;
}