Editor = {
    EditorMode: false,
    GetMenu: function() { return '#EditorMenu' },
    WzEditor: function() { return $('#message')[0]; }, // return window.frames[this.IframeID]; },
    codeHead: function() { return $('#iHead'); },
    codeBody: function() { return $('#txtCode'); },
    IsIEbrowser: function() { if ($.browser.msie) { return true; } else { return false; } },
    btnDesign: function() { return $('#HtmlEditor_Design'); },
    btnHtml: function() { return $('#HtmlEditor_Html'); },
    btnPreview: function() { return $('#HtmlEditor_View'); },
    pCode: function() { var strpCode = this.codeBody().attr('pCode'); return $('#' + strpCode, $(parent.document)); },
    Init: function() {
        var WzEditor;
        try {
            WzEditor = this.WzEditor().contentWindow;
            //$('iframe', parent.document).each(function() { alert($(this.contentWidow.document)); });

            //WzEditor = document.getElementById("message").contentWindow;
        } catch (e) {
            alert("您的浏览器不支持此插件！请使用IE访问！");
            //return;
        }
        this.codeBody().css('display', 'none');
        $(WzEditor).css('display', 'block');
        $('img[abbr]', this.GetMenu() + '>td').each(function(i) {
            $(this).css('cursor', 'pointer');
            $(this).css('border', '0px');
            $(this).bind('click', function() { _this.MenuClick(this); });
            $(this).bind('mouseover', function() { $(this).css('background-color', '#c6edff') });
            $(this).bind('mouseout', function() { $(this).css('background-color', '#fff') });
        });
        $(this.GetMenu() + '>td').each(function(i) { $(this).css('line-height', '25px'); $(this).css('text-align', 'center'); });
        this.btnDesign().bind('click', function() { _this.setMode(false); });
        this.btnHtml().bind('click', function() { _this.setMode(true); });
        this.btnPreview().bind('click', function() { _this.Preview(); });
        this.SetValue();
        var thisPage = this.codeBody().attr('value');

        WzEditor.document.open();
        WzEditor.document.clear();
        WzEditor.document.write('<html xmlns:ctl="http://www.w3.org/1999/xhtml"><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /><title>无标题 1</title><link href="/App_Themes/ManagerTheme/Field.css" type="text/css" rel="stylesheet" /></head>' + thisPage);
        //WzEditor.document.charset = "utf-8";
        WzEditor.document.close();

        if (this.IsIEbrowser) { WzEditor.document.body.contentEditable = "true"; } else { WzEditor.document.designMode = "on"; }
        $('#message').css('height', this.codeBody().css('height'));
        //颜色框
        var SelectColor = "<div style='position: absolute; background: #fff; left: 234px; top: 66px; display: none;' id='SelectColor'>";
        var k = 1;
        var c = ["", "#FF9999", "#FFFF80", "#80FF80", "#80FFFF", "#0080FF", "#FF80C0", "#FF0000", "#CCCC66", "#00FF40", "#0099CC", "#9999CC", "#FF00FF", "#CC3333", "#FF9933", "#009999", "#006699", "#9999FF", "#990033", "#990000", "#FF9900", "#009900", "#0000FF", "#0000CC", "#990099", "#660000", "#006666", "#999900", "#660099", "#339999", "#66CCCC", "#000000", "#494949", "#767676", "#A6A6A6", "#C7C7C7", "#FFFFFF"];
        for (var i = 0; i < 6; i++) {
            for (var j = 0; j < 6; j++) {
                SelectColor += ("<img src='/App_Themes/ManagerTheme/Images/Explorer/image/blank.gif' style='background: " + c[k] + ";border: 1px solid #999; margin: 0px; padding: 0px; margin-top: 1px; margin-left: 1px; width: 14px; height: 14px; cursor: hand; cursor: pointer;' value='" + c[k] + "' />");
                k++;
            }
            SelectColor += ('<br />');
        }
        $(SelectColor).appendTo("body");
        var FontInfo = {
            Family: [{ t: "宋体" }, { t: "黑体" }, { t: "隶书" }, { t: "楷体" }, { t: "幼圆" }, { t: "Arial" }, { t: "Impact" }, { t: "Georgia" }, { t: "Verdana" }, { t: "Courier New" }, { t: "Times New Roman"}],
            Size: [{ t: "1", n: "(一号字)" }, { t: "2", n: "(二号字)" }, { t: "3", n: "(三号字)" }, { t: "4", n: "(四号字)" }, { t: "5", n: "(五号字)" }, { t: "6", n: "(六号字)" }, { t: "7", n: "(七号字)"}]
        };
        var divFloat = new String;
        for (key in FontInfo) {
            for (var key2 in FontInfo[key]) {
                node = FontInfo[key][key2];
                if (key == "Family") {
                    key2 = parseInt(key2);
                    if (key2 == 0) {// head
                        divFloat += "<div style='position: absolute; background: #fff; left: 234px; top: 66px; display: none;' id='FontName'>";
                    }
                    if (key2 < FontInfo[key].length) {// body
                        divFloat += "<div style='text-align: center; background: #f4f4f4; border: 1px solid #ccc; margin: 1px; padding: 4px; width: 120px!important ; width: 129px; font-family: " + node["t"] + "' unselectable='on' action='family' value='" + node["t"] + "'>" + node["t"] + "</div>";
                    }
                    if (key2 + 1 == FontInfo[key].length) {// foot
                        divFloat += "</div>";
                    }
                }
                if (key == "Size") {
                    key2 = parseInt(key2);
                    if (key2 == 0) {// head
                        divFloat += "<div style='position: absolute; background: #fff; left: 234px; top: 66px; display: none;' id='FontSize'>";
                    }
                    if (key2 < FontInfo[key].length) {// body
                        divFloat += "<div style='text-align: center; background: #f4f4f4; border: 1px solid #ccc; margin: 1px; padding: 4px; width: 60px!important ; width: 69px; font-size: " + node["t"] + ";' unselectable='on' action='size' value='" + node["t"] + "'><span unselectable='on'  style='font-size:9pt'>" + node["n"] + "</span></div>";
                    }
                    if (key2 + 1 == FontInfo[key].length) {// foot
                        divFloat += "</div>";
                    }
                }
            }
        }
        $(divFloat).appendTo("body");
        $(">div[action='family']", '#FontName').each(function() {
            $(this).css('cursor', 'pointer');
            $(this).bind('click', function() { _this.runCMD('FontName', $(this).attr('value')) });
        });
        $(">div[action='size']", '#FontSize').each(function() {
            $(this).css('cursor', 'pointer');
            $(this).bind('click', function() { _this.runCMD('FontSize', $(this).attr('value')) });
        });
        /*绑定文档点击事件*/
        var _this = this;
        $(WzEditor.document).bind('click', function(event) { event = event || window.event; _this.vent(event); })
        //$(WzEditor.document.body).bind('paste', function(event) { event = event || window.event; _this.Onpaste(event); })
        $(document).bind('click', function(event) { event = event || window.event; _this.vent(event); })

        /*绑定页面按钮提交*/
        var btnName = $('#ButtonName').attr('value');
        if (btnName != null && btnName != "") {
            $('#' + btnName, $(parent.document)).bind('click', function() { _this.GetValue(); });
        }
        this.GetValue();
    },
    //编辑器模式
    setMode: function(ViewCode) {
        var WzEditor = this.WzEditor().contentWindow;
        this.EditorMode = ViewCode;
        if (!ViewCode) {
            var Regbody = new RegExp("(<body)+[^<>]*>[^\0]*(<\/body>)+");
            var strHTML = this.codeBody().attr('value');
            var I = strHTML.search(Regbody);
            if (I > 0) {
                this.codeHead().attr('value', strHTML.slice(0, I));
                this.codeBody().attr('value', strHTML.replace(this.codeHead().attr('value'), '').replace('</html>', ''));
            } else {
                this.codeHead().attr('value', '<html><head><title>未命名文档</title></head>');
                this.codeBody().attr('value', '<body><div>&nbsp;</div></body>');
            }
            //WzEditor.document.body.innerHTML = this.codeBody().attr('value');
            WzEditor.document.open();
            WzEditor.document.clear();
            WzEditor.document.write('<html xmlns:ctl="http://www.w3.org/1999/xhtml"><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /><title>无标题 1</title><link href="/App_Themes/ManagerTheme/Field.css" type="text/css" rel="stylesheet" /></head>' + this.codeBody().attr('value'));
            //WzEditor.document.charset = "utf-8";
            WzEditor.document.close();
            if (this.IsIEbrowser) { WzEditor.document.body.contentEditable = "true"; } else { WzEditor.document.designMode = "on"; }

            this.btnDesign().attr('class', 'HtmlEditor_On');
            this.btnHtml().attr('class', 'HtmlEditor_Off');
            this.codeBody().css('display', 'none');
            $('#message').css('display', 'block');
            $('#EditorMenu').css('display', 'block');
        } else {
            var headValue = this.codeHead().attr('value');
            var bodyValue = WzEditor.document.body.innerHTML;
            if (headValue == '' || headValue == null) {
                headValue = '<html xmlns:ctl="http://www.w3.org/1999/xhtml"><head><title>未命名文档</title></head>';

            }
            if (bodyValue == '' || bodyValue == null) {
                bodyValue = '<body><div>&nbsp;</div></body>';
            } else {
                bodyValue = '<body>' + WzEditor.document.body.innerHTML + '</body>';
            }
            this.codeHead().attr('value', headValue);
            this.codeBody().attr('value', headValue + bodyValue + '</html>');

            this.btnDesign().attr('class', 'HtmlEditor_Off');
            this.btnHtml().attr('class', 'HtmlEditor_On');
            this.codeBody().css('display', 'block');
            $('#message').css('display', 'none');
            $('#EditorMenu').css('display', 'none');
        }
        this.GetValue();
    },
    /*执行命令*/
    runCMD: function(CMD, Value) {
        var WzEditor = this.WzEditor().contentWindow;
        var vValue;
        if (Value != '') { vValue = Value; }
        WzEditor.focus();
        WzEditor.document.execCommand(CMD, false, vValue);
        WzEditor.focus();
    },

    /*菜单点击*/
    MenuClick: function(objIMG) {
        var Action = $(objIMG).attr('abbr');
        var value = $(objIMG).attr('char');
        switch (Action) {
            case 'Color':
                $('#SelectColor').css('top', this.getAbsoluteTop(objIMG));
                $('#SelectColor').css('left', this.getAbsoluteLeft(objIMG));
                $('#SelectColor').css('display', "block");
                break;
            case 'FontName':
                $('#FontName').css('top', this.getAbsoluteTop(objIMG));
                $('#FontName').css('left', this.getAbsoluteLeft(objIMG));
                $('#FontName').css('display', "block");
            case 'FontSize':
                $('#FontSize').css('top', this.getAbsoluteTop(objIMG));
                $('#FontSize').css('left', this.getAbsoluteLeft(objIMG));
                $('#FontSize').css('display', "block");
            case 'Window':
                switch (value) {
                    case 'TagList':
                        iAPI = $.dialog({
                            lock: false,
                            content: 'url:' + $('#TagList').attr('value'),
                            min: false,
                            max: false,
                            width: 700,
                            height: 450
                        });
                        this.WzEditor().contentWindow.focus();
                        break;
                }
                break;
            case 'File':
                iAPI = $.dialog({
                    lock: false,
                    min: false,
                    max: false,
                    content: 'url:' + $('#UploadPagePath').attr('value'),
                    width: 700,
                    height: 450
                });
                this.WzEditor().contentWindow.focus();
                break;
            default:
                this.runCMD(Action, value);
                break;
        }
    },
    Preview: function() {
        var preWin;
        this.GetValue();
        preWin = window.open('preview', '', 'left=0,top=0,width=550,height=400,resizable=1,scrollbars=1, status=1, toolbar=1, menubar=0');
        preWin.document.open();
        preWin.document.write(decodeURI(this.pCode().attr('value')));
        preWin.document.close();
        preWin.document.title = "预览";
        preWin.document.charset = 'utf-8';
    },
    getAbsoluteLeft: function(ob) {
        var x = ob.offsetLeft;
        var y = ob.offsetTop;
        while (ob = ob.offsetParent) { y += ob.offsetTop; x += ob.offsetLeft; }
        return x + "px";
    },
    getAbsoluteTop: function(ob) {
        var x = ob.offsetLeft;
        var y = ob.offsetTop;
        var h = ob.offsetHeight;
        while (ob = ob.offsetParent) { y += ob.offsetTop; x += ob.offsetLeft; }
        return (y + h) + "px";
    },
    vent: function(event) {
        var display = '';
        var eventObj;
        var objColor = $("img[abbr='Color']", this.GetMenu() + '>td');
        var objFontName = $("img[abbr='FontName']", this.GetMenu() + '>td');
        var objFontSize = $("img[abbr='FontSize']", this.GetMenu() + '>td');
        var divOptions;
        if (event == null) { return; }
        if (document.all) {
            eventObj = event.srcElement;
        } else {
            eventObj = event.target;
        }
        switch (eventObj) {
            case objFontName[0]:
                divOptions = '#FontName';
                break;
            case objFontSize[0]:
                divOptions = '#FontSize';
                break;
            default:
                var _this = this;
                for (var i = 0; i < objColor.length; i++) {
                    if (objColor[i] == eventObj) {
                        divOptions = '#SelectColor';
                        display = '';
                        $('#SelectColor >img').each(function(i) {
                            $(this).unbind('click');
                            $(this).bind('click', function() {
                                _this.runCMD($(eventObj).attr('char'), $(this).attr('value'));
                            });
                        });
                        break;
                    } else {
                        divOptions = null;
                        display = 'none';
                    }
                }
                break;
        }
        $('#SelectColor,#FontName,#FontSize').css('display', 'none');
        if (divOptions != null) { $(divOptions).css('display', display); }

    },
    SelectFileCallBack: function(FilePath, ExName) {
        var pasteBag = '';
        switch (ExName) {
            case 'mov':
                pasteBag = "<EMBED src='" + FilePath + "' width='200' height='200' type='audio/x-pn-realaudio-plugin' autostart='true' controls='IMAGEWINDOW,ControlPanel,StatusBar' console='Clip1'></EMBED>";
                break;
            case 'swf':
                pasteBag = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0' ><param name=movie value=" + FilePath + "><param name=quality value=high><embed src=" + FilePath + " pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash'></embed></object>";
                break;
            case 'img':
                pasteBag = '<img src="' + FilePath + '" border="0" />';
                break;
            case 'htm':
                this.ReadHTML(FilePath);
                break;
        }
        if (pasteBag != '') { this.PasteHTML(pasteBag, false); }
    },
    ReadHTML: function(strFileName) {
        var _this = this;
        $.ajax({
            type: "GET",
            url: '/GetHTMLFile.gdr?FilePath=' + strFileName,
            contentType: 'html',
            cache: false,
            success: function(responce) {
                var WzEditor = _this.WzEditor().contentWindow;
                var Regbody = new RegExp("(<body)+[^<>]*>[^\0]*(<\/body>)+");
                var I = responce.search(Regbody);
                if (I > 0) {
                    _this.codeHead().attr('value', responce.slice(0, I));
                    _this.codeBody().attr('value', responce.replace(_this.codeHead().attr('value'), '').replace('</html>', ''));
                } else {
                    _this.codeHead().attr('value', '<html xmlns:ctl="http://www.w3.org/1999/xhtml"><head><title>未命名文档</title></head>');
                    _this.codeBody().attr('value', '<body><div>&nbsp;</div></body>');
                }
                WzEditor.document.body.innerHTML = _this.codeBody().attr('value');

                _this.btnDesign().attr('class', 'HtmlEditor_On');
                _this.btnHtml().attr('class', 'HtmlEditor_Off');
                _this.codeBody().css('display', 'none');
                $('#message').css('display', 'block');
                $('#EditorMenu').css('display', 'block');
            }
        });
    },
    PasteHTML: function(strHTML, IsTxt) {
        if (this.IsIEbrowser) {
            this.WzEditor().contentWindow.focus();
            edit = this.WzEditor().document.selection.createRange();
            edit.pasteHTML(strHTML);
            edit.collapse(IsTxt);
            edit.select();

        } else {
            var WzEditor = this.WzEditor().contentWindow;
            WzEditor.document.execCommand('insertHTML', false, strHTML);
        }
    },
    /*初始化赋值*/
    SetValue: function() {
        var strCode = this.pCode().attr('value');
        if (strCode != '' && strCode != null) {
            var Regbody = new RegExp("(<body)+[^<>]*>[^\0]*(<\/body>)+");
            var I = strCode.search(Regbody);
            if (I > 0) {
                this.codeHead().attr('value', strCode.slice(0, I));
                this.codeBody().attr('value', strCode.replace(this.codeHead().attr('value'), '').replace('</html>', ''));
            } else {
                this.codeHead().attr('value', '<html xmlns:ctl="http://www.w3.org/1999/xhtml"><head><title>未命名文档</title></head>');
                this.codeBody().attr('value', '<body><div>&nbsp;</div></body>');
            }
        } else {
            this.codeBody().attr('value', '<html xmlns:ctl="http://www.w3.org/1999/xhtml"><head><title>未命名文档</title></head><body><div>&nbsp;</div></body></html>');
        }
    },
    /*获取内容*/
    GetValue: function() {
        var WzEditor = this.WzEditor().contentWindow;
        if (!this.EditorMode) {
            var headValue = this.codeHead().attr('value');
            var bodyValue = WzEditor.document.body.innerHTML;
            if (headValue == '' || headValue == null) {
                headValue = '<html xmlns:ctl="http://www.w3.org/1999/xhtml"><head><title>未命名文档</title></head>';
            }
            if (bodyValue == '' || bodyValue == null) {
                bodyValue = '<body><div>&nbsp;</div></body>';
            } else {
                bodyValue = '<body>' + WzEditor.document.body.innerHTML + '</body>';
            }
            this.pCode().attr('value', this.URLencode(headValue + bodyValue + '</html>'));
        } else {
            this.pCode().attr('value', this.URLencode(this.codeBody().attr('value')));
        }
    },
    URLencode: function(sStr) {
        var aStr = sStr.replace(/\+/g, '/&/&');
        return encodeURI(aStr);
    }
};
function callBack(filePath, ExName) {
    Editor.SelectFileCallBack(filePath,ExName);
}
function AscxcallBack(strAscx) {
    Editor.PasteHTML( strAscx,false);
}
$(document).ready(function() {
Editor.Init();
});