// name : [ cmd, tooltip, imagefn ] for buttons
// junk : [ null                  ] for separators
Editor.buttons =
{
    "bold"          : [ "bold",          "Félkövér",       "bold"        ],
    "italic"        : [ "italic",        "Dőlt",           "italic"      ],
    "underline"     : [ "underline",     "Aláhúzott",      "underline"   ],
    "sep1"          : [ null                                             ],
    "justifyleft"   : [ "justifyleft",   "Balra igazítás", "left"        ],
    "justifycenter" : [ "justifycenter", "Középre zárás",  "center"      ],
    "justifyright"  : [ "justifyright",  "Jobbra zárás",   "right"       ],
    "justifyfull"   : [ "justifyfull",   "Sorkizárás",     "full"        ],
    "sep2"          : [ null                                             ],
    "superscript"   : [ "superscript",   "Felső index",    "superscript" ],
    "subscript"     : [ "subscript",     "Alsó index",     "subscript"   ],
    "sep3"          : [ null                                             ],
    "undo"          : [ "undo",          "Visszavonás",    "undo"        ],
    "redo"          : [ "redo",          "Mégis",          "redo"        ]
};

Editor.onload = function()
{
    document.editors = new Array();

    var spans = document.getElementsByTagName("span");
    for (var i = 0; i < spans.length; i++)
        if (spans[i].className == "editable")
        {
            var id = spans[i].id;
            document.editors[id] = new Editor(id);
            i--;
        }
}

Editor.get = function(id)
{
    return document.editors[id];
}

function Editor(id)
{
    this.editor = this;	// %%
    this.name = id;
    this.contentChanged = false;

    var span = document.getElementById(this.name);
    if (!span)
        return this;

    if (0 < span.innerHTML.length)
        this.oldContent = span.innerHTML;
    else
        this.oldContent = "";

    var tools = document.createElement("div");
    tools.className = "toolbar";

    for (var i in Editor.buttons)
    {
        var cmd = Editor.buttons[i][0];
        if (cmd)
        {
            var btn = document.createElement("button");
            btn.actionCmd = cmd;
            btn.title = Editor.buttons[i][1];
            btn.editor = this;
            btn.setAttribute("onclick", "return this.editor.buttonClicked(this);");

            var img = document.createElement("img");
            img.setAttribute("src", "/bodza/page/ieditor/icons/" + Editor.buttons[i][2] + ".png");

            btn.appendChild(img);
            tools.appendChild(btn);
        }
        else
        {
            var sep = document.createElement("span");
            sep.className = "separator";
            tools.appendChild(sep);
        }
    }

    this.iframe = document.createElement("iframe");
    this.iframe.editor = this;
    Event.listen(this.iframe, "load", this.iframeLoaded);

    var text = document.createElement("input");
    text.setAttribute("name", name);
    text.setAttribute("type", "hidden");
    text.setAttribute("value", "");

    var panel = document.createElement("div");
    panel.className = "ieditor";
    panel.appendChild(tools);
    panel.appendChild(this.iframe);
    panel.appendChild(text);

    span.parentNode.replaceChild(panel, span);

    return this;
}

Editor.prototype.buttonClicked = function(button)
{
    try
    {
        this.iframe.contentDocument.execCommand(button.actionCmd, false, null);
        button.blur();
        this.iframe.contentWindow.focus();
    }
    catch (_)
    {
    }

    this.documentChanged();

    return false;
}

Editor.prototype.iframeLoaded = function(event)
{
    // this is iframe

//    Event.forget(this, 'load', Editor.prototype.iframeLoaded);

    var editor = this.editor;
    if (!editor)
        return;

    var doc;
    if (this.contentDocument)
    {
        doc = this.contentDocument;
        //%%doc.designMode = "off";
    }
    else
    {
        doc = this.document;
        doc.body.contentEditable = false;
        doc = this.document;	// IE needs to reget the doc after designMode was set
    }

    var first = (!editor.innerBody);

    doc.body.style.margin = "0.3em";	// %%
    editor.innerBody = doc.body;
    doc.documentElement.editor = editor;

    Event.listen(doc.documentElement, "keyup", editor.documentChanged);
    Event.listen(doc.documentElement, "paste", editor.documentChanged);

    var f = function()
    {
        window.setTimeout("Editor.submit('" + editor.name + "');", 0);	// %%
    };

    if (this.contentDocument)
        this.contentDocument.addEventListener("blur", f, false);
    else
        this.attachEvent("onblur", f);

    window.setInterval(f, 15000);

    if (0 < editor.oldContent.length)
    {
        while (editor.innerBody.childNodes.length)
            editor.innerBody.removeChild(editor.innerBody.childNodes[0]);

        editor.innerBody.innerHTML = editor.oldContent;
        editor.documentChanged();

        if (!first)
            editor.oldContent = "";
    }

    editor.contentChanged = false;

    try
    {
        if (first)
            doc.designMode = "on";

        //%%doc.body.contentEditable = false;
        doc.execCommand("styleWithCSS", false, false);
    }
    catch (_)
    {
    }

    event.stopPropagation();
}

Editor.prototype.documentChanged = function(_event)
{
    // this is editor|documentElement

    var editor = this.editor;

    var f = editor.iframe;
    var d = editor.innerBody;

    if (f.clientHeight < d.offsetHeight)
        f.style.height = (d.offsetHeight+20) + "px";

    if (160 < f.clientHeight && d.offsetHeight < f.clientHeight)
        f.style.height = (d.offsetHeight+20) + "px";

    editor.contentChanged = true;

    return false;
}

Editor.submit = function(id)
{
    var editor = Editor.get(id);
    if (editor && editor.contentChanged)
    {
        var url = null;

        for (var par = editor.iframe.parentNode; par; par = par.parentNode)
            if (par.tagName.toLowerCase() == "form")
            {
                url = par.action;
                break;
            }

        if (url)
        {
            var data = id + '=' + encodeURIComponent(editor.innerBody.innerHTML);

            Async.post(url, data);
        }

        editor.contentChanged = false;
    }
}

