// This script allows to use several Epoz WYSIWYG fields.
// Copyright (C) 2005 Benoit PIN <mailto:pin@cri.ensmp.fr>

var IFramePrefix = "Iframe_";
var IFramePrefixLength = IFramePrefix.length;
var DocPrefix = "doc_";
var DocPrefixLength = DocPrefix.length;
var CheckBoxPrefix = "CB_";
var CheckBoxPrefixLength = CheckBoxPrefix.length;
var ToolBarPrefix = "ToolBar_";
var ToolBarPrefixLength = ToolBarPrefix.length;


function redirectEpoz(iframe) {
    if(EpozElement) {
        if (EpozElement == iframe) {
            return;
        }
        changeBorderStyle(EpozElement, "dashed");
        unwrapEpozVariables();
    }

    // update Epoz variables
    wrapEpozVariables(iframe);

    //cosmetic
    changeBorderStyle(EpozElement, "solid");
    EpozElement.contentWindow.focus();
}


function wrapEpozVariables(iframe) {
    fieldId = iframe.contentWindow.document.id.slice(DocPrefixLength);

    iframe.id = Epoz ;
    EpozElement=iframe;
    EpozTextArea = document.getElementById(fieldId);
    toolBar = document.getElementById(ToolBarPrefix + fieldId);
    toolBar.id = "EpozToolbar";
    checkBox = document.getElementById(CheckBoxPrefix + fieldId);
    checkBox.id = "EpozViewMode";
}


function unwrapEpozVariables() {
    if (!EpozElement) {
        // no redirection happens yet.
        return;
    }

    fieldId = EpozElement.contentWindow.document.id.slice(DocPrefixLength);

    EpozElement.id = IFramePrefix + fieldId;
    toolBar = document.getElementById("EpozToolbar");
    toolBar.id = ToolBarPrefix + fieldId;
    checkBox = document.getElementById("EpozViewMode");
    checkBox.id = CheckBoxPrefix + fieldId;
}



// Initialization functions

if (!formDatas) {
    var formDatas = new Array();
}

function InitIframe(name, data, path, toolbox, style, button, css, customcss, charset, pageurl) {
    form_name = name;
    form_data = data;
    form_path = path;
    form_toolbox = toolbox;
    form_area_style = style;
    form_button_style = button;
    form_css = css;
    form_customcss = customcss;
    form_charset = charset;
    form_pageurl = pageurl;

    var richText;

    if (browser.isIE55 || browser.isIE6up) {
        // Mac-IE doesn't support RichText-Edit at the moment
        if (browser.isMac) {
            CreateTextarea();
            richText = false;
        } else {
            CreateEpoz();
            richText = true;
        }
    }
    else if (browser.isGecko) {
        //check to see if midas is enabled
        try {
            // Just a few cleanups for Mozilla

            form_data = form_data.replace(/<strong>/ig,'<b>');
            form_data = form_data.replace(/<strong(\s[^>]*)>/ig,'<b$1>');
            form_data = form_data.replace(/<\/strong>/ig,'</b>');

            form_data = form_data.replace(/<em>/ig,'<i>');
            form_data = form_data.replace(/<em(\s[^>]*)>/ig,'<i$1>');
            form_data = form_data.replace(/<\/em>/ig,'</i>');

            CreateEpoz();
            richText = true;
        } catch (e) {
          CreateTextarea();
          richText = false;
        }
    }
    else {
        CreateTextarea();
        richText = false;
    }

    if (richText) {
        // change iframe id
        formDatas[name] = form_data;
        var iframe = document.getElementById(Epoz);
        iframe.id = IFramePrefix + name;

        // change EpozToolbar id
        var toolBar = document.getElementById("EpozToolbar");
        toolBar.id = ToolBarPrefix + name;

        // Retrieve the switch HTML mode checkbox and change id and onclick function.
        nodes = iframe.parentNode.nextSibling.nextSibling.childNodes;
        var i;
        for (i=0 ; i<nodes.length ; i++ ) {
            if (nodes[i].nodeType == 1 && nodes[i].nodeName == 'INPUT')
                break;
        }
        var checkbox = nodes[i];
        checkbox.id = CheckBoxPrefix + name;

        changeBorderStyle(iframe, "dashed");
    }
}



function HandleEpozRedirect() {
    if (browser.isIE55 || browser.isIE6up) {
        iframe = event.srcElement;
    }
    else if (browser.isGecko) {
        iframe = this;
    }
    redirectEpoz(iframe);
}

function redirectAndSwitchViewMode(checkbox) {
    if (checkbox.id != "EpozViewMode") {    // Otherwise, EpozElement already targets the good field.
        var iframe = document.getElementById(IFramePrefix + checkbox.id.slice(CheckBoxPrefixLength));
        redirectEpoz(iframe);
    }
    SwitchViewMode(checkbox.checked);
}


function changeBorderStyle(ob, borderStyle) {
    s = ob.style;
    s.borderBottomStyle = borderStyle;
    s.borderLeftStyle = borderStyle;
    s.borderRightStyle = borderStyle;
    s.borderTopStyle = borderStyle;
}


function InitDocument(iframe) {
    fieldId = iframe.id.slice(IFramePrefixLength);
    var data = formDatas[fieldId];
    delete formDatas[fieldId];
    ta = document.getElementById(fieldId);
    iframe.contentWindow.document.body.innerHTML = data;
    ta.value = data;
    iframe.contentWindow.document.id = DocPrefix + fieldId;
    addListener(iframe, "click", HandleEpozRedirect);
    form = retrieveForm(ta);
    form.onsubmit=SyncEpoz;
    if (browser.isGecko) {
        scriptExpr = 'EnableDesignMode("' + iframe.id + '");';
        window.setTimeout(scriptExpr, 10);
    }
}


function SyncEpoz() {
    if (document.getElementsByTagName) {
        var iframes = document.getElementsByTagName("IFRAME");
    }
    else if (document.all) {
        var iframes = document.all.tags("IFRAME");
    }

    for (var i=0;i<iframes.length;i++) {
    
        // iframe fix
		// @author Olegk
		var iframe_id = iframes[i].id;

		if( iframe_id.indexOf(Epoz) != '-1' ) {

            unwrapEpozVariables();
            wrapEpozVariables(iframes[i]);

            var html = TidyHTML(GetHTML());

            // strip trailing whitespace
            html = (html.replace(/^\s*/,'')).replace(/\s*$/,'');

            // remove single br left by Firefox / Mozilla
            if (html=="<br />" || html=="<br>" || html=="<p></p>") {
                html = "";
            }
            form_name = EpozElement.contentWindow.document.id.slice(DocPrefixLength);
            document.getElementById(form_name).value = html;
        
        }
    }

}

function EnableDesignMode(iframeId) {
    iframe = document.getElementById(iframeId);
    try {
        iframe.contentDocument.designMode = "on";
        iframe.contentWindow.document.addEventListener("keypress", HandleKeyboardEvent, true);
    } catch (e) {
        scriptExpr = 'EnableDesignMode("' + iframeId + '");';
        setTimeout(scriptExpr, 10);
    }
}



//utils

function retrieveForm(baseOb){
    var pn = baseOb.parentNode;

    while (pn != document) {
        if (pn.nodeName == 'FORM') {
            break;
        } else {
            pn = pn.parentNode;
        }
    }
    return pn;
}


function addListener(ob, eventName, functionReference){
    if (browser.isIE55 || browser.isIE6up) {
        eventName = "on" + eventName;
        ob.attachEvent(eventName, functionReference);
    }
    else if (browser.isGecko) {
        ob.addEventListener(eventName, functionReference, false);
    }

}