// JavaScript Document
var _groups = [];
var _moreActionSelectCount = 2;
var _checkedItemCount = 0;
var _batchActionEnabled = true;
var _floatOn = "bDisplayTemp";
var _floatMenu;
_GLOBAL_VAR["bACCID"] = "bookmarkShareToACC";

function editB(args) {
    var pw;
    var winTitle = "Edit Bookmark";
    var root = "leftColumn";
    var index = args.index;
    var b = $("bookmarkItem_"+index);
    var top = b.positionedOffset()[1]-180;
    pw = new PopWindow({optEnabled: true, left:120, top:top, width:500, title:winTitle, root:root});
    pw.appendNode(createBEditTemp(index));
    pw.cancel = function(){resetMoreAction();resetAllChecker();}
    pw.submitBtn.value = "Submit";
    pw.cancelBtn.value = "Cancel";
    pw.submit = submitEditB.bind(this, args);
    initEditor(index);
}

function switchBToEditMode(index) {
    hideDisplayTemp(index);
    showEditTemp(index);
    initEditor(index);
}
function initEditor(index) {
    var title;
    var tags;
    var desc;

    var index = index;
    title = _GLOBAL_VAR['bookmarks'][index].title;
    tags = _GLOBAL_VAR['bookmarks'][index].tags.split(",");
    desc = _GLOBAL_VAR['bookmarks'][index].desc;

    $("bTxtTitle").value = title;
    var t_t = [];
    for(var i=0; i<tags.length; i++) {
        tags[i].indexOf(" ")!=-1 ? t_t.push("\""+tags[i]+"\"") : t_t.push(tags[i])
    }
    var t_name = t_t.join(", ");
    $("bTxtTags").value = t_name.trim();
    $("bTxtDesc").value = desc.trim();
}

function switchBToDisplayMode(index){
    showDisplayTemp(index);
    hideEditTemp(index);
    hideItemNoticer(index);
    initDisplayTemp(index);
    if(_checkedItemCount==0) {
        hideBatchEditBBB();
        showBListOB();
        resetMoreAction();
    }
}

function initDisplayTemp(index) {
    $("titleLink_"+index).innerHTML = _bookmarks[index].title;
    var tagsEl = $("tags_"+index);
    tagsEl.innerHTML = "";
    var tags = _bookmarks[index].t_name.split(",");
    for(var i=0; i<tags.length; i++) {
        var a = document.createElement("a");
        a.href= "/" + _bookmarks[index].g_name + "/bookmark/tag/"+escape(tags[i]);
        a.innerHTML = tags[i];
        tagsEl.appendChild(a);
    }
    var desc = _bookmarks[index].description;
    if(!desc.blank()) {
        $("descContent_"+index).update(desc);
        $("desc_"+index).show();
    }else {
        $("desc_"+index).hide();
    }
}
function cancelEditB(){
    var index = this.id.split("_")[1];
    showDisplayTemp(index);
    hideEditTemp(index);
    hideItemNoticer(index);
    var checker = $("bookmarkChecker_"+index);
    if(checker.checked) {
        checker.click();
    }
    if(_checkedItemCount==0) {
        hideBatchEditBBB();
        showBListOB();
        resetMoreAction();
    }
}
function showDisplayTemp(index) {
    $("bDisplayTemp_"+index).style.display = "";
    _floatMenu.switchMenu2(true, index);
}
function hideDisplayTemp(index) {
    $("bDisplayTemp_"+index).style.display = "none";
}
function showEditTemp(index) {
    var winTitle = "Edit Bookmark";
    var root = "leftColumn";
    pw = new PopWindow({optEnabled: true, left:120, top:top, width:500, title:winTitle, root:root});
    pw.appendNode(createBEditTemp(index));
    pw.cancel = function(){resetMoreAction();resetAllChecker();}
    pw.submitBtn.value = "Submit";
    pw.cancelBtn.value = "Cancel";
}
function hideEditTemp(index) {
    $("bEditTemp_"+index).style.display = "none";
}
function loadDisplayTemp(index) {
    return _bookmarks[index].bDisplayTemp;
}

function loadBEditTemp(index) {
    if(typeof(_bookmarks[index].bEditTemp)=="undefined") {
        _bookmarks[index].bEditTemp =  createBEditTemp(index);
    }
    return _bookmarks[index].bEditTemp;
}

function createBEditTemp(index) {
    var index = index;

    var table = new Element("table", {className: "editBTable"});
    var tbody = new Element("tbody");
    // title row
    var tr = new Element("tr");
    tr.appendChild(new Element("th").update("Title:"));
    tr.appendChild(new Element("td").update("<input type='text' id='bTxtTitle' name='title' size='70' maxlength='255' />"));
    tbody.appendChild(tr);
    // desc row
    tr = new Element("tr");
    tr.appendChild(new Element("th").update("Description:"));
    tr.appendChild(new Element("td").update("<textarea id='bTxtDesc' name='desc' rows='3' cols='67' ></textarea>"));
    tbody.appendChild(tr);
    // tags row
    tr = new Element("tr");
    tr.appendChild(new Element("th").update("Tags:"));
    tr.appendChild(new Element("td").update("<input type='text' id='bTxtTags' name='tags' size='70' maxlength='255' />"));
    tbody.appendChild(tr);

    table.appendChild(tbody);
    return table;
}

/* submit bookmark editing */
function submitEditB(args) {
    try{
        var index = args.index;
        var titleTxt = $("bTxtTitle");
        if(titleTxt.value.trim()=="") {
            titleTxt.focus();
            showItemNoticer(index, WORD_1);
            return false;
        }
        var tagsTxt = $("bTxtTags");
        if(tagsTxt.value.trim()=="") {
            tagsTxt.focus();
            showItemNoticer(index, WORD_2);
            return false;
        }
        var descTxt = $("bTxtDesc");
        var url = '/site/edit_bookmark';
        var json = {"g_l_id" : _bookmarks[index].g_l_id,
            "user_id" : _bookmarks[index].last_user_id,
            "title" : titleTxt.value,
            "desc" : descTxt.value,
            "tags" : tagsTxt.value,
            "user_name" : _bookmarks[index].u_name
        };

        PopWindow.instance.onLoading();
        new Ajax.Request(url,
        {asynchronous:true,
            evalScripts:false,
            onComplete:function(request){
                PopWindow.instance.onComplete({status:request.status});
                if(request.status=="200") {
                    var r = request.responseText.evalJSON();
                    var b = _GLOBAL_VAR['bookmarks'][index];
                    b.title = r["title"];
                    b.t_name = r["tags"].join(",");
                    b.description = r["desc"];
                    refreshBookmark(index);
                }
            },
            parameters:"json="+encodeURIComponent(Object.toJSON(json))}
    );
        return false;
    }catch(e) {
        alert(e);
    }
}

