/*
 *  (C) utilz.jp
 *   http://www.utilz.jp/
 */
var __emf_rex = new RegExp("<img src=\"" + emf_g_imgBaseUrl + "/([^/]+)/([^\\.]+)\\.gif\">", "gi");
var __emf_emojis = new Array("emf_i_emoji", "emf_e1_emoji", "emf_e2_emoji", "emf_s1_emoji", "emf_s2_emoji");
var __emf_cur_visible = null;

function emf_init(editWindow){
    __emf_getIFrameDocument(editWindow).designMode = "On";
}

function emf_setFocus(editWindow){
    document.getElementById(editWindow).contentWindow.focus();
}

function __emf_getIFrameDocument(editWindow){ 
    if (document.getElementById(editWindow).contentDocument){ 
        return document.getElementById(editWindow).contentDocument; 
    }
    else{ 
        return document.frames[editWindow].document; 
    } 
} 

function __emf_execCommand(editWindow, command, arg){
    emf_setFocus(editWindow);
    __emf_getIFrameDocument(editWindow).execCommand(command, false, arg);
}

function __emf_c(carrier){
    if (carrier == "emf_i_emoji"){
        return "i";
    }
    else if (carrier == "emf_e1_emoji" || carrier == "emf_e2_emoji"){
        return "e";
    }
    else{
        return "s";
    }
}

