var petemplate = {
    modeid: 0,
    init: function () {
        //初始化 loading....
        var templateLoaddingdiv = document.createElement("div");
        templateLoaddingdiv.id = "templateLoaddingdiv";
        templateLoaddingdiv.style.position = "absolute";
        templateLoaddingdiv.style.top = "50%";
        templateLoaddingdiv.style.margin = "-30px 0px 0px -150px";
        templateLoaddingdiv.style.background = "url(../../Admin/JS/template/images/loading.gif) 80px 22px no-repeat #edf8fc";
        templateLoaddingdiv.style.zIndex = "65535";
        templateLoaddingdiv.style.left = "50%";
        templateLoaddingdiv.style.display = "block";
        templateLoaddingdiv.style.width = "300px";
        templateLoaddingdiv.style.height = "60px";
        templateLoaddingdiv.style.textAlign = "center";
        templateLoaddingdiv.style.verticalAlign = "middle";
        templateLoaddingdiv.style.lineHeight = "60px";
        templateLoaddingdiv.style.border = "2px solid #00a5e6";
        templateLoaddingdiv.style.borderRadius = "3px";
        templateLoaddingdiv.style.fontSize = "14px";
        templateLoaddingdiv.style.fontWeight = "bold";
        templateLoaddingdiv.innerHTML = "Loading...";
        document.documentElement.lastChild.appendChild(templateLoaddingdiv);

        if (!window.jQuery) {
            this.AjaxPage("jquerypackjs", "../../Admin/JS/template/jquery.pack.js", "js");
        }
        this.AjaxPage("jquerypeexpackjs", "../../Admin/JS/template/jquery.peex.js", "js");
        this.AjaxPage("Template", "../../Admin/JS/template/Template.css", "css");

        window.onload = function () {
            petemplate.DropInit();
            petemplate.editTemplateSelectInit();
            petemplate.labelinit();
            petemplateerrorno = 1;

            jQuery("#templateLoaddingdiv").remove();
            jQuery(document.documentElement.lastChild).append('<div id="PELabeldivoverlay" style="display:none"></div>');
            jQuery(document.documentElement.lastChild).append('<div style="display: none;" id="PELabelContentdivoverlay">' +
                '<div class="overlay" style="width:600px;height:400px">' +
                '<div class="overlay_header" style="cursor: move;">' +
                '<span class="overlay_header_title_sign"></span>' +
                '<h3 class="overlay_header_title">\u4EE3\u7801\u7F16\u8F91</h3>' +
                '<span class="overlay_header_background_right"></span>' +
                '<a href="javascript:void(0)" class="overlay_header_close"></a>' +
                '</div>' +
                '<div class="overlay_body">' +
                '<div id="labelcontentbody"><input type="hidden" id="labelid" /><input type="hidden" id="type" /><input type="hidden" id="gid" /><input type="hidden" id="containername" /><input type="hidden" id="labelindex" />' +
                '<textarea id="labelcontent"></textarea></div>' +
                '<div id="labelcontentbuttom"><input type="button" onclick="petemplate.savedate();" value="\u4FDD\u5B58" class="inputbutton">' +
                '<input type="button" onclick="petemplate.cancel();" value="\u53D6\u6D88" class="inputbutton">' +
                '</div></div>' +
                '</div>' +
                '</div>');

            if (! +"\v1" && !document.querySelector) { // for IE6 IE7 
                document.body.onresize = petemplate.resize;
            } else {
                window.onresize = petemplate.resize;
            }
        }
    },
    /**************标签编辑初始化*****************/
    labelinit: function () {
        switch (parseInt(petemplate.modeid)) {
            case 1:
                jQuery("pe[id^='PELabel_']").each(function (i) {
                    var id = jQuery(this).attr("id").replace(/PELabel_/, "");
                    if (jQuery(this).children().length == 0) {
                        if (jQuery(this).css("display") != "none") {
                            var obj = jQuery(this);
                            var offset = obj.offset();
                            var right = obj.width() - 2;
                            var down = obj.height();
                            if (right < 20) right = 20;
                            if (down < 10) down = 10;
                            jQuery(document.documentElement.lastChild).append('<div class="pelabelmask" id="pelabelmask_' + id + '_0" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                        }
                    } else {
                        jQuery(this).children().each(function (i) {
                            if (jQuery(this).css("display") != "none") {
                                var obj = jQuery(this);
                                var offset = obj.offset();
                                var right = obj.width() - 2;
                                var down = obj.height();
                                if (right < 20) right = 20;
                                if (down < 10) down = 10;
                                try {
                                    if (jQuery(this).get(0).nodeName.toLowerCase() != "script" && jQuery(this).get(0).nodeName.toLowerCase() != "style") {
                                        jQuery(document.documentElement.lastChild).append('<div class="pelabelmask" id="pelabelmask_' + id + '_' + i + '" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                                    }
                                } catch (err) {
                                }
                            }
                        });
                    }
                });
                break;
            case 2:
                jQuery("pe[id^='PELabel_']").each(function (i) {
                    var id = jQuery(this).attr("id").replace(/PELabel_/, "");
                    if (jQuery(this).children().length == 0) {
                        var obj = jQuery(this);
                        var offset = obj.offset();
                        var right = obj.width() - 2;
                        var down = obj.height();
                        if (right < 20) right = 20;
                        if (down < 10) down = 10;
                        jQuery(document.documentElement.lastChild).append('<div class="pelabelmask" id="pelabelmask_' + id + '_0" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                    } else {
                        jQuery(this).children().each(function (i) {
                            var obj = jQuery(this);
                            var offset = obj.offset();
                            var right = obj.width() - 2;
                            var down = obj.height();
                            if (right < 20) right = 20;
                            if (down < 10) down = 10;
                            try {
                                if (jQuery(this).get(0).nodeName.toLowerCase() != "script" && jQuery(this).get(0).nodeName.toLowerCase() != "style") {
                                    if (jQuery(this).get(0).nodeName.toLowerCase() != "embed") {
                                        jQuery(this).append('<div class="pelabelmask" id="pelabelmask_' + id + '_' + i + '" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                                    } else {
                                        jQuery(document.documentElement.lastChild).append('<div class="pelabelmask" id="pelabelmask_' + id + '_' + i + '" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                                    }
                                }
                            } catch (err) {
                                jQuery(document.documentElement.lastChild).append('<div class="pelabelmask" id="pelabelmask_' + id + '_' + i + '" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                            }
                        });
                    }
                });
                break;
            default:
                jQuery("pe[id^='PELabel_']").each(function (i) {
                    var id = jQuery(this).attr("id").replace(/PELabel_/, "");
                    if (jQuery(this).children().length == 0) {
                        if (jQuery(this).parent().length > 0 && jQuery(this).parent().get(0).nodeName.toLowerCase() == "ul") {
                            var obj = jQuery(this).parent();
                            var offset = obj.offset();
                            var right = obj.width() - 2;
                            var down = obj.height();
                            if (right < 20) right = 20;
                            if (down < 10) down = 10;
                            obj.append('<div class="pelabelmask" id="pelabelmask_' + id + '_0" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                        } else {
                            var obj = jQuery(this);
                            var offset = obj.offset();
                            var right = obj.width() - 2;
                            var down = obj.height();
                            if (right < 20) right = 20;
                            if (down < 10) down = 10;
                            jQuery(document.documentElement.lastChild).append('<div class="pelabelmask" id="pelabelmask_' + id + '_0" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                        }
                    } else {
                        if (jQuery(this).find("div").length > 0) {
                            jQuery(this).children().each(function (i) {
                                var obj = jQuery(this);
                                var offset = obj.offset();
                                var right = obj.width() - 2;
                                var down = obj.height();
                                if (right < 20) right = 20;
                                if (down < 10) down = 10;
                                try {
                                    if (jQuery(this).get(0).nodeName.toLowerCase() != "script" && jQuery(this).get(0).nodeName.toLowerCase() != "style") {
                                        if (jQuery(this).get(0).nodeName.toLowerCase() != "embed") {
                                            jQuery(this).append('<div class="pelabelmask" id="pelabelmask_' + id + '_' + i + '" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                                        } else {
                                            jQuery(document.documentElement.lastChild).append('<div class="pelabelmask" id="pelabelmask_' + id + '_' + i + '" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                                        }
                                    }
                                } catch (err) {
                                    jQuery(document.documentElement.lastChild).append('<div class="pelabelmask" id="pelabelmask_' + id + '_' + i + '" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                                }
                            });
                        } else {
                            if (jQuery(this).parent().length > 0 && jQuery(this).parent().get(0).nodeName.toLowerCase() == "ul") {
                                var obj = jQuery(this).parent();
                                var offset = obj.offset();
                                var right = obj.width() - 2;
                                var down = obj.height();
                                if (right < 20) right = 20;
                                if (down < 10) down = 10;
                                obj.append('<div class="pelabelmask" id="pelabelmask_' + id + '_0" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                            } else {
                                var obj = jQuery(this);
                                var offset = obj.offset();
                                var right = obj.width() - 2;
                                var down = obj.height();
                                if (right < 20) right = 20;
                                if (down < 10) down = 10;
                                jQuery(document.documentElement.lastChild).append('<div class="pelabelmask" id="pelabelmask_' + id + '_0" style="top:' + offset.top + 'px;left:' + offset.left + 'px;z-Index:' + jQuery(this).css("zIndex") + ';width:' + right + 'px;height:' + down + 'px;position: absolute;"></div>');
                            }
                        }
                    }
                });
                break;
        }

        jQuery(".pelabelmask").each(function () {
            jQuery(this)
                .mouseenter(function () {
                    if (!petemplate.StartDroping) {
                        var id = jQuery(this).attr("id").replace(/(pelabelmask_\d+_)\d+/, "$1");
                        jQuery("div[id^=" + id + "]").each(function () {
                            jQuery(this).addClass("pelabelmaskbackgroundover");
                        });
                        var lid = jQuery(this).attr("id").replace(/pelabelmask_(\d+)_\d+/, "$1");
                        var labeldata = jQuery("pe[id='PELabel_" + lid + "']").attr("labeldata");
                        labeldata = petemplate.UrlDecode(labeldata);
                        var labelName = jQuery("pe[id='PELabel_" + lid + "']").attr("labelName");
                        labelName = petemplate.UrlDecode(labelName);
                        var lableType = jQuery("pe[id='PELabel_" + lid + "']").attr("lableType");

                        jQuery(this).css("zIndex", "999");
                        jQuery(this).html('<div class="png pelabelmaskmouseenter" onmousedown="petemplate.StartDrop(this,event);"><div class="png pelabelmaskmouseentertitle">' +
                    labelName + '</div><div id="pelabelmaskmouseenteredit" class="png"><a href="#" class="pelabelmaskmouseentera1 png" onclick="petemplate.editPELabel(this,event);return false;" onmousedown="petemplate.editPELabel(this,event);return false;"><span class="png"></span>\u53C2\u6570\u8BBE\u5B9A</a>' +
                    '<a class="pelabelmaskmouseentera2 png" href="#" onclick="petemplate.editPELabel2(this,event);return false;" onmousedown="petemplate.editPELabel2(this,event);return false;"><span class="png"></span>\u4EE3\u7801\u7F16\u8F91</a>' +
                    '<a class="pelabelmaskmouseentera3 png" href="#" onclick="petemplate.delTemplateLabel(this,event);return false;" onmousedown="petemplate.delTemplateLabel(this,event);return false;"><span class="png"></span>\u5220\u9664</a>' +
                    '</div></div>');
                    }
                })
                .mouseleave(function () {
                    var id = jQuery(this).attr("id").replace(/(pelabelmask_\d+_)\d+/, "$1");
                    jQuery("div[id^=" + id + "]").each(function () {
                        jQuery(this).removeClass("pelabelmaskbackgroundover");
                    });
                    jQuery(this).css("zIndex", "auto");
                    jQuery(this).html('');
                });
        });
    },
    /*********窗口调整*********/
    resize: function () {
        jQuery(".pelabelmask").remove();
        petemplate.labelinit();
        petemplate.editTemplateSelectResize();
    },
    /**************拖动****************/
    StartDroping: false,
    DropcolumnsX: [],
    DropInit: function () {
        jQuery("div[id^='PE_GroupContainer_']").each(function () {
            var id = jQuery(this).attr("id").replace(/PE_GroupContainer_(\d+)/, "$1");
            var layout = jQuery(this).attr("layout");
            var objwidth = jQuery(this).width();
            jQuery(this).css("width", (jQuery(this).width() - 10) + "px");
            jQuery(this).addClass("PE_GroupContainer")
            .mouseenter(function () {
                if (!petemplate.StartDroping) {
                    if (jQuery(this).width() < 0) {
                        jQuery(this).css("width", "20px");
                    }
                    var obj = jQuery(this);
                    var offset = obj.offset();
                    var right = obj.width();
                    if (right < 20) right = 20;
                    var top = (offset.top - 32) < 32 ? 0 : (offset.top - 32);
                    jQuery(this).append('<div id="PE_Group_ContainerHeard" style="top:' + top + 'px;left:' + (offset.left + 6) + 'px;position: absolute;"><a href="#" onclick="petemplate.LayoutList(' + id + ',\'' + layout + '\');return false;">\u66F4\u6539\u5E03\u5C40</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="petemplate.ShowAddLabel(' + id + ');return false;">\u6DFB\u52A0\u6807\u7B7E</a></div>');
                }
            }).mouseleave(function () {
                //jQuery(this).css("width", objwidth + "px");
                jQuery(this).find("div[id='PE_Group_ContainerHeard']").remove();
            });

            jQuery(this).find("div[ContainerName]").each(function () {
                var objheight = jQuery(this).height();
                jQuery(this).addClass("PE_ContainerName");
                jQuery(this).mouseenter(function () {
                    //if (objheight < 1) {
                    //   jQuery(this).css("height", "20px");
                    //}
                    if (petemplate.StartDroping) {
                        jQuery(document).unbind('mousemove');
                        jQuery("#PELabelStartDropDivPlace").remove();
                        petemplate.DropcolumnsX = [];
                        jQuery(this).children().each(function () {
                            petemplate.DropcolumnsX.push(petemplate.findPosMidY(jQuery(this).get(0)));
                        });
                        jQuery(this).append(jQuery("#PELabelStartDropDiv"));
                    }
                }).mouseleave(function () {
                    //if (objheight < 1) {
                    //   jQuery(this).css("height", objheight + "px");
                    //}
                    if (petemplate.StartDroping) {
                        jQuery(document).bind('mousemove', function (e) {
                            if (petemplate.StartDroping) {
                                jQuery("#PELabelStartDropDivPlace").remove();
                                var pos = petemplate.GetmousePosition(e);
                                jQuery("#PELabelStartDropDiv").css("top", pos[1] + "px").css("left", pos[0] + "px");
                            }
                        });
                        jQuery("#PELabelStartDropDivPlace").remove();
                        jQuery(document.documentElement.lastChild).append(jQuery("#PELabelStartDropDiv"));
                    }
                }).mousemove(function (e) {
                    if (petemplate.StartDroping) {
                        var pos = petemplate.GetmousePosition(e);
                        jQuery("#PELabelStartDropDiv").css("top", pos[1] + "px").css("left", pos[0] + "px");
                        jQuery("#PELabelStartDropDivPlace").remove();
                        var columnIndex = -1;
                        for (var i = 0; i < petemplate.DropcolumnsX.length; i++) {
                            if (pos[1] > petemplate.DropcolumnsX[i]) {
                                columnIndex = i;
                            }
                        }
                        if (columnIndex >= 0) {
                            jQuery(this).children().eq(columnIndex).after('<div id="PELabelStartDropDivPlace"></div>');
                        } else {
                            jQuery(this).prepend('<div id="PELabelStartDropDivPlace"></div>');
                        }
                    }
                });
            });

        });
    },
    LayoutList: function (gid, name) {
        jQuery("#PELabeldivoverlay").overlay({ "width": 600, "api": true, "loadUrl": "LayoutList.aspx?layout=" + name + "&gid=" + gid, "title": "\u5E03\u5C40\u6A21\u677F\u5217\u8868", "render": true, "expose": true, "height": 400 }).load();
    },
    SetLayout: function (gid, name) {
        jQuery.post('Template_EditLabel.aspx?Ajax=true', "<?xml version='1.0' encoding='utf-8'?><root><type>setlayout</type><dir>" + petemplate.UrlEncode(templatedir) + "</dir><gid>" + gid + "</gid><name>" + petemplate.UrlEncode(name) + "</name></root>", function (s) {
            var status = jQuery("status", s).text();
            if (status == "err") {
                alert(jQuery("body", s).text());
            } else {
                window.location.reload();
            }
        });
    },
    ShowAddLabel: function (gid) {
        jQuery("#PELabeldivoverlay").overlay({ "width": 600, "api": true, "loadUrl": "Template_EditLabel.aspx?a=add&id=" + gid, "title": "\u6DFB\u52A0\u6807\u7B7E", "render": true, "expose": true, "height": 400 }).load();
    },
    AddLabelText: function (gid, labelTxt) {
        jQuery.post('Template_EditLabel.aspx?Ajax=true', "<?xml version='1.0' encoding='utf-8'?><root><type>add</type><dir>" + petemplate.UrlEncode(templatedir) + "</dir><id>" + gid + "</id><label>" + petemplate.UrlEncode(labelTxt) + "</label></root>", function (s) {
            var status = jQuery("status", s).text();
            if (status == "err") {
                alert(jQuery("body", s).text());
            } else {
                window.location.reload();
            }
        });
        PELabeldivoverlayCloseWindow();
    },
    StartDrop: function (obj, evt) {
        var e = (evt) ? evt : window.event;
        if (window.event) {
            e.cancelBubble = true;
        } else {
            e.stopPropagation();
        }
        jQuery(document.documentElement.lastChild).addClass("PELabeluser_select_none");
        petemplate.StartDroping = true;
        var id = jQuery(obj).parent().attr("id").replace(/pelabelmask_(\d+)_\d+/, "$1");
        obj = jQuery("pe[id='PELabel_" + id + "']");
        var labelName = obj.attr("labelName");
        labelName = petemplate.UrlDecode(labelName);

        var type = 0;
        var typeid = id;
        var labelindex = -1;
        var containername = '';
        //查找是否包含在容器内
        var parentobj = obj;
        while (parentobj.length > 0) {
            if (parentobj.attr("LabelIndex")) {
                type = 1;
                labelindex = parentobj.attr("LabelIndex");
                //containername = parentobj.attr("ContainerName");
                break;
            }
            parentobj = parentobj.parent();
        }
        if (type == 1) {
            var parentobj = obj;
            while (parentobj.length > 0) {
                if (parentobj.attr("PE_GroupContainer")) {
                    typeid = parentobj.attr("id").replace(/PE_GroupContainer_(\d+)/, "$1");
                    break;
                }
                parentobj = parentobj.parent();
            }
            parentobj = obj;
            while (parentobj.length > 0) {
                if (parentobj.attr("ContainerName")) {
                    containername = parentobj.attr("ContainerName");
                    break;
                }
                parentobj = parentobj.parent();
            }
        }


        jQuery(document.documentElement.lastChild).append('<div id="PELabelStartDropDiv" type="' + type + '" labelindex="' + labelindex + '" typeid="' + typeid + '" containername="' + containername + '">' + labelName + '</div>');
        var pos = petemplate.GetmousePosition(e);
        jQuery("#PELabelStartDropDiv").css("top", pos[1] + "px").css("left", pos[0] + "px");

        jQuery(document).mouseup(function () {
            if (petemplate.StartDroping) {
                petemplate.StartDroping = false;
                jQuery(document.documentElement.lastChild).removeClass("PELabeluser_select_none");
                petemplate.DropcolumnsX = [];
                if (jQuery("#PELabelStartDropDivPlace").length > 0) {
                    var dotype = parseInt(jQuery("#PELabelStartDropDiv").attr("type"));
                    var olabelindex = parseInt(jQuery("#PELabelStartDropDiv").attr("labelindex"));
                    var oparentid = parseInt(jQuery("#PELabelStartDropDiv").attr("typeid"));
                    var ocontainername = jQuery("#PELabelStartDropDiv").attr("containername");
                    jQuery("#PELabelStartDropDiv").remove();

                    var nlabelindex = -1;
                    var ncontainername = '';
                    if (jQuery("#PELabelStartDropDivPlace").prev().length > 0) {
                        nlabelindex = parseInt(jQuery("#PELabelStartDropDivPlace").prev().attr("LabelIndex")) + 1;
                    } else {
                        nlabelindex = 0;
                    }
                    ncontainername = jQuery("#PELabelStartDropDivPlace").parent().attr("ContainerName");

                    var nparentid = -1;
                    var parentobj = jQuery("#PELabelStartDropDivPlace");
                    while (parentobj.length > 0) {
                        if (parentobj.attr("PE_GroupContainer")) {
                            nparentid = parseInt(parentobj.attr("id").replace(/PE_GroupContainer_(\d+)/, "$1"));
                            break;
                        }
                        parentobj = parentobj.parent();
                    }

                    if (dotype == 1) {
                        //没移动，不变
                        if (oparentid == nparentid && ocontainername == ncontainername && (nlabelindex == olabelindex || nlabelindex == olabelindex + 1)) {
                            jQuery("#PELabelStartDropDivPlace").remove();
                            jQuery("#PELabelStartDropDiv").remove();
                            jQuery(document).unbind('mouseup');
                            return;
                        }
                    }
                    //alert('type:' + dotype + '|oparentid:' + oparentid + '|olabelindex:' + olabelindex + '|nparentid:' + nparentid + '|nlabelindex:' + nlabelindex + '|ncontainername:' + ncontainername + '|ocontainername:' + ocontainername);

                    jQuery.post('Template_EditLabel.aspx?Ajax=true', "<?xml version='1.0' encoding='utf-8'?><root><type>dropdiv</type><dir>" + petemplate.UrlEncode(templatedir) + "</dir><dotype>" + dotype + "</dotype><oparentid>" + oparentid + "</oparentid><olabelindex>" + olabelindex + "</olabelindex><nparentid>" + nparentid + "</nparentid><nlabelindex>" + nlabelindex + "</nlabelindex><ocontainername>" + petemplate.UrlEncode(ocontainername) + "</ocontainername><ncontainername>" + petemplate.UrlEncode(ncontainername) + "</ncontainername></root>", function (s) {
                        var status = jQuery("status", s).text();
                        if (status == "err") {
                            alert(jQuery("body", s).text());
                        } else {
                            window.location.reload();
                        }
                    });

                }
                jQuery("#PELabelStartDropDivPlace").remove();
                jQuery("#PELabelStartDropDiv").remove();
                jQuery(document).unbind('mouseup');
            }
        });

        jQuery(document).bind('mousemove', function (event) {
            if (petemplate.StartDroping) {
                var pos = petemplate.GetmousePosition(event);
                jQuery("#PELabelStartDropDiv").css("top", pos[1] + "px").css("left", pos[0] + "px");
            }
        });
    },
    /*************动态加载js css***************/
    GetHttpRequest: function () {
        if (window.XMLHttpRequest) // Gecko
            return new XMLHttpRequest();
        else if (window.ActiveXObject) // IE
            return new ActiveXObject("MsXml2.XmlHttp");
    },
    AjaxPage: function (sId, url, type) {
        var oXmlHttp = this.GetHttpRequest();
        oXmlHttp.onreadystatechange = function () {
            if (oXmlHttp.readyState == 4) {
                if (oXmlHttp.status == 200 || oXmlHttp.status == 304) {
                    petemplate.Include(sId, url, oXmlHttp.responseText, type);
                }
            }
        }
        oXmlHttp.open('GET', url, false);
        oXmlHttp.send(null);
    },
    Include: function (sId, fileUrl, source, type) {
        if ((source != null) && (!document.getElementById(sId))) {
            var includeobject;
            if (type == "js") {
                includeobject = document.createElement("script");
                includeobject.language = "javascript";
                includeobject.type = "text/javascript";
                includeobject.text = source;
                includeobject.defer = true;
                includeobject.id = sId;
            } else {
                //                includeobject = document.createElement("style");
                //                includeobject.type = "text/css";
                //                if (includeobject.styleSheet) { // IE
                //                    includeobject.styleSheet.cssText = source;
                //                } else {
                //                    includeobject.appendChild(document.createTextNode(source));
                //                }
                var s = document.getElementsByTagName('script');
                var path;
                for (var i = 0, n = s.length; i < n; i++) {
                    if (s[i].src.match('Template.js')) {
                        path = s[i].src.replace('Template.js', '');
                    }
                }
                includeobject = document.createElement('link');
                includeobject.href = path + sId + '.css';
                includeobject.rel = 'stylesheet';
                includeobject.type = 'text/css';
            }
            document.documentElement.lastChild.appendChild(includeobject);
        }
    },
    /*************动态加载js css End***************/
    /*************URL Encode***************/
    UrlEncode: function (inputStr) {
        var resultArr = [];
        var chars = "!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
        for (var i = 0; i < inputStr.length; i++) {
            var tmpChar = inputStr.charAt(i);
            var c = inputStr.charCodeAt(i);
            if (c > 0x7E) {
                resultArr[i] = encodeURI(tmpChar);
            } else {
                if (tmpChar == " ")
                    resultArr[i] = "+";
                else if (chars.indexOf(tmpChar) != -1)
                    resultArr[i] = "%" + c.toString(16);
                else
                    resultArr[i] = tmpChar;
            }
        }
        return resultArr.join("");
    },
    UrlDecode: function (inputStr) {
        var resultArr = [];
        for (var i = 0; i < inputStr.length; i++) {
            var chr = inputStr.charAt(i);
            if (chr == "+") {
                resultArr[resultArr.length] = " ";
            } else if (chr == "%") {
                var asc = inputStr.substring(i + 1, i + 3);
                if (parseInt("0x" + asc) > 0x7f) {
                    resultArr[resultArr.length] = decodeURI(inputStr.substring(i, i + 9));
                    i += 8;
                } else {
                    resultArr[resultArr.length] = String.fromCharCode(parseInt("0x" + asc));
                    i += 2;
                }
            } else {
                resultArr[resultArr.length] = chr;
            }
        }
        return resultArr.join("");
    },
    /*************URL Encode***************/
    GetLength: function (str) {
        var realLength = 0, len = str.length, charCode = -1;
        for (var i = 0; i < len; i++) {
            charCode = str.charCodeAt(i);
            if (charCode >= 0 && charCode <= 128) realLength += 1;
            else realLength += 2;
        }
        return realLength;
    },
    /*************编辑标签***************/
    editPELabel: function (obj, evt) {
        var e = (evt) ? evt : window.event;
        if (window.event) {
            e.cancelBubble = true;
        } else {
            e.stopPropagation();
        }
        var id = jQuery(obj).parent().parent().parent().attr("id").replace(/pelabelmask_(\d+)_\d+/, "$1");
        var label = jQuery("pe[id='PELabel_" + id + "']");
        var parentobj = label;
        var type = 0;
        var labelindex = -1;
        var containername = '';
        var gid = -1;
        while (parentobj.length > 0) {
            if (parentobj.attr("LabelIndex")) {
                labelindex = parentobj.attr("LabelIndex");
            }
            if (parentobj.attr("ContainerName")) {
                containername = parentobj.attr("ContainerName");
            }
            if (parentobj.attr("PE_GroupContainer")) {
                type = 1;
                gid = parentobj.attr("id").replace(/PE_GroupContainer_(\d+)/, "$1");
                break;
            }
            parentobj = parentobj.parent();
        }
        var url = '';
        if (type == 1) {
            url = "Template_EditLabel.aspx?dir=" + templatedir + "&type=1&labelindex=" + labelindex + "&gid=" + gid + "&containername=" + petemplate.UrlEncode(containername);
        } else {
            url = "Template_EditLabel.aspx?dir=" + petemplate.UrlEncode(templatedir) + "&type=0&id=" + id;
        }

        jQuery("#PELabeldivoverlay").overlay({ "width": 600, "api": true, "loadUrl": url, "title": "\u53C2\u6570\u8BBE\u5B9A", "render": true, "expose": true, "height": 400 }).load();
    },
    /*************编辑标签方式2***************/
    editPELabel2: function (obj, evt) {
        var e = (evt) ? evt : window.event;
        if (window.event) {
            e.cancelBubble = true;
        } else {
            e.stopPropagation();
        }
        var lid = jQuery(obj).parent().parent().parent().attr("id").replace(/pelabelmask_(\d+)_\d+/, "$1");

        var label = jQuery("pe[id='PELabel_" + lid + "']");
        var parentobj = label;
        var type = 0;
        var labelindex = -1;
        var containername = '';
        var gid = -1;
        while (parentobj.length > 0) {
            if (parentobj.attr("LabelIndex")) {
                labelindex = parentobj.attr("LabelIndex");
            }
            if (parentobj.attr("ContainerName")) {
                containername = parentobj.attr("ContainerName");
            }
            if (parentobj.attr("PE_GroupContainer")) {
                type = 1;
                gid = parentobj.attr("id").replace(/PE_GroupContainer_(\d+)/, "$1");
                break;
            }
            parentobj = parentobj.parent();
        }

        var labeldata = jQuery("pe[id='PELabel_" + lid + "']").attr("labeldata");
        labeldata = petemplate.UrlDecode(labeldata);
        var labelName = jQuery("pe[id='PELabel_" + lid + "']").attr("labelName");
        labelName = petemplate.UrlDecode(labelName);
        jQuery("#PELabelContentdivoverlay").find("#type").val(type);
        if (type == 1) {
            jQuery("#PELabelContentdivoverlay").find("#gid").val(gid);
            jQuery("#PELabelContentdivoverlay").find("#containername").val(containername);
            jQuery("#PELabelContentdivoverlay").find("#labelindex").val(labelindex);
        } else {
            jQuery("#PELabelContentdivoverlay").find("#labelid").val(lid);
        }
        jQuery("#PELabelContentdivoverlay").find("#labelcontent").val(labeldata);
        jQuery("#PELabelContentdivoverlay").overlay({ "api": true, "expose": true }).load();
    },
    savedate: function () {
        var type = jQuery("#PELabelContentdivoverlay").find("#type").val();
        if (type == 1) {
            var labeldata = jQuery("#PELabelContentdivoverlay").find("#labelcontent").val();
            var gid = jQuery("#PELabelContentdivoverlay").find("#gid").val();
            var containername = jQuery("#PELabelContentdivoverlay").find("#containername").val();
            var labelindex = jQuery("#PELabelContentdivoverlay").find("#labelindex").val();
            var labeldata = jQuery("#PELabelContentdivoverlay").find("#labelcontent").val();
            jQuery.post('Template_EditLabel.aspx?Ajax=true', "<?xml version='1.0' encoding='utf-8'?><root><type>editglabel</type><dir>" + petemplate.UrlEncode(templatedir) + "</dir><gid>" + gid + "</gid><containername>" + petemplate.UrlEncode(containername) + "</containername><labelindex>" + labelindex + "</labelindex><label>" + petemplate.UrlEncode(labeldata) + "</label></root>", function (s) {
                var status = jQuery("status", s).text();
                if (status == "err") {
                    alert(jQuery("body", s).text());
                } else {
                    window.location.reload();
                }
            });

        } else {
            var lid = jQuery("#PELabelContentdivoverlay").find("#labelid").val();
            var labeldata = jQuery("#PELabelContentdivoverlay").find("#labelcontent").val();
            jQuery.post('Template_EditLabel.aspx?Ajax=true', "<?xml version='1.0' encoding='utf-8'?><root><type>edit</type><dir>" + petemplate.UrlEncode(templatedir) + "</dir><id>" + lid + "</id><label>" + petemplate.UrlEncode(labeldata) + "</label></root>", function (s) {
                var status = jQuery("status", s).text();
                if (status == "err") {
                    alert(jQuery("body", s).text());
                } else {
                    window.location.reload();
                }
            });
        }
        jQuery("#PELabelContentdivoverlay").find(".overlay_header_close").click();
    },
    cancel: function () {
        jQuery("#PELabelContentdivoverlay").find(".overlay_header_close").click();
    },
    PELabeldivoverlayCloseWindow: function () {
        jQuery("#PELabeldivoverlay").find(".overlay_header_close").click();
    },
    editLabelText: function (id, txt) {
        jQuery.post('Template_EditLabel.aspx?Ajax=true', "<?xml version='1.0' encoding='utf-8'?><root><type>edit</type><dir>" + petemplate.UrlEncode(templatedir) + "</dir><id>" + id + "</id><label>" + petemplate.UrlEncode(txt) + "</label></root>", function (s) {
            var status = jQuery("status", s).text();
            if (status == "err") {
                alert(jQuery("body", s).text());
            } else {
                window.location.reload();
            }
        });
        PELabeldivoverlayCloseWindow();
    },
    editGLabelText: function (gid, containername, labelindex, txt) {
        jQuery.post('Template_EditLabel.aspx?Ajax=true', "<?xml version='1.0' encoding='utf-8'?><root><type>editglabel</type><dir>" + petemplate.UrlEncode(templatedir) + "</dir><gid>" + gid + "</gid><containername>" + petemplate.UrlEncode(containername) + "</containername><labelindex>" + labelindex + "</labelindex><label>" + petemplate.UrlEncode(txt) + "</label></root>", function (s) {
            var status = jQuery("status", s).text();
            if (status == "err") {
                alert(jQuery("body", s).text());
            } else {
                window.location.reload();
            }
        });
        PELabeldivoverlayCloseWindow();
    },
    /*************删除标签***************/
    delTemplateLabel: function (obj, evt) {
        var e = (evt) ? evt : window.event;
        if (window.event) {
            e.cancelBubble = true;
        } else {
            e.stopPropagation();
        }
        var id = jQuery(obj).parent().parent().parent().attr("id").replace(/pelabelmask_(\d+)_\d+/, "$1");
        var label = jQuery("pe[id='PELabel_" + id + "']");
        var parentobj = label;
        var type = 0;
        var labelindex = -1;
        var containername = '';
        var gid = -1;
        while (parentobj.length > 0) {
            if (parentobj.attr("LabelIndex")) {
                labelindex = parentobj.attr("LabelIndex");
            }
            if (parentobj.attr("ContainerName")) {
                containername = parentobj.attr("ContainerName");
            }
            if (parentobj.attr("PE_GroupContainer")) {
                type = 1;
                gid = parentobj.attr("id").replace(/PE_GroupContainer_(\d+)/, "$1");
                break;
            }
            parentobj = parentobj.parent();
        }

        if (confirm("\u786E\u8BA4\u8981\u5220\u9664\u4E48\uFF1F")) {

            if (type == 1) {
                jQuery.post('Template_EditLabel.aspx?Ajax=true', "<?xml version='1.0' encoding='utf-8'?><root><type>delglabel</type><dir>" + petemplate.UrlEncode(templatedir) + "</dir><gid>" + gid + "</gid><containername>" + petemplate.UrlEncode(containername) + "</containername><labelindex>" + labelindex + "</labelindex></root>", function (s) {
                    var status = jQuery("status", s).text();
                    if (status == "err") {
                        alert(jQuery("body", s).text());
                    } else {
                        window.location.reload();
                    }
                });

            } else {
                jQuery.post('Template_EditLabel.aspx?Ajax=true', "<?xml version='1.0' encoding='utf-8'?><root><type>del</type><dir>" + petemplate.UrlEncode(templatedir) + "</dir><id>" + id + "</id></root>", function (s) {
                    var status = jQuery("status", s).text();
                    if (status == "err") {
                        alert(jQuery("body", s).text());
                    } else {
                        window.location.reload();
                    }
                });
            }
        }
    },
    /*************工具方法***************/
    getMaskWidthAndHeight: function () {
        if (jQuery.browser.msie) {
            var d = jQuery(document).height(), w = jQuery(window).height();
            return [window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth, d - w < 20 ? w : d]
        }
        return [jQuery(document).width(), jQuery(document).height()];
    },
    /*************编辑内容变量***************/
    startpoint: [],
    endpoint: [],
    startdrawingpoint: [],
    startdrawing: false,
    petemplatecontenteditor: null,
    editTemplateSelect: function (flag) {
        if (flag) {
            jQuery("#editPELabeldivoverlay").show();
        } else {
            if (jQuery('#editPELabeldivoverlay').is(':hidden')) {
                jQuery("#editPELabeldivoverlay").show();
            } else {
                jQuery("#editPELabeldivoverlay").hide();
                jQuery("#editPELabeldivoverlayTips").hide();
            }
        }
    },
    /*************编辑内容Resize***************/
    editTemplateSelectResize: function () {
        var pos = this.getMaskWidthAndHeight();
        jQuery("#editPELabeldivoverlay").css("width", pos[0] + "px").css("height", pos[1] + "px");
    },
    /*************编辑内容初始化***************/
    editTemplateSelectInit: function () {
        var pos = this.getMaskWidthAndHeight();
        //全局底面
        jQuery(document.documentElement.lastChild).append('<div id="editPELabeldivoverlay" style="position: absolute; top: 0px; left: 0px; width: ' + pos[0] + 'px; height: ' + pos[1] + 'px; display: none;z-index: 10000;"></div>');
        //选择内容
        jQuery(document.documentElement.lastChild).append('<div id="editPELabeldivoverlaydrawing" style="position: absolute;display: none;z-index: 9999;"></div>');
        //提示内容
        jQuery(document.documentElement.lastChild).append('<div id="editPELabeldivoverlayTips" style="position: absolute;display: none;z-index: 9999;">请框选要编辑的内容</div>');
        //禁止拖动
        document.getElementById("editPELabeldivoverlay").ondrag = function () { return false; };
        document.getElementById("editPELabeldivoverlay").ondragstart = function () { return false; };

        if (self.frameElement && self.frameElement.tagName == "IFRAME") {
            //空
        } else {
            //高亮组件
            this.AjaxPage("codemirrorjs", "../../Admin/JS/template/CodeMirror/codemirror.js", "js");
            this.AjaxPage("codemirrorxmljs", "../../Admin/JS/template/CodeMirror/xml.js", "js");
            this.AjaxPage("codemirrorcssjs", "../../Admin/JS/template/CodeMirror/css.js", "js");
            this.AjaxPage("codemirrorjavascriptjs", "../../Admin/JS/template/CodeMirror/javascript.js", "js");
            this.AjaxPage("codemirrorhtmlmixedjs", "../../Admin/JS/template/CodeMirror/htmlmixed.js", "js");
            this.AjaxPage("CodeMirror/codemirror", "../../Admin/JS/template/CodeMirror/codemirror.css", "css");
            this.AjaxPage("CodeMirror/default", "../../Admin/JS/template/CodeMirror/default.css", "css");
            //弹出层编辑
            jQuery(document.documentElement.lastChild).append('<div style="display: none;" id="PETemplateContentdivoverlay">' +
                '<div class="overlay" style="width:600px;height:400px">' +
                '<div class="overlay_header" style="cursor: move;">' +
                '<span class="overlay_header_title_sign"></span>' +
                '<h3 class="overlay_header_title">\u7F16\u8F91\u6A21\u677F</h3>' +
                '<span class="overlay_header_background_right"></span>' +
                '<a href="javascript:void(0)" class="overlay_header_close"></a>' +
                '</div>' +
                '<div class="overlay_body">' +
                '<div id="contentbody"><input type="hidden" id="sid" />' +
                '<input type="hidden" id="eid" />' +
                '<textarea id="petemplatecontent"></textarea>' +
                '</div><div id="contentbuttom"><input type="button" onclick="petemplate.SaveTemplatedate(event);" value="\u4FDD\u5B58" class="inputbutton">' +
                '<input type="button" onclick="petemplate.Templatecancel(event);" value="\u53D6\u6D88" class="inputbutton">' +
                '</div></div>' +
                '</div>' +
                '</div>');
            //高亮组件
            petemplatecontenteditor = CodeMirror.fromTextArea(document.getElementById("petemplatecontent"), { mode: "text/html", tabMode: "indent", lineNumbers: true });
        }
        //鼠标事件
        jQuery(document).mousedown(function (e) {
            if (!jQuery('#editPELabeldivoverlay').is(':hidden')) {
                var e = (e) ? e : window.event;
                if (e.button == 2) {
                    jQuery("#editPELabeldivoverlay").hide();
                    jQuery("#editPELabeldivoverlayTips").hide();
                    jQuery(document).bind("contextmenu", function (e) {
                        return false;
                    });
                } else {
                    petemplate.startdrawing = true;
                    petemplate.startdrawingpoint = petemplate.GetmousePosition(e);
                    petemplate.startpoint = [e.clientX, e.clientY];
                }
            } else {
                jQuery(document).unbind("contextmenu", "");
            }
        });
        jQuery("#editPELabeldivoverlay").mousemove(function (e) {
            var point = petemplate.GetmousePosition(e);
            if (petemplate.startdrawing) {
                jQuery("#editPELabeldivoverlaydrawing").css("top", petemplate.startdrawingpoint[1] + "px")
        .css("left", petemplate.startdrawingpoint[0] + "px")
        .css("width", (point[0] - petemplate.startdrawingpoint[0]) + "px")
        .css("height", (point[1] - petemplate.startdrawingpoint[1]) + "px")
        .show();
            }
            jQuery("#editPELabeldivoverlayTips").css("top", (point[1] + 10) + "px")
              .css("left", (point[0] + 10) + "px").show();
        }).mouseup(function (e) {
            if (petemplate.startdrawing) {
                petemplate.startdrawing = false;
                petemplate.endpoint = [e.clientX, e.clientY];
                jQuery("#editPELabeldivoverlaydrawing").hide();
                jQuery("#editPELabeldivoverlay").hide();
                jQuery("#editPELabeldivoverlayTips").hide();
                jQuery(".pelabelmask").hide();
                setTimeout("petemplate.ShowEditorContent()", 500);  //IE8延时500毫秒
            }
        });

    },
    ShowEditorContent: function () {
        var startTarget = document.elementFromPoint(petemplate.startpoint[0], petemplate.startpoint[1]);
        var endTarget = document.elementFromPoint(petemplate.endpoint[0], petemplate.endpoint[1]);
        jQuery(".pelabelmask").show();
        if (startTarget != null && endTarget != null) {
            var sparent = petemplate.GetParenthtmllabelindex(jQuery(startTarget));
            var eparent = petemplate.GetParenthtmllabelindex(jQuery(endTarget));
            var sindex = sparent;
            var eindex = eparent;
            var findflag = false;
            var ssparent = sparent;
            while (!findflag) {
                var tempeparent = eparent;
                while (true) {
                    if (ssparent.attr("htmllabelindex") == tempeparent.attr("htmllabelindex")) {
                        findflag = true;
                        break;
                    } else {
                        if (tempeparent.get(0).nodeName == "BODY") {
                            break;
                        }
                        eindex = tempeparent;
                        tempeparent = petemplate.GetParenthtmllabelindex(tempeparent);
                    }
                }
                if (!findflag) {
                    sindex = ssparent;
                    eindex = eparent;
                    ssparent = petemplate.GetParenthtmllabelindex(ssparent);
                }
            }
            var p = ssparent.attr("htmllabelindex");
            var sid = sindex.attr("htmllabelindex");
            var eid = eindex.attr("htmllabelindex");

            if (sid == p && eid == p) {
                if (p != jQuery("body").attr("htmllabelindex")) {
                    eid = petemplate.GetNextHtmlLabelIndex(eindex).attr("htmllabelindex");
                }
            } else if (sid == p) {
                sid = eid;
                eid = petemplate.GetNextHtmlLabelIndex(eindex).attr("htmllabelindex");
            } else if (eid == p) {
                eid = petemplate.GetNextHtmlLabelIndex(sindex).attr("htmllabelindex");
            } else if (sid > eid) {
                var tempid = sid;
                sid = eid;
                eid = tempid;
            }
            if (self.frameElement && self.frameElement.tagName == "IFRAME") {
                parent.ShowEditorContent(templatedir, sid, eid);
            } else {
                jQuery.post('Template_EditLabel.aspx?Ajax=true', "<?xml version='1.0' encoding='utf-8'?><root><type>geteditcontent</type><dir>" + petemplate.UrlEncode(templatedir) + "</dir><sid>" + sid + "</sid><eid>" + eid + "</eid></root>", function (s) {
                    var status = jQuery("status", s).text();
                    if (status == "ok") {
                        jQuery("#PETemplateContentdivoverlay").find("#sid").val(sid);
                        jQuery("#PETemplateContentdivoverlay").find("#eid").val(eid);
                        jQuery("#PETemplateContentdivoverlay").find("#petemplatecontent").val(jQuery("body", s).text());
                        petemplatecontenteditor.setValue(jQuery("body", s).text());
                        jQuery("#PETemplateContentdivoverlay").overlay({ "api": true, "expose": true }).load();
                    } else {
                        alert(jQuery("body", s).text());
                    }
                });
            }
        }
    },
    /*************查找最近标记***************/
    GetNextHtmlLabelIndex: function (obj) {
        var parent = obj;
        if (parent.get(0).nodeName == "BODY") {
            return parent;
        }
        if (parent.next().length > 0) {
            parent = parent.next();
            if (parent.attr("htmllabelindex")) {
                return parent;
            }
            if (parent.find("[htmllabelindex]").length > 0) {
                return parent.find("[htmllabelindex]").get(0);
            }
            while (true) {
                if (parent.get(0).nodeName == "BODY") {
                    break;
                }
                if (parent.attr("htmllabelindex")) {
                    break;
                }
                if (parent.find("[htmllabelindex]").length > 0) {
                    parent = parent.find("[htmllabelindex]").get(0);
                    break;
                }
                parent = parent.parent();
            }
        } else {
            return this.GetNextHtmlLabelIndex(parent.parent());
        }
        return parent;
    },
    /*************查找父标记***************/
    GetParenthtmllabelindex: function (obj) {
        var parent = obj;
        if (parent.get(0).nodeName == "BODY") {
            return parent;
        }
        while (parent = parent.parent()) {
            if (parent.get(0).nodeName == "BODY") {
                break;
            }
            if (parent.attr("htmllabelindex")) {
                break;
            }
        }
        return parent;
    },
    /*************鼠标位置***************/
    GetmousePosition: function (ev) {
        if (ev.pageX || ev.pageY) {
            return [ev.pageX, ev.pageY];
        }
        return [
        ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        ev.clientY + document.body.scrollTop - document.body.clientTop
    ];
    },
    /*************查找元素中间点与浏览器上边的边距***************/
    findPosMidY: function (obj) {
        var h = jQuery(obj).height();
        if (h == 0) {//有些取不是高度，很怪异，特殊处理。
            h = 20;
        }
        var curtop = 0;
        if (obj && obj.offsetParent) {
            while (obj.offsetParent) {
                curtop += obj.offsetTop;
                obj = obj.offsetParent;
            }
        } else if (obj && obj.y) curtop += obj.y;
        return curtop + (h / 2);
    },
    /*************取消模板编辑***************/
    Templatecancel: function (evt) {
        var e = (evt) ? evt : window.event;
        if (window.event) {
            e.cancelBubble = true;
        } else {
            e.stopPropagation();
        }
        jQuery("#PETemplateContentdivoverlay").find(".overlay_header_close").click();
        jQuery("#editPELabeldivoverlay").show();
        petemplate.startdrawing = false;
    },
    /*************保存模板编辑***************/
    SaveTemplatedate: function (evt) {
        var e = (evt) ? evt : window.event;
        if (window.event) {
            e.cancelBubble = true;
        } else {
            e.stopPropagation();
        }
        var sid = jQuery("#PETemplateContentdivoverlay").find("#sid").val();
        var eid = jQuery("#PETemplateContentdivoverlay").find("#eid").val();
        //var content = jQuery("#PETemplateContentdivoverlay").find("#petemplatecontent").val();
        var content = petemplatecontenteditor.getValue();
        jQuery.post('Template_EditLabel.aspx?Ajax=true', "<?xml version='1.0' encoding='utf-8'?><root><type>editcontent</type><dir>" + petemplate.UrlEncode(templatedir) + "</dir><sid>" + sid + "</sid><eid>" + eid + "</eid><content>" + petemplate.UrlEncode(content) + "</content></root>", function (s) {
            var status = jQuery("status", s).text();
            if (status == "err") {
                alert(jQuery("body", s).text());
            } else {
                window.location.reload();
            }
        });
        jQuery("#PETemplateContentdivoverlay").find(".overlay_header_close").click();
        petemplate.startdrawing = false;
    }
}
var petemplateerrorno = 0;

window.onerror = function () {
    if (petemplateerrorno == 0) {
        jQuery("#templateLoaddingdiv").remove();
        jQuery(".pelabelmask").remove();
        petemplate.init();
        petemplateerrorno = 1;
    }
};
petemplate.init();