function refreshBookmark(index) {
    $("titleLink_"+index).update(_GLOBAL_VAR['bookmarks'][index].title);
    var tagsEl = $("tags_"+index).update("");
    var tags = _GLOBAL_VAR['bookmarks'][index].t_name.split(",");
    for(var i=0; i<tags.length; i++) {
        var a = document.createElement("a");
        if(tags[i].indexOf(" ")!=-1) {
            a.href= "/" + _GLOBAL_VAR['bookmarks'][index].g_name +"/bookmark/tag/"+encodeURIComponent("\""+tags[i]+"\"");
        }else {
            a.href="/" + _GLOBAL_VAR['bookmarks'][index].g_name +"/bookmark/tag/"+encodeURIComponent(tags[i]);
        }
        a.innerHTML = tags[i];
        tagsEl.appendChild(a);
        if(i<tags.length-1) {
            tagsEl.innerHTML += ", "
        }
    }
//    var desc = _GLOBAL_VAR['bookmarks'][index].description;
//    if(!desc.blank()) {
//        $("descContent_"+index).update(desc);
//        $("desc_"+index).show();
//    }else {
//        $("desc_"+index).hide();
//    }
}

function submitEditBComplete(request, index) {
    PopWindow.instance.onComplete({status:request.status});
    if(request.status=="200") {
        // var title = $F("bTxtTitle_"+index);
        var r = request.responseText.evalJSON();
        var t_name = r["t_name"];
        var desc = r["desc"];
        //_bookmarks[index].title = title;
        _bookmarks[index].t_name = t_name;
        _bookmarks[index].description = desc;
    }
}
/* submit bookmark editing */

function canDelete(index,admin){
    if(admin){
        return true;
    }
    var comments = _bookmarks[index].comments;
    for(var i=0;i<comments.size();i++){
        if(comments[i].u_name != _GLOBAL_VAR['current_user'].name){
            return false;
        }
    }

    var annotations = _bookmarks[index].annotations;
    for(var j=0;j<annotations.size();j++){
        if(annotations[j].u_name != _GLOBAL_VAR['current_user'].name){
            return false;
        }

        var stickyNotes = annotations[j].stickyNotes;
        for(var k=0;k<stickyNotes.size();k++){
            if(stickyNotes[k].u_name != _GLOBAL_VAR['current_user'].name){
                return false;
            }
        }
    }
    return true;
}

function deleteB(index) {
    if(canDelete(index) == false){
        alert(ALE_113);
        return;
    }
    if(confirm(ALE_25)) {
        doDeleteB([_bookmarks[index]]);
    }else {
        resetMoreAction();
        resetAllChecker();
        return false;
    }
}

function resetAllChecker() {
    for(var i=0; i<_GLOBAL_VAR['bookmarks'].length; i++) {
 resetChecker(i);
}

}

function doDeleteB(bA) {
    var delete_bm_infos = [];
    for(var i=0; i<bA.length; i++) {
        delete_bm_infos.push({link_id:bA[i].link_id, user_id:bA[i].last_user_id});
    }
    var url = '/site/delete_bookmark';
    new Ajax.Request(url,
    {method: "post",
        asynchronous:true,
        evalScripts:true,
        onLoading:function(request){doDeleteBLoading(request, bA)},
        onComplete:function(request){doDeleteBComplete(request, bA)},
        parameters:{
            group_id:bA[0].group_id,
            delete_bms:delete_bm_infos.toJSON()
        }}
);
    return false;
}
function doDeleteBLoading(request, bA) {
    showCentralNoticer(WORD_105);
}
function doDeleteBComplete(request, bA) {
    hideCentralNoticer();
    if(request.status == '200'){
        for(var i=0; i<bA.length; i++){
            var b = bA[i];
            delete(_bookmarks[b.index]);
            removeBookmark(b.index);
        }
        if(_bookmarks.compact().length==0) {
            self.location.reload();
        }
    }
    resetMoreAction();
    resetAllChecker();
}
function removeBookmark(index) {
    $("bookmarkItem_"+index).remove();
}


/* mark unread/readed */
function markUnread() {
    doMarkUnreadRead([_bookmarks[this.index]], 0);
}
function markRead() {
    doMarkUnreadRead([_bookmarks[this.index]], 1)
}
function doMarkUnreadRead(bA, mark) {
    if(bA.length==0) return false;
    var url_id = "";
    for(var i=0; i<bA.length; i++) {
        url_id += bA[i].url_id+","
    }
    if(url_id=="") return false;
    var url = "/bookmark_mana2/mark_readed";
    var params = "url_id="+url_id+"&readed="+mark;
    new Ajax.Request(url,
    {asynchronous:true,
        evalScripts:true,
        onLoading:function(request){markUnreadLoading(request, bA)},
        onComplete:function(request){markUnreadComplete(request, bA, mark)},
        parameters:params}
);
    return false;
}
function markUnreadLoading(request, bA) {
    for(var i=0; i<bA.length; i++) {
        showItemNoticer(bA[i].index, WORD_3);
    }
}
function markUnreadComplete(request, bA, mark) {
    if(request.status=="200") {
        for(var i=0; i<bA.length; i++) {
            var index = bA[i].index;
            hideItemNoticer(index);
            resetChecker(index);
            var menuItem = $(_floatOn+"_menu_item_"+index+"_4");
            if(mark==1) {
                menuItem.onclick = markUnread;
                menuItem.innerHTML = "Unread";
            }else {
                menuItem.onclick = markRead;
                menuItem.innerHTML = "Read";
            }
            /* change title style */
            var b = _bookmarks[index];
            b.readed = mark;
            var title = $("title_"+index);
            if(b.readed==1) {
                title.className = "title";
            }else {
                title.className = "title unread";
            }
            /* change title style */
        }

    }else if(request.status=="401") {

    }else {
        showItemNoticer(index, WORD_4);
    }
}
/* mark unread/readed */