function __emf_toImgUrl(carrier, ncr){
    var found = ncr.match(/&#x(.*);/);
    if (found != null){
        ncr = found[1];
    }
    return emf_g_imgBaseUrl + "/" + __emf_c(carrier) + "/" + ncr + ".gif";
}

function emf_getval(editWindow){
    return __emf_replace(__emf_getIFrameDocument(editWindow).body.innerHTML);
}

function __emf_replace(html){
    html = html.replace(/<p>(.*)<\/p>/gi, "$1");
    html = html.replace(__emf_rex, "&#x$2;");
    html = html.replace(/<br>/gi, "\n");
    html = html.replace(/&nbsp;/gi, " ");
    html = html.replace(/&lt;/gi, "<");
    html = html.replace(/&gt;/gi, ">");
    html = html.replace(/&quot;/gi, "\"");
    return html;
}

function emf_writeDivs(editWindow){

    var func = document.getElementById(editWindow).tagName.toLowerCase() == "iframe"?
                "__emf_clicked" : "__emf_clicked2";

    var i;
    for (i=0; i < __emf_emojis.length; i++){
        carrier = __emf_emojis[i];
        imgUrl = emf_g_imgBaseUrl + "/" + carrier + ".gif";
        document.writeln(
            "<div id=\"" + carrier + "\" style=\"position: absolute; left: 0; top: 0; visibility: hidden;\">" +
            "<div style=\"padding: 4px; background-color: #ffffff; border: 1px solid black;\">" +
            "<div style=\"background-color: #3366cc; text-align: right;\">" +
            "<a style=\"color: #ffffff; font-size: 8pt;\" href=\"javascript:void(0)\" onclick=\"emf_show(null)\">close</a></div>" +
            "<img src=\"" + imgUrl + "\" usemap=\"#" + carrier + "_map\" border=\"0\">" +
            __emf_buildmap(carrier, editWindow, func) +
            "</div></div>\n");
    }
}

function emf_show(carrier, anch){
    if (__emf_cur_visible != null){
        document.getElementById(__emf_cur_visible).style.visibility = "hidden";
        __emf_cur_visible = null;
    }
    if (carrier != null){
        targ = document.getElementById(carrier);
        var top = 0;
        var left = 0;
        if (anch != null){
            do {
                top += anch.offsetTop;
                left += anch.offsetLeft;
                anch = anch.offsetParent;
            } while (anch != null);
        }

        var width = 0;
        if (window.innerWidth){
            width = window.innerWidth - 20;
        }
        else if(document.documentElement && document.documentElement.clientWidth){
            width = document.documentElement.clientWidth;
        }
        else if (document.body){
            width = document.body.clientWidth;
        }

        if ((left + targ.clientWidth) > width){
            left = width - targ.clientWidth;
            if (left < 0){
                left = 0;
            }
        }

        targ.style.left = left;
        targ.style.top = top;
/*
            targ.style.left = anch.offsetLeft;
            targ.style.top = anch.offsetTop + anch.offsetHeight;
*/
        targ.style.visibility = "visible";
        __emf_cur_visible = carrier;
    }
}

function __emf_clicked2(ncr, carrier, editWindow){
    emf_show(null);
    var e = document.getElementById(editWindow);
    e.focus();
    e.value += ncr;
}

function __emf_ie_onload(url, editWindow){
    __emf_getIFrameDocument(editWindow).disabled = false;
    __emf_execCommand(editWindow, "insertImage", url);
    __emf_execCommand(editWindow, "cut", null);
    __emf_execCommand(editWindow, "paste", null);
/*
    var doc = document.getElementById(editWindow).contentWindow.document;
    if (doc.selection){
        var range = doc.selection.createRange();
        if (range != null){
            range.pasteHTML("<img src=\"" + url + "\">");
        }
    }
*/
}

function __emf_ie_onfail(editWindow){
    __emf_getIFrameDocument(editWindow).disabled = false;
    alert('error.');
}

function __emf_clicked(ncr, carrier, editWindow){

    var imgUrl = __emf_toImgUrl(carrier, ncr);

    emf_show(null);
    emf_setFocus(editWindow);

    var doc = document.getElementById(editWindow).contentWindow.document;
    if (doc.selection){
/*
        var range = doc.selection.createRange();
        if (range != null){
            range.pasteHTML("<img src=\"" + imgUrl + "\">");
        }
*/
        /* IE is buggy!! */
        var img = new Image();
        img.src = imgUrl;
        if (img.complete){
            __emf_ie_onload(imgUrl, editWindow);
        }
        else{
            __emf_getIFrameDocument(editWindow).disabled = true;
            img.onload = function(){
                __emf_ie_onload(imgUrl, editWindow);
            };
            img.onerror = function(){
                __emf_ie_onfail(editWindow);
            };
            img.onabort = function(){
                __emf_ie_onfail(editWindow);
            };
        }

/*
        __emf_execCommand(editWindow, "insertImage", imgUrl);
        __emf_execCommand(editWindow, "cut", null);
        __emf_execCommand(editWindow, "paste", null);
*/
    }
    else{
        __emf_execCommand(editWindow, "insertImage", imgUrl);
    }
    emf_setFocus(editWindow);
}

function __emf_buildmap(career, editWindow, func){

    var s = __emf_buildmap_s(
                carrier,
                __emf_emoji_defs[carrier + "_unary_wid"],
                __emf_emoji_defs[carrier + "_unary_hei"],
                __emf_emoji_defs[carrier + "_margin"],
                __emf_emoji_defs[carrier + "_cols"],
                __emf_emoji_defs[carrier + "_codes"],
                __emf_emoji_defs[carrier + "_code_offset"],
                editWindow, func);
    return s;
}

function __emf_buildmap_s(
            name,
            unary_wid,
            unary_hei,
            margin,
            cols,
            codes,
            code_offset,
            editWindow,
            func){

    var r = "<map name=\"" + name + "_map\">";
    
    var row = 0;
    var col = 0;
    var i;

    for (i=0; i < codes.length; i++){
        if (codes[i] == 0){
            continue;
        }
        if (col != 0 && (col%cols) == 0){
            row++;
            col = 0;
        }
        var val = codes[i] + code_offset;
        var ncr = "&amp;#x" + val.toString(16).toUpperCase() + ";";
        var x = (col*(unary_wid+(margin*2))) + margin;
        var y = (row*(unary_hei+(margin*2))) + margin;
        r +=
            "<area shape=\"rect\" coords=\"" +
                x + "," +
                y + "," +
                (x+unary_wid) + "," +
                (y+unary_hei) + "\" " +
            "href=\"javascript:void(0)\" " +
            "onclick=\"javascript:" + func + "('" +
                ncr + "', '" + name + "','" + editWindow + "')\" " +
                "title=\"" + ncr + "\" " +
                ">";
        col++;
    }
    r += "</map>";
    return r;
}
