﻿var receivingFTB;
var thePopup;

function FTBExtSend(popupID, callerFTB, mediaManPath) {
    var apee = FTB_API[callerFTB];
    var imgr = apee.GetNearest('img');
    if (imgr) {
        apee.InsertImage();
        return;
    }
    popupID.SetContentUrl(mediaManPath);
    popupID.Show();
    receivingFTB = callerFTB;
    thePopup = popupID;
}

function FTBExtReceive(html) {
    var ftb = FTB_API[receivingFTB];
    ftb.Focus();
    ftb.InsertHtml(html);
    thePopup.Hide();
}



function FTBExtSend_CreateLink(apiName) {

    var apee = FTB_API[apiName];

    var link = apee.GetNearest('a');
    var url = '';

    if (link) {
        var url = link.getAttribute('temp_href');
        if (!url) url = link.getAttribute('href');
    } else {
        var sel = apee.GetSelection();
        var text = '';
        if (FTB_Browser.isIE) {
            text = sel.createRange().text;
        } else {
            text = new String(sel);
        }
        if (text == '' || text == null) {

            var daImg = apee.GetNearest('img');
            if (daImg) {
                var daClon = daImg.cloneNode(true);
                var daLnk = apee.designEditor.document.createElement("a");
                daLnk.setAttribute('temp_href', 'http://');
                daLnk.setAttribute('href', 'http://');
                daLnk.appendChild(daClon);
                daImg.parentNode.replaceChild(daLnk, daImg);
                apee.SelectNodeContents(daLnk);
                FTBExtSend_CreateLink(apiName)
                return;
            } else {
                alert('Please select some text');
                return;
            }
        }
    }


    var linkWin = window.open("", "linkWin", "width=350,height=155");
    if (linkWin) {
        linkWin.focus();
    } else {
        alert("Please turn off your PopUp blocking software");
        return;
    }

    linkWin.document.body.innerHTML = '';
    linkWin.document.open();
    linkWin.document.write(FTB_LinkPopUpHtml);
    linkWin.document.close();

    launchParameters = new Object();
    launchParameters['ftb'] = apee;
    linkWin.launchParameters = launchParameters;
    linkWin.load();

};


function FTBHookThingsUp(ftbName, cookieName) {
    var o = document.getElementById(ftbName + "_designModeTab");
    var p = document.getElementById(ftbName + "_htmlModeTab");

    if (o != null)
        FTB_AddEvent(o, 'click', function() {
            setTimeout(function() {
                /// WE PUT THIS IN A TIMEOUT BECAUSE IF WE RUN IT RIGHT AWAY
                /// A JAVASCRIPT ERROR IS THROWN IN FIREFOX.
                /// THIS DELAY SEEMS TO HELP.

                FTBSetCookie("Design", cookieName);

                var ftb = FTB_API[ftbName];
                if (!ftb.readOnly) {
                    try {
                        ftb.GoToDesignMode();
                        ftb.Focus();
                        ftb.UpdateToolbars();
                    } catch (err) {
                    }
                }

            }, 10);

        });

    if (p != null) FTB_AddEvent(p, 'click', function() { FTBSetCookie("Html", cookieName); });
}


function FTBSetCookie(val, cookieName) {
    Set_Cookie(cookieName, val, 180, "/", "", false);
}


function Set_Cookie(name, value, daysToExpire, path, domain, secure) {
    var today = new Date();
    today.setTime(today.getTime());

    if (daysToExpire) {
        daysToExpire = daysToExpire * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (daysToExpire));

    document.cookie = name + "=" + escape(value) +
        ((daysToExpire) ? ";expires=" + expires_date.toGMTString() : "") +
        ((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "") +
        ((secure) ? ";secure" : "");
}





///////////////////////////////
///////////////////////////////
///////////////////////////////


function FTBExtSaving(ftbName) {

    /// need to replace the $ with lowerscores
    var editor = FTB_API[ftbName.replace(/\$/g, '_')];

    if (editor.VizAids && editor.mode != FTB_MODE_HTML) {
        FTBExtToggleVizAids(ftbName);
        editor.CopyDesignToHtml();
    }

}


///////////////////////////////
///////////////////////////////
///////////////////////////////



/// Setting up the highlighting logic.
///
var _vizAidsElems = "td, table, div, object, li, ul, p";
function FTBExtToggleVizAids(ftbName) {
    var ok = false;
    try {
        if (jq132BTM != undefined) ok = true;
    } catch (e) { }
    if (!ok) {
        alert('Feature not Supported. Sorry :(');
        return;
    }

    /// need to replace the $ with lowerscores
    var editor = FTB_API[ftbName.replace(/\$/g, '_')];

    if (!editor.VizAids) {
        editor.VizAids = true;
        FTBExtVizAidsScoutOn(editor);
        jq132BTM(editor).bind('HtmlModeStarted', function(e, s) { FTBExtVizAidsScoutOff(s); });
        jq132BTM(editor).bind('DesignModeEnded', function(e, s) { FTBExtVizAidsScoutOn(s); });
    } else {
        editor.VizAids = false;
        FTBExtVizAidsScoutOff(editor);
        jq132BTM(editor).unbind('HtmlModeStarted');
        jq132BTM(editor).unbind('DesignModeEnded');
    }
}



/// Looks for elements to turn ON highlight.
///
function FTBExtVizAidsScoutOn(editor) {
    FTBExtVizAidsDoLog("Scouting on...");
    jq132BTM(editor.designEditor.document).find(_vizAidsElems).each(function() {
        FTBExtVizAidsDoLog("found..");
        jq132BTM(this).addClass('BTM_VizAids_');
    });
}


/// Looks for elements to turn OFF highlight.
///
function FTBExtVizAidsScoutOff(editor) {
    FTBExtVizAidsDoLog("Scouting off...");
    jq132BTM(editor.designEditor.document).find(_vizAidsElems).each(function() {
        jq132BTM(this).removeClass('BTM_VizAids_');
    });
}



function FTBExtVizAidsDoLog(msg) {
//    DoLog(msg);
}
////////////////////////
////////////////////////
////////////////////////