/* mark bookmark mode */
function markModePublic() {
    doMarkMode([_bookmarks[this.index]], 0);
}
function markModePrivate() {
    doMarkMode([_bookmarks[this.index]], 2);
}
function doMarkMode(bA, mode) {
    if(bA.length==0) return false;
    var url_id = "";
    for(var i=0; i<bA.length; i++) {
        url_id += bA[i].url_id+","
    }
    if(url_id=="") return false;
    var url = "/bookmark_mana2/convert_mode";
    var params = "url_id="+url_id+"&mode="+mode;
    new Ajax.Request(url,
    {asynchronous:true,
        evalScripts:true,
        onLoading:function(request){markModeLoading(request, bA)},
        onComplete:function(request){markModeComplete(request, bA, mode)},
        parameters:params}
);
    return false;
}
function markModeLoading(request, bA) {
    for(var i=0; i<bA.length; i++) {
        showItemNoticer(bA[i].index, WORD_3);
    }
}
function markModeComplete(request, bA, mode) {
    if(request.status=="200") {
        for(var i=0; i<bA.length; i++) {
            var index = bA[i].index;
            hideItemNoticer(index);
            var menuItem = $(_floatOn+"_menu_item_"+index+"_5");
            if(mode==0) {
                menuItem.onclick = markModePrivate;
                menuItem.innerHTML = "Private";
            }else {
                menuItem.onclick = markModePublic;
                menuItem.innerHTML = "Public";
            }
            // mark mode
            var b = _bookmarks[index];
            b.mode = mode;
            var bMode = $("bMode_"+index);
            if(b.mode==0) {
                bMode.innerHTML = "";
            }else {
                bMode.innerHTML = "*";
            }
            // mark mode
            resetChecker(index);
        }
    }else if(request.status=="401") {

    }else {
        showItemNoticer(index, WORD_4);
    }
}
/* mark bookmark mode */

function publicB() {
}

/* share to group */
function showShareToTemp(index) {
    var bShareToTemp = $("bShareToTemp_"+index);
    if(bShareToTemp) {
        bShareToTemp.style.display = "";
    }else {
        var bookmarkItem = $("bookmarkItem_"+index);
        bookmarkItem.appendChild(loadBShareToTemp(index));
    }
    _floatMenu.switchMenu2(false, index);
}
function hideShareToTemp(index) {
    $("bShareToTemp_"+index).style.display = "none";
}
function loadBShareToTemp(index) {
    if(typeof(_bookmarks[index].bShareToTemp)=="undefined") {
        _bookmarks[index].bShareToTemp =  createBShareToTemp(index);
    }
    return _bookmarks[index].bShareToTemp;
}

function createBShareToTemp(index) {
    var table = document.createElement("table");
    var tbody = document.createElement("tbody");

    // share to row
    var tr = document.createElement("tr");
    var th = document.createElement("th");
    th.innerHTML = "Share to:";
    var selectItem = document.createElement("select");
    selectItem.id = "shareTo_"+index;
    selectItem.name = "shareTo";
    var option = document.createElement("option");
    option.innerHTML = "-------";
    option.value = "-1";
    selectItem.appendChild(option);
    for(var i=0; i<_groups.length; i++) {
        option = document.createElement("option");
        option.value = _groups[i].name;
        option.innerHTML = _groups[i].name;
        selectItem.appendChild(option);
    }
    var td = document.createElement("td");
    td.appendChild(selectItem);
    tr.appendChild(th);
    tr.appendChild(td);
    tbody.appendChild(tr);

    // button row
    tr = document.createElement("tr");
    th = document.createElement("th");
    th.innerHTML = "&nbsp;";

    td = document.createElement("td");
    td.className = "btnTd";

    input = document.createElement("input");
    input.type = "button";
    input.id = "bShareToSubmit_"+index;
    input.value = "Submit";
    input.className = "inputBtn";
    input.onclick = submitShareToB;
    td.appendChild(input);

    input = document.createElement("input");
    input.type = "button";
    input.id = "bShareToCancel_"+index;
    input.value = "Cancel";
    input.className = "inputBtn";
    input.onclick = cancelShareToB;
    td.appendChild(input);

    tr.appendChild(th);
    tr.appendChild(td);
    tbody.appendChild(tr);

    table.appendChild(tbody);
    table.id = "bShareToTemp_"+index;
    table.className = "bookmarkShareToTable";
    /*var form = document.createElement("form");
    form.id = "bShareToTemp_"+index;
    form.className = "bookmarkShareToForm";
    form.onsubmit = submitShareToB;
    form.method = "post";
    form.appendChild(table);*/
    return table;
}
function cancelShareToB() {
    var index = this.id.split("_")[1];
    showDisplayTemp(index);
    hideShareToTemp(index);
    hideItemNoticer(index);
}
function cancelShareToB2(index) {
    resetChecker(index);
    _floatMenu.switchMenu2(true, index);
    if(_checkedItemCount==0) {
        hideBatchShareToBBB();
        showBListOB();
    }
}

function shareBTo() {
    switchBToShareToMode(this.index);
}
function switchBToShareToMode(index) {
    hideDisplayTemp(index);
    showShareToTemp(index);
}

function createBatchBShareToTemp() {
    var table = document.createElement("table");
    var tbody = document.createElement("tbody");

    // share to row
    var tr = document.createElement("tr");
    var th = document.createElement("th");
    th.innerHTML = "Share to:";
    tr.appendChild(th);
    var selectItem = document.createElement("select");
    selectItem.id = "batchShareTo";
    selectItem.name = "shareTo";
    var option = document.createElement("option");
    option.innerHTML = "-------";
    option.value = "-1";
    selectItem.appendChild(option);
    for(var i=0; i<_groups.length; i++) {
        option = document.createElement("option");
        option.value = _groups[i].name;
        option.innerHTML = _groups[i].name;
        selectItem.appendChild(option);
    }
    var td = document.createElement("td");
    td.appendChild(selectItem);
    tr.appendChild(td);
    td = document.createElement("td");
    td.className = "btnTd";
    input = document.createElement("input");
    input.type = "button";
    input.id = "batchBShareToSubmit";
    input.value = "Batch Share To";
    input.onclick = submitBatchShareToB;
    td.appendChild(input);

    input = document.createElement("input");
    input.type = "button";
    input.id = "batchBShareToCancel";
    input.value = "Cancel";
    input.onclick = cancelBatchShareToB;
    td.appendChild(input);
    tr.appendChild(td);

    tbody.appendChild(tr);

    table.appendChild(tbody);
    table.id = "batchBShareToTemp";
    table.className = "bookmarkShareToTable";
    /*  var form = document.createElement("form");

    form.onsubmit = submitBatchShareToB;
    form.method = "post";
    form.appendChild(table);  */
    return table;
}
function cancelBatchShareToB() {
    var checkedBA = whichBChecked();
    for(var i=0; i<checkedBA.length; i++) {
        cancelShareToB2(checkedBA[i].index);
    }
    showAllChecker();
}
function submitBatchShareToB() {
    try{
        var urlIDA = [];
        var indexA = [];
        var checkedBA = whichBChecked();
        for(var i=0; i<checkedBA.length; i++) {
            urlIDA.push(checkedBA[i].url_id);
            indexA.push(checkedBA[i].index);
        }
        var groupName = $F("batchShareTo");
        if(groupName == "-1") return false;
        var url = '/bookmark_mana2/share_to';
        var params = "url_id="+urlIDA.join(",")+"&group_name="+groupName;
        new Ajax.Request(url,
        {asynchronous:true,
            evalScripts:true,
            onLoading:function(request){submitBatchShareToBLoading(request)},
            onComplete:function(request){submitBatchShareToBComplete(request, indexA, groupName)},
            parameters:params}
    );
        return false;
    }catch(e) {
        alert(e);
    }
}
function submitBatchShareToBLoading(request) {
    showHeaderNoticer(WORD_3);
    hideBatchShareToBBB();
}

