﻿var __iseditnow = false;    //是否处于编辑状态
var __rawID = "";           //源对象ID
var __rawhtml = "";         //原始源
var __db_keys = "";         //字段数据



//显示一个隐藏的编辑器窗口
function __InitEditor() { 
    var edit_hidden_html = ("<style>#content_editor p{ border:1px dotted #cccccc;margin:1px;}</style><div id=editor_div style='display:none;'>");
    edit_hidden_html += ("<div id=hediv style='margin:0px;padding:0px;background:#FFEEEE;position:relative;border:1px solid Gray; overflow-y:scroll;'>");
    edit_hidden_html += ("    <div id='content_editor' style='background:#FFEEEE;border:1px solid #CFDAE7;position:relative;' contentEditable='true'></div>");
    edit_hidden_html += "           <!--按钮--><div id=hebuttons style='filter:alpha(style=0,opacity=88);font-size:9pt; position:absolute;width:16px;height:16px;top:1px;right:1px;'>";
    edit_hidden_html += ("              <a href='javascript:__close_editor();' ><img src='/content/close.png' border='0' /></a>");
    edit_hidden_html += "           </div>";
    edit_hidden_html += ("      </div>");
    edit_hidden_html += ("</div>");
    var ppp = document.createElement("div");
    ppp.setAttribute("title", "(@_@)【隐藏内容，用于支持即时编辑功能】");
    ppp.setAttribute("style","display:none");
    ppp.innerHTML = edit_hidden_html;
    document.body.appendChild(ppp); 
}

function __close_editor() {
    if (window.confirm("即将退出编辑状态，是否保存对内容的修改？\r\n *确定：保存\r\n *取消：不保存"))
        __saveEdit();
    else
        __cacnelEdit();
    
}


window.attachEvent("onload", __InitEditor); 

//对象选择器
function __$(id) {
    return document.getElementById(id);
}
//保存修改
function __saveEdit() {
    //主键名/值，内容字段名/内容字段值，用户名
    var editedhtml = __$("content_editor").innerHTML;
    editedhtml = editedhtml.replace(/&/g, "$link$");
    var ajaxuri = "content=" + encodeURI(editedhtml) + "&p=" + __db_keys + "&t=" + (new Date()).getTime();
    //alert(ajaxuri);
    //Ajax保存
    $.ajax(
            { type: "post",
                url: "/Member.do/SaveNewsContent",
                error: function() {
                    alert("保存失败");
                },
                data: ajaxuri,
                datatype: "html",
                timeout: 5000,
                success: function(msg) {
                    //alert(msg);
                    window.location.reload();

                }
            }); 
    //保存完成
    __cacnelEdit();
    __$(__rawID).innerHTML = editedhtml;
    //alert(escape(editedhtml)); 
    
}
//取消编辑
function __cacnelEdit() {
    __$("editor_div").appendChild(__$("hediv"));
    __$(__rawID).innerHTML = __rawhtml;
    __iseditnow = false;
}
//将某对象设为可编辑状态
function __SetEditable(__editableID, ks) { 
    if (!__uce) return; //是否允许编辑
    if (__iseditnow) {
        //alert("一个对象处理编辑状态！暂时不能执行此操作!");
        return;
    } 
    else {
        var areah = document.getElementById(__editableID).offsetHeight;
        if (areah < 30) areah = 30;
        __db_keys = ks;
        __rawhtml =__$(__editableID).innerHTML;
        __rawID = __editableID;
        __$("content_editor").innerHTML = "正在执行Ajax请求..";     //__rawhtml;
        $("#content_editor").load("/Member.do/GetNewsContent?p=" + ks + "&t="+(new Date()).getTime());
        __$(__editableID).innerHTML = "";
        __$(__editableID).appendChild(__$("hediv"));
        __$("hediv").style.height = ""+areah+"px";
        __iseditnow = true;
    }
    
    
}


    
