/* jslint support */
/*global eop: false, npsClientEditor: false */

if (!npsClientEditor) {
    alert("npsClientEditor not defined. You have to include nps_client_editor_support.js.");
}

function eopQuote(x) {
    return x.replace(/\"/g,"&quot;");
}

function eopUnquote(x) {
    x = "" + x;
    return x.replace(/\&quot\;/gi,"\"");
}


npsClientEditor.useRelativeUrls = true;

npsClientEditor.setLink = function(href, title, target) {
    href = eopQuote(href);
    title = eopQuote(title);
    target = eopQuote(target);

    if (npsClientEditor.editState === "EDIT_LINK") {
        var attrs = eop.getParentElementByNameAttributes("a");
        attrs.href = href;
        attrs.title = title;
        if (npsClientEditor.isWhitespace(target)) {
            // TBD deleting attributes is not supported
            if (attrs.target) {
                attrs.target = "_self";
            }
        } else {
            attrs.target = target;
        }
        eop.setParentElementByNameAttributes("a", attrs);
    } else if (npsClientEditor.editState === "NEW_LINK") {
        var currentSel = eop.getSelectedHTMLData();
        if (currentSel === "") {
            currentSel = href;
        }
        var result = "<a";
        result += " href=\"" + eopQuote(href) + "\"";
        result += " title=\"" + eopQuote(title) + "\"";
        if (!npsClientEditor.isWhitespace(target)) {
            result += " target=\"" + eopQuote(target) + "\"";
        }
        result += ">" + currentSel + "</a>";
        eop.insertHTMLData(result);
    }
    eop.pumpEvents();
};

npsClientEditor.setImage = function(src, alt) {
    src = eopQuote(src);
    alt = eopQuote(alt);

    if (npsClientEditor.editState === "NEW_IMAGE") {
        var result = "<img";
        result += " src=\"" + eopQuote(src) + "\"";
        result += " alt=\"" + eopQuote(alt) + "\"";
        result += "/>";
        eop.insertHTMLData(result);
    } else if (npsClientEditor.editState === "EDIT_IMAGE") {
        var attrs = eop.getElementAttributes();
        attrs.src = src;
        attrs.alt = alt;
        eop.setElementAttributes(attrs);
    }
    eop.pumpEvents();
};


function editLink() {
    var attrs = eop.getParentElementByNameAttributes("a");
    var href = attrs.href || "";
    var title = attrs.title || "";
    var target = attrs.target || "";
    href = eopUnquote(href);
    title = eopUnquote(title);
    target = eopUnquote(target);

    npsClientEditor.editLink(eop, href, title, target);
}

function newLink() {
    // if current selection is a link, edit it
    // tbd: after double click on link, the surrounding elem is selected!
    var current = eop.getCurrentElement();
    if (current.indexOf("<a ") === 0) {
        editLink();
    } else {
        npsClientEditor.createLink(eop);
    }
}

function editImage() {
    var attrs = eop.getElementAttributes();
    var src = attrs.src || "";
    var alt = attrs.alt || "";
    src = eopUnquote(src);
    alt = eopUnquote(alt);

    npsClientEditor.editImage(eop, src, alt);
}

function newImage() {
    var current = eop.getCurrentElement();
    if (current.indexOf("<img ") === 0) {
        editImage();
    } else {
        npsClientEditor.createImage(eop);
    }
}


function npsEopEditorSave(button)
{
    eop.postDocumentToServer();
    eop.pumpEvents();
    return true;
}