function submitBatchShareToBComplete(request, indexA, groupName) {
    if(request.status!="200") {
        showHeaderNoticer(WORD_7);
        for(var i=0; i<indexA.length; i++) {
            resetChecker(indexA[i]);
            var sharedTo = $("sharedTo_"+indexA[i]);
            var a = document.createElement("a");
            a.href = "";
            a.innerHTML = groupName;
            sharedTo.appendChild(a);
            _floatMenu.switchMenu2(true, indexA[i]);
        }
        showAllChecker();
        showBListOB();
    }else if(request.status=="401") {
    }else {
        showHeaderNoticer(WORD_4);
        showBatchShareToBBB();
    }
}

function switchBToShareToMode2(index) {
    _floatMenu.switchMenu2(false, index);
}

function submitShareToB() {
    try{
        var index = this.id.split("_")[1];
        var b = _bookmarks[index];
        var groupName = $F("shareTo_"+index);
        if(groupName == "-1") return false;
        var url = '/bookmark_mana2/share_to';
        var params = "url_id="+b.url_id+"&group_name="+groupName;
        new Ajax.Request(url,
        {asynchronous:true,
            evalScripts:true,
            onLoading:function(request){submitShareToBLoading(request, index)},
            onComplete:function(request){submitShareToBComplete(request, index)},
            parameters:params}
    );
        return false;
    }catch(e) {
        alert(e);
    }
}

function submitShareToBLoading(request, index) {
    var form = $("bShareToTemp_"+index);
    showItemNoticer(index, WORD_3);
    hideShareToTemp(index);
}

function submitShareToBComplete(request, index) {
    if(request.status=="200") {
    }else if(request.status=="401") {
    }else {
        showShareToTemp(index);
        showItemNoticer(index, WORD_4);
    }
}
/* share to group */

/* bookmark item noticer */
function showItemNoticer(index, word) {
    var noticer = $("itemNoticer_"+index);
    noticer.style.display = "";
    noticer.innerHTML = word;
}
function hideItemNoticer(index) {
    $("itemNoticer_"+index).style.display = "none";
}

function showHeaderNoticer(word) {
    var noticer = $("headerNoticer");
    noticer.style.display = "";
    noticer.innerHTML = word;
}
function hideHeaderNoticer() {
    $("headerNoticer").style.display = "none";
}
function showNewBNoticer(index,word){
    var noticer = $("newBNoticer_"+index);
    noticer.style.display = "";
    noticer.innerHTML = word;
}
function hideNewBNoticer(index){
    $("newBNoticer_"+index).style.display = "none";
}
/* bookmark item noticer */

/* more action */
function doAction() {
    var cmd = this.value;
    switch(cmd) {
        case '0':{
                batchForwardB();
            }break;
        case '1':{
                batchEditB(cmd);
            }break;
        case '2':{
                batchmarkMode(0);
            }break;
        case '3':{
                batchmarkMode(2);
            }break;
        case '4':{
                batchDeleteB();
            }break;
        case '5':{
            }break;
        case '6':{
                batchExtract();
            }break;
        case '7':{
                batchMarkUnreadRead(1);
            }break;
        case '8':{
                batchMarkUnreadRead(0);
            }break;
        case '9':{
                newBookmark();
            }break;
        case '10':{
                blogThis();
            }break;
        case '11':{
                batchShareTo("11");
            }break;
        case '13':
        case '14':{
                setDefaultEC(cmd);
            }break;
        case '20':
        case '50':
        case '100':{
                changeListCount(cmd);
            }break;
    }
}
function batchForwardB(){
    var checkedBA = whichBChecked();
    if(checkedBA.length==0) {
        alert("No item(s) selected");
        resetMoreAction();
        return false;
    }
    var urlIDA = [];
    for(var i=0; i<checkedBA.length; i++) {
        urlIDA.push(checkedBA[i].index);
    }
    forwardB(urlIDA);
    //postToDraftForward(urlIDA.join(","));
}
function batchEditB(cmd) {
    var checkedBA = whichBChecked();
    if(checkedBA.length==0) {
        alert("No item(s) selected");
        resetMoreAction();
        return false;
    }
    switchToBatchEditB(checkedBA);
    lockMoreAction(cmd);
}
function switchToBatchEditB(checkedBA){
    for(var i=0; i<checkedBA.length; i++) {
        switchBToEditMode(checkedBA[i].index);
    }
    showBatchEditBBB();
    hideBListOB();
}
function batchSubmitEditB() {
    var checkedBA = whichBChecked();
    for(var i=0; i<checkedBA.length; i++) {
        $("bEditSubmit_"+checkedBA[i].index).click();
    }
}
function batchCancelEditB() {
    var checkedBA = whichBChecked();
    for(var i=0; i<checkedBA.length; i++) {
        $("bEditCancel_"+checkedBA[i].index).click();
    }
}
function batchShareTo(cmd) {
    var checkedBA = whichBChecked();
    if(checkedBA.length==0) {
        alert("No item(s) selected");
        resetMoreAction();
        return false;
    }
    switchToBatchShareTo(checkedBA);
    lockMoreAction(cmd);
}
function switchToBatchShareTo(checkedBA) {
    for(var i=0; i<checkedBA.length; i++) {
        switchBToShareToMode2(checkedBA[i].index);
    }
    hideAllChecker();
    showBatchShareToBBB();
    hideBListOB();
}
function showBatchShareToBBB() {
    var bar = $("shareToBBB");
    if(bar.innerHTML == "") {
        bar.appendChild(createBatchBShareToTemp());
    }
    bar.style.display = "";
}
function hideBatchShareToBBB() {
    $("shareToBBB").style.display = "none";
}


