// コメントにカスタマイズ絵文字入力機能：画像設定
function getCommentEmojiList() {
    var list = new Array();
    // 画像名、画像URLの設定
    list[list.length] = new Array('00', 'http://navi222.com/sb/img/template/pp_01.gif');
    list[list.length] = new Array('01', 'http://navi222.com/sb/img/template/pp_02.gif');
    list[list.length] = new Array('02', 'http://navi222.com/sb/img/template/pp_03.gif');
    list[list.length] = new Array('03', 'http://navi222.com/sb/img/template/pp_04.gif');
    list[list.length] = new Array('04', 'http://navi222.com/sb/img/template/pp_05.gif');
    list[list.length] = new Array('05', 'http://navi222.com/sb/img/template/pp_06.gif');
    list[list.length] = new Array('06', 'http://navi222.com/sb/img/template/pp_07.gif');
    list[list.length] = new Array('07', 'http://navi222.com/sb/img/template/pp_08.gif');
    list[list.length] = new Array('08', 'http://navi222.com/sb/img/template/pp_09.gif');
    list[list.length] = new Array('09', 'http://navi222.com/sb/img/template/pp_10.gif');
    list[list.length] = new Array('10', 'http://navi222.com/sb/img/template/pp_11.gif');
    list[list.length] = new Array('11', 'http://navi222.com/sb/img/template/pp_12.gif');
    list[list.length] = new Array('12', 'http://navi222.com/sb/img/template/pp_13.gif');
    list[list.length] = new Array('13', 'http://navi222.com/sb/img/template/pp_14.gif');
    list[list.length] = new Array('14', 'http://navi222.com/sb/img/template/pp_15.gif');
    list[list.length] = new Array('15', 'http://navi222.com/sb/img/template/pp_16.gif');
    list[list.length] = new Array('16', 'http://navi222.com/sb/img/template/pp_17.gif');
    list[list.length] = new Array('17', 'http://navi222.com/sb/img/template/pp_18.gif');
    list[list.length] = new Array('18', 'http://navi222.com/sb/img/template/pp_19.gif');
    list[list.length] = new Array('19', 'http://navi222.com/sb/img/template/pp_20.gif');
    list[list.length] = new Array('20', 'http://navi222.com/sb/img/template/pp_21.gif');
    list[list.length] = new Array('21', 'http://navi222.com/sb/img/template/pp_22.gif');
    list[list.length] = new Array('22', 'http://navi222.com/sb/img/template/pp_23.gif');
    list[list.length] = new Array('23', 'http://navi222.com/sb/img/template/pp_24.gif');
    list[list.length] = new Array('24', 'http://navi222.com/sb/img/template/pp_25.gif');
    list[list.length] = new Array('25', 'http://navi222.com/sb/img/template/pp_26.gif');
    list[list.length] = new Array('26', 'http://navi222.com/sb/img/template/pp_27.gif');
    list[list.length] = new Array('27', 'http://navi222.com/sb/img/template/pp_28.gif');
    list[list.length] = new Array('28', 'http://navi222.com/sb/img/template/pp_29.gif');
    list[list.length] = new Array('29', 'http://navi222.com/sb/img/template/pp_30.gif');
    list[list.length] = new Array('30', 'http://navi222.com/sb/img/template/pp_31.gif');
    list[list.length] = new Array('31', 'http://navi222.com/sb/img/template/pp_32.gif');
    return list;
}

function getCustmizeEmojiAttribute() {
    // 画像タグに含める属性値（alt属性以外）
    var att = 'border="0" style="vertical-align:middle"';

    // ***************** 設定ここまで *****************
    att = (att != '') ? (' ' + att) : '';
    return att;
}

function writeCustmizeEmojiTagList(textarea) {

    // ***************** 設定ここまで *****************
    var list = getCommentEmojiList();
    var att = getCustmizeEmojiAttribute();
    for (var i = 0; i < list.length; i++) {
        window.document.write('<a href="javascript:;" onClick="addCustmizeEmoji(' + textarea + ', \'' + list[i][0] + '\');"><img src="' + list[i][1] + '"' + att + ' alt="' + list[i][0] + '" /></a>');
    }
}

// reference from jugem.jp (c)
function addCustmizeEmoji(obj, tag) {
    var item = '[' + tag + ']';
    if (document.selection) {
        obj.focus();
        sel = document.selection.createRange();
        sel.text = item;
    } else if (obj.selectionStart || obj.selectionStart == '0') {
        var startPos = obj.selectionStart;
        var endPos = obj.selectionEnd;
        obj.value = obj.value.substring(0, startPos) + item + obj.value.substring(endPos, obj.value.length);
    } else {
        obj.value += item;
    }
}
// コメントにカスタマイズ絵文字入力機能：画像変換
function changeCustmizeEmojiTag(commentAreaIdName, commentAreaTag, commentBodyClassName) {

    // ***************** 設定ここまで *****************
    var list = getCommentEmojiList();
    var att = getCustmizeEmojiAttribute();
    var objComArea;
    var classComArea;
    var objComBody = new Array();
    if (commentAreaIdName != '') {
        objComArea = document.getElementById(commentAreaIdName);
        if (!objComArea || !objComArea.innerHTML) return;
        classComArea = objComArea.getElementsByTagName(commentAreaTag);
    } else {
        classComArea = document.getElementsByTagName('*');
    }
    for (var i = 0; i < classComArea.length; i++) {
        if (classComArea[i].className == commentBodyClassName) objComBody[objComBody.length] = classComArea[i];
    }
    for (var i = 0; i < objComBody.length; i++) {
        objComBody[i].innerHTML = getStringCustmizeEmojiTag(list, objComBody[i].innerHTML, att);
    }
}

function getStringCustmizeEmojiTag(list, s, att) {
    for (var i = 0; i < list.length; i++) {
        s = s.split('[' + list[i][0] + ']').join('<img src="' + list[i][1] + '"' + att + ' alt="' + list[i][0] + '" />');
    }
    return s;
}