function showBatchEditBBB() {
    var bar = $("editBBB");
    if(bar.innerHTML == "") {
        var input = document.createElement("input");
        input.type = "button";
        input.value = "Submit All";
        input.onclick = batchSubmitEditB;
        bar.appendChild(input);
        input = document.createElement("input");
        input.type = "button";
        input.value = "Cancel All";
        input.onclick = batchCancelEditB;
        bar.appendChild(input);
    }
    bar.style.display = "";
}
function hideBatchEditBBB() {
    //$("editBBB").style.display = "none";
}
function showBListOB() {
    //$("bookmarkListOB").style.display = "";
}
function hideBListOB() {
    //$("bookmarkListOB").style.display = "none";
}
function batchMarkUnreadRead(mark) {
    var checkedBA = whichBChecked();
    var bA = [];
    for(var i=0; i<checkedBA.length; i++) {
        if(checkedBA[i].readed == mark) {
            resetChecker(checkedBA[i].index);
        }else {
            bA.push(checkedBA[i]);
        }
    }
    doMarkUnreadRead(bA, mark);
}
function batchmarkMode(mode) {
    var checkedBA = whichBChecked();
    var bA = [];
    for(var i=0; i<checkedBA.length; i++) {
        if(checkedBA[i].mode == mode) {
            resetChecker(checkedBA[i].index);
        }else {
            bA.push(checkedBA[i]);
        }
    }
    doMarkMode(bA, mode);
}
function batchShareBTo() {
}
function batchDeleteB() {
    if(confirm(ALE_115)) {
        doDeleteB(whichBChecked());
    }else {
        resetMoreAction();
        resetAllChecker();
        return false;
    }
}
//function batchExtract() {
//    var checkedBA = whichBChecked();
//    if(checkedBA.length==0) {
//        alert("No item(s) selected");
//        resetMoreAction();
//        return false;
//    }
//    var urlIDA = [];
//    for(var i=0; i<checkedBA.length; i++) {
//        urlIDA.push(checkedBA[i].index);
//    }
//    postToExtract(urlIDA.join(","));
//}
/*  new bookmark  */
function newBookmark() {
    //switchNewBEditor();
    /*var urlIDA = [];
    for(var i=0; i<checkedBA.length; i++) {
    urlIDA.push(checkedBA[i].g_url_u);
    }
    postToDraftForward(urlIDA.join(","));*/
    postToNewBookmark($("group_name").value);
}
function postToNewBookmark(g_name) {
    var form = document.createElement("form");
    form.method = "post";
    form.action = "/site/new_bookmark";
    var hidden = document.createElement("input");
    hidden.type = "hidden";
    hidden.name = "g_name";
    hidden.value = g_name;
    form.appendChild(hidden);
    form.style.display = "none";
    document.body.appendChild(form);
    form.submit();
}
function switchNewBEditor() {
    var editor = $("newBEditor");
    var newBForm = document.forms["newBForm"];
    if(newBForm) {
        resetNewBForm(newBForm);
        if(editor.style.display=="") {
            editor.style.display = "none";
            resetMoreAction();
            return false;
        }else {
            editor.style.display = "";
            lockMoreAction('9');
            return false;
        }
    }else {
        newBForm = loadNewBEForm();
        $("newBEditor").appendChild(newBForm);
        lockMoreAction('9');
        return false;
    }
}
function resetNewBForm(form){
    hideNewBNoticer('url');
    hideNewBNoticer('title');
    hideNewBNoticer('tags');
    form.url.value = "";
    form.title.value = "";
    form.tags.value = "";
}
function submitNewB() {
    try{
        var form = document.forms["newBForm"];
        var url = form.url;
        var title = form.title;
        var tags = form.tags;
        validator = new Validator();
        if(url.value.trim()=="" || !validator.IsUrl(url.value.trim())){
            url.focus();
            showNewBNoticer('url', WORD_101);
            return false;
        }
        if(title.value.trim()=="") {
            title.focus();
            showNewBNoticer('title', WORD_102);
            return false;
        }
        if(tags.value.trim()=="") {
            tags.focus();
            showNewBNoticer('tags', WORD_103);
            return false;
        }
        //ajax
        var url = '/site/add_bookmark';
        var params = Form.serialize(this) + "&g_name=" +  $("group_name").value;
        alert(params);
        new Ajax.Request(url,
        {asynchronous:true,
            evalScripts:true,
            onLoading:function(request){submitNewBLoading(request, index)},
            onComplete:function(request){submitNewBComplete(request, index)},
            parameters:params}
    );
        return false;
    }catch(e) {
        alert(e);
    }
}
function submitNewBLoading(request, index) {
    var form = $("bEditTemp_"+index);
    showItemNoticer(index, WORD_3);
    hideEditTemp(index);
}
function submitNewBComplete(request, index) {
    if(request.status=="200") {
        var title = $F("bTxtTitle_"+index);
        var r = request.responseText.evalJSON();
        var t_name = r["t_name"];
        var desc = r["desc"];
        _bookmarks[index].title = title;
        _bookmarks[index].t_name = t_name;
        _bookmarks[index].description = desc;
        switchBToDisplayMode(index);
    }else if(request.status=="401") {
    }else {
        showEditTemp(index);
        showItemNoticer(index, WORD_4);
    }
}
function loadNewBEForm() {
    var form = document.createElement("form");
    form.name = "newBForm";
    form.onsubmit = submitNewB;
    var label = document.createElement("label");
    label.innerHTML = "Url:";
    var input = document.createElement("input");
    input.type = "text";
    input.name = "url";
    input.className = "inputTxt";
    var noticer = document.createElement("div");
    noticer.className = "tailNoticer";
    noticer.id = "newBNoticer_url";
    noticer.style.display = "none";
    form.appendChild(label);
    form.appendChild(input);
    form.appendChild(noticer);
    label = document.createElement("label");
    label.innerHTML = "Title:";
    input = document.createElement("input");
    input.type = "text";
    input.name = "title";
    input.className = "inputTxt";
    noticer = document.createElement("div");
    noticer.className = "tailNoticer";
    noticer.id = "newBNoticer_title";
    noticer.style.display = "none";
    form.appendChild(label);
    form.appendChild(input);
    form.appendChild(noticer);
    label = document.createElement("label");
    label.innerHTML = "Tags:";
    input = document.createElement("input");
    input.type = "text";
    input.name = "tags";
    input.className = "inputTxt";
    noticer = document.createElement("div");
    noticer.className = "tailNoticer";
    noticer.id = "newBNoticer_tags";
    noticer.style.display = "none";
    form.appendChild(label);
    form.appendChild(input);
    form.appendChild(noticer);
    var bottomBtn = document.createElement("div");
    bottomBtn.className = "bottomBtn";
    input = document.createElement("input");
    input.type = "submit";
    input.value = "New Bookmark"
    bottomBtn.appendChild(input);
    input = document.createElement("input");
    input.type = "button";
    input.value = "Cancel";
    input.onclick = switchNewBEditor;
    bottomBtn.appendChild(input);
    form.appendChild(bottomBtn);
    return form;
}
/* new bookmark */
function blogThis() {
}
function whichBChecked() {
    var checkedBA = [];
    for(var i=0; i<_bookmarks.length; i++) {
        if(typeof(_bookmarks[i])=="undefined") continue;
        if(_bookmarks[i].checked) checkedBA.push(_bookmarks[i]);
    }
    return checkedBA;
}
function resetMoreAction() {
    try{
        for(var i=0; i<_moreActionSelectCount; i++) {
            var action = $("moreAction_"+i);
            action.value = "-1";
            action.disabled = false;
        }
    }catch(e)
    {}
}
function lockMoreAction(cmd) {
    for(var i=0; i<_moreActionSelectCount; i++) {
        var action = $("moreAction_"+i);
        action.value = cmd;
        action.disabled = true;
    }
}
/* more action */
function postToDraftForward(urlID) {
    var form = document.createElement("form");
    form.method = "post";
    form.action = "/dmail/draft_forward";
    var hidden = document.createElement("input");
    hidden.type = "hidden";
    hidden.name = "g_url_u";
    hidden.value = urlID;
    form.appendChild(hidden);
    form.style.display = "none";
    document.body.appendChild(form);
    form.submit();
}
function postToExtract(urlID) {
    var form = document.createElement("form");
    form.method = "post";
    form.action = "/site/extract_bm_info";
    var hidden = document.createElement("input");
    hidden.type = "hidden";
    hidden.name = "g_url_u";
    hidden.value = urlID;
    form.appendChild(hidden);
    form.style.display = "none";
    document.body.appendChild(form);
    form.submit();
}
function removeAnnotation(index) {
    if(confirm(WORD_10)) {
        var indexA = index.split("_");
        var annotation = _bookmarks[indexA[0]].annotations[indexA[1]];
        doRemoveAnnotation(annotation,_bookmarks[indexA[0]]);
    }
}

function doRemoveAnnotation(annotation,bookmark) {
    var url = '/site/delete_highlight';
    new Ajax.Request(url,
    {method:"post",
        asynchronous:true,
        evalScripts:true,
        onLoading:function(request){doRemoveAnnotationLoading(request, annotation.index)},
        onComplete:function(request){doRemoveAnnotationComplete(request, annotation.index)},
        parameters:{
            ann_id:annotation.id,
            g_l_id:annotation.g_l_id,
            user_id:annotation.user_id
        }}
);
    return false;
}
function doRemoveAnnotationLoading(request, index) {

}
function doRemoveAnnotationComplete(request, index) {
    if(request.status=="200") {
        var indexA = index.split("_");
        delete(_bookmarks[indexA[0]].annotations[indexA[1]]);
        Element.remove("annotation_"+index);
        renderCount();
    }else {
    }
}
function markDefaultECAction(mark) {
    for(var i=0; i<_moreActionSelectCount; i++) {
        var option = $("defaultEC_"+i);
        // expand
        if(mark=="0") {
            option.value = "13";
            option.innerHTML = option.innerHTML.replace("expand", "collapse");
            // collapse
        }else {
            option.value = "14";
            option.innerHTML = option.innerHTML.replace("collapse", "expand");
        }
    }
}



function Validator(){
    this.isEmail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    this.isEmail2 = /^.+<\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*>$/;
    this.isTags = /^[^%&]+$/;
    this.isTag = /^[^%&\s]*$/;
    this.isUsername = /^[a-zA-Z]([0-9a-zA-Z\-_]{5,15})$/;
    this.isUrl = /^https?:\/\/.+$/;
    this.IsEmail = function (email) { re = this.isEmail; return (re.test(email)); };
    this.IsEmail2 = function (email) { re = this.isEmail2; return (re.test(email)); };
    this.IsTags = function (tags) { re = this.isTags; return (re.test(tags)); };
    this.IsTag = function (tag) { re = this.isTag; return (re.test(tag)); };
    this.IsUsername = function (username) { re = this.isUsername; return (re.test(username)) }; this.IsPwd = function (pwd) { if(pwd.length>=6 && pwd.length<=32) { return true; }else { return false; } };
    this.IsRealname = function(realname){if(realname.length>=1 && realname.length<=64){return true; }else{ return false; } }
    this.IsTitle = function(title){if(title.length<=255){return true; }else { return false; } }
    this.IsSignature = function(signature) {if(signature.length<=255){ return true; }else { return false; } }
    this.IsUrl = function(url) {re = this.isUrl; return (re.test(url)) }
};

function forwardB(index_a) {
    forwardItem({'type':'bookmark', 'index_a':index_a});
}

/*======================================
 *
 * forward item
 *
 *======================================*/
function forwardItem(args) {
    var toggler = this;
    var pw;

    /*------------------------------------
     * init for each type
     *------------------------------------*/
    if(args.type=='list') {
        var root = "column";
        pw = new PopWindow({optEnabled: true, left:160, top:20, width:500, title:"Forward", root:root});
        pw.submitBtn.value = "Forward";
        pw.cancelBtn.value = "Close";

    }else if(args.type=='bookmark') {
        var root="leftColumn";
        var p = $("leftColumn").viewportOffset();
        var top = 0;
        var index_a = args.index_a;
        if(p[1]<0) {top = 0 - p[1];}
        pw = new PopWindow({optEnabled: true, left:120, top:top+20, width:500, title:"Send To", root:root});
        pw.cancel = function(){resetMoreAction();resetAllChecker();}
        index_a.each(function(index){
            switchDetails(index, "collapse");
        })
    }

    pw.appendNode(loadForwardTemp(args));
    pw.submit = function(){
        var url = BOOKMARK_HOST + "/message_mana/send_msg";
        var sendTo = $("sendToSelector").value;
        var json = {};
        switch(sendTo) {
            case "all":{
                    json["send_type"] = "all";
                }break;
            case "some":{
                    json["send_type"] = "some";
                    json["send_to"] = _GLOBAL_VAR[_GLOBAL_VAR["bACCID"]+"_instance"].getRecItems();
                }break;
            default: {
                    json["send_type"] = "list";
                    json["send_to"] = sendTo;
                }break;
        }
        json["privacy"] = $("sendToPrivacy").getAttribute("v");
        json["star"] = $("sendToStar").getAttribute("v");
        var ff = $("forwardForm");
        json["subject"] = ff.subject.value;
        json["message"] = ff.message.value;
        if(args.type=='bookmark') {
            if(ff.includeA.checked){json["quotes"] = $("forwardPreview").innerHTML;}
        }else if(args.type='list') {
            json["quotes"] = $("forwardPreview").innerHTML;
        }
        this.onLoading();
        CrossSubDomainAjax.request(url,
        {asynchronous:true,
            evalScripts:false,
            onComplete:function(request){
                PopWindow.instance.onComplete({status:request.status});
                resetMoreAction();
                resetAllChecker();
            },parameters:"json="+encodeURIComponent(Object.toJSON(json))});
        return false;
    }.bind(pw);

    /*--------------------------------
     * init contacts
     *--------------------------------*/
    if(!ACContact.contacts || ACContact.contacts.length==0){
        pw.onLoading();
        var url = BOOKMARK_HOST + "/user_mana2/load_contacts";
        CrossSubDomainAjax.request(url,
        {asynchronous:true,
            evalScripts:false,
            method:'get',
            onComplete:function(request){
                if(request.status=="200") {
                    var contacts = request.responseText.evalJSON();
                    _GLOBAL_VAR[_GLOBAL_VAR["bACCID"]+"_instance"] = new ACContact({rec:$(_GLOBAL_VAR["bACCID"]), contacts:contacts});
                    initContactsSelect();
                    PopWindow.instance.noticer.hide();
                    PopWindow.instance.content.show();
                    PopWindow.instance.opt.show();
                    PopWindow.instance.reCalShadowRange();
                }}});
        return false;
    }else {
        _GLOBAL_VAR[_GLOBAL_VAR["bACCID"]+"_instance"] = new ACContact({rec:$(_GLOBAL_VAR["bACCID"])});
        initContactsSelect();
    }
}

/*=================================
 *
 * forward template
 *
 *=================================*/
function loadForwardTemp(args) {
    var table = new Element("table", {id:"forwardTable"});
    var tbody = new Element("tbody");

    // send to
    var tr = new Element("tr");
    tr.appendChild(new Element("th").update("Send to:"));
    var td = new Element("td", {id:"sendToTD"});
    tr.appendChild(td);
    tbody.appendChild(tr);

    // contacts
    tr = new Element("tr", {id:"contactRow"});
    tr.appendChild(new Element("th"));
    td = new Element("td");
    var div = new Element("div", {className:"accOuter"});
    div.appendChild(new Element("div", {id:_GLOBAL_VAR["bACCID"], className:"autocompleteContacts"}));
    td.appendChild(div);
    tr.appendChild(td);
    tbody.appendChild(tr);

    // subject
    tr = new Element("tr", {id:"subjectRow"});
    tr.appendChild(new Element("th").update("Subject:"));
    var subEl = new Element("input", {type:"text", name:"subject", maxlength:255, size:70, className:"inputTxt"});

    if(args.type=='bookmark') {
        subEl.value = _GLOBAL_VAR['bookmarks'][args.index_a[0]].title.truncate(75);
    }else if(args.type=='list') {
        subEl.value = _GLOBAL_VAR['list'].title;
    }

    td = new Element("td");
    td.appendChild(subEl);
    tr.appendChild(td);
    tbody.appendChild(tr);

    // option 1
    tr = new Element("tr");
    tr.appendChild(new Element("th").update("Message:"));
    td = new Element("td", {id:"forwardOptions"});
    var privacy = new Element("a", {id:"sendToPrivacy", href:"javascript:void(0);"}).update("Private");
    privacy.setStyle({"background":"url(/images/lock.gif) left top no-repeat", "paddingLeft":"16px"});
    privacy.setAttribute("v", "private");
    privacy.onclick = function(){
        var v = this.getAttribute("v");
        if(v=="private"){
            this.update("Public");
            this.setAttribute("v", "public");
            this.setStyle({"background":"url(/images/unlock.gif) left top no-repeat", "paddingLeft":"16px"});
        }else{
            this.update("Private");
            this.setAttribute("v", "private");
            this.setStyle({"background":"url(/images/lock.gif) left top no-repeat", "paddingLeft":"16px"});
        }
    }.bind(privacy);
    td.appendChild(privacy);
    var star = new Element("a", {id:"sendToStar", href:"javascript:void(0);"}).update("Just chit-chat");
    star.setStyle({"background":"url(/images/unstarred.gif) left top no-repeat", "paddingLeft":"16px"});
    star.setAttribute("v", "no");
    star.onclick = function(){
        var v = this.getAttribute("v");
        if(v=="yes"){
            this.update("Just chit-chat");
            this.setAttribute("v", "no");
            this.setStyle({"background":"url(/images/unstarred.gif) left top no-repeat", "paddingLeft":"16px"});
        }else{
            this.update("Important message!");
            this.setAttribute("v", "yes");
            this.setStyle({"background":"url(/images/starred.gif) left top no-repeat", "paddingLeft":"16px"});
        }
    }.bind(star);
    td.appendChild(star);

    if(args.type=='bookmark') {
        td.appendChild(new Element("label").update("<input type='checkbox' checked='checked' name='includeA' onclick='includeAnnotations.bind(this)();' /> Include annotations"));
        //td.appendChild(new Element("a").update("Rich formatting &raquo;"));
    }

    tr.appendChild(td);
    tbody.appendChild(tr);

    // message
    tr = new Element("tr");
    td = new Element("td", {"colspan":"2"});
    td.appendChild(new Element("textarea", {"name":"message", className:"inputTxt"}));
    tr.appendChild(td);
    tbody.appendChild(tr);
    table.appendChild(tbody);

    var form = new Element("form", {id:"forwardForm"});
    form.setAttribute('autocomplete', 'off');
    form.appendChild(table);

    // preview box
    var preBox = new Element("div", {"id":"forwardPreview"});
    if(args.type=='bookmark') {
        preBox.appendChild(loadAnnotations(args.index_a))
    }else if(args.type=='list') {
        preBox.setStyle({"height":"50px"});
        if(_GLOBAL_VAR['list'].mode==2) {
            preBox.update("<p><a href='"+BOOKMARK_HOST+"/list?token="+_GLOBAL_VAR['token']+"'>"+_GLOBAL_VAR['list'].title+"</a></p>");
        }else {
            preBox.update("<a href='"+BOOKMARK_HOST+"/list/"+_GLOBAL_VAR['username']+"/"+_GLOBAL_VAR['list'].uri+"'>"+_GLOBAL_VAR['list'].title+"</a>");
        }
    }
    form.appendChild(preBox);
    return form;

}

function loadAnnotations(index_a) {
    var contentOutter = new Element("div", {className:"extraAContentOutter", id:"extraAContentOutter"});
    var content = new Element("dl", {className:"extraAContent", id:"extraAContent"});
    for(var k=0; k<index_a.length; k++) {
        var index = index_a[k];
        var title = $("titleLink_"+index);
        var t = new Element("a", {href:title.href,className:"eTitle"}).update(title.innerHTML);
        var dt = new Element("dt");
        dt.appendChild(t);
        content.appendChild(dt);
        var dd = new Element("dd");
        var aUL = new Element("ul", {className:"eAUL"});
        var annotations = $("annotations_"+index);
        var a = annotations.down();
        while(a){
            var h = a.down();
            var aLI = new Element("li");
            aLI.appendChild(new Element("div",{className:"eHContent"}).update(h.down().innerHTML));
            if(h.next()) {
                var sUL = new Element("ul",{className:"eSUL"});
                var s = h.next().down().down();
                while(s) {
                    var sLI = new Element("li")
                    sLI.appendChild(new Element("span", {className:"eSContent"}).update(s.down().innerHTML));
                    var tail = new Element("span", {className:"eSTail"}).update(" - posted by ");
                    var poster = s.down().next().down().next().innerHTML;
                    tail.appendChild(new Element("a", {href:BOOKMARK_HOST+"/user/"+poster}).update(poster));
                    sLI.appendChild(tail);
                    sUL.appendChild(sLI);
                    s = s.next();
                }
                aLI.appendChild(sUL);
            }
            aUL.appendChild(aLI);
            a = a.next();
        }
        dd.appendChild(aUL);
        content.appendChild(dd);
    }
    contentOutter.appendChild(content);
    return contentOutter;
}

function includeAnnotations(){
    var preBox = $("forwardPreview");
    if(this.checked){
        preBox.show();
    }else {
        preBox.hide();
    }
}

function initContactsSelect() {
    var select = new Element("select", {id:"sendToSelector"});
    select.onchange = function(){
        if(this.value=="some") {
            $(_GLOBAL_VAR["bACCID"]).show();
        }else {
            _GLOBAL_VAR[_GLOBAL_VAR["bACCID"]+"_instance"].recBox.reset();
            $(_GLOBAL_VAR["bACCID"]).hide();
        }
    }.bind(select);
    select.appendChild(new Element("option", {value:"all"}).update("all my friends"));
    select.appendChild(new Element("option", {value:"some", "selected":"selected"}).update("some of my friends"));
    select.setAttribute("autocomplete", "off");
    if(ACContact.contactListsID.length>0) {
        select.appendChild(new Element("option", {className:"sep", "disabled":"disabled"}).update("--My Contact Lists--"));
        ACContact.contactListsID.each(function(cIndex){
            var list = ACContact.contacts[cIndex];
            select.appendChild(new Element("option", {value:list.id}).update(list.extra.title));
        });
    }
    var sendTo = $("sendToTD");
    sendTo.update("");
    sendTo.appendChild(select);
}

/*------------------------------------
 * menu
 *-----------------------------------*/
function batchExtract() {
    var checkedBA = whichBChecked();
    if(checkedBA.length==0) {
        alert("No item(s) selected");
        resetMoreAction();
        return false;
    }
    var index_a = [];
    for(var i=0; i<checkedBA.length; i++) {
        index_a.push(checkedBA[i].index);
    }
    extraA(index_a);
}
function extraA(i) {
    var index_a = [];
    var root = "leftColumn";
    if(typeof(i)=="number"){index_a.push(i);root = "bookmarkItem_"+i;
    }else{index_a = i;root = "bookmarkItem_" + i[0]}
    var pw = new PopWindow({optEnabled: true, left:120, top:20, width:500, title:"Extract Annotations", root:root});
    pw.appendNode(loadAnnotations(index_a));
    pw.cancel = function(){resetMoreAction();resetAllChecker();}
    pw.submitBtn.value = "Copy";
    pw.cancelBtn.value = "Close";
    pw.submit = function(){
        _copy("<dl>"+$("extraAContent").innerHTML+"</dl>");
        pw.submitBtn.value = "Copied";
    }
}

/*------------------------------------------
 * expand and collapse
 *-----------------------------------------*/
function switchDetails(index, eoc) {
    var el = $("bookmarkDetails_"+index);
    var eocItem = $("eoc_"+index);
    if(typeof(eoc)=="undefined") eoc = null;
    if(eoc) {
        if(eoc=="collapse") {
            el.hide();
            eocItem.innerHTML = eocItem.innerHTML.replace(DIS_6, DIS_7);
            _GLOBAL_VAR['bookmarks'][index].expand = false;
            switchCEditor(index, "collapse");
        }else {
            el.show();
            eocItem.innerHTML = eocItem.innerHTML.replace(DIS_7, DIS_6);
            _GLOBAL_VAR['bookmarks'][index].expand = true;
        }
    }else {
        if(el.visible()) {
            el.hide();
            eocItem.innerHTML = eocItem.innerHTML.replace(DIS_6, DIS_7);
            _GLOBAL_VAR['bookmarks'][index].expand = false;
            switchCEditor(index, "collapse");
        }else {
            el.show();
            eocItem.innerHTML = eocItem.innerHTML.replace(DIS_7, DIS_6);
            _GLOBAL_VAR['bookmarks'][index].expand = true;
        }
    }
    checkEOCStatus();
}

function checkEOCStatus() {
    var expand = false;
    var collapse = false
    for(var i=0; i<_GLOBAL_VAR['bookmarks'].length; i++) {
        if(!_GLOBAL_VAR['bookmarks'][i]) continue;
        if(_GLOBAL_VAR['bookmarks'][i].expand) {
            expand = true;
        }else {
            collapse = true;
        }
    }
    if(expand && !collapse) {
        _GLOBAL_VAR['expand'] = true;
        var i=0;
        while($("EOCSwitcher_"+i)){
            $("EOCSwitcher_"+i).innerHTML = DIS_9;
            i++;
        }
    }else if(!expand && collapse) {
        _GLOBAL_VAR['expand'] = false;
        var i=0;
        while($("EOCSwitcher_"+i)){
            $("EOCSwitcher_"+i).innerHTML = DIS_8;
            i++;
        }
    }
}