$(function () {
    //动态计算整个页面高度宽度
    $("#fileList").css("height", $("#divMenusMain", top.document).height() - 110);
    $("#code_Text").css("width", $("#divMenusMain", top.document).width() - 320);
    $("#code_Text").css("height", $("#divMenusMain", top.document).height() - 120);
    $("#templateIframe").css("height", $("#divMenusMain", top.document).height() - 110);
    $(window).resize(function () {
        $("#fileList").css("height", $("#divMenusMain", top.document).height() - 110);
        $("#code_Text").css("width", $("#divMenusMain", top.document).width() - 320);
        $("#code_Text").css("height", $("#divMenusMain", top.document).height() - 120);
        $("#templateIframe").css("height", $("#divMenusMain", top.document).height() - 110);
    });


    //左边鼠标移动
    $("#fileList a").hover(function () {
        $(this).parent().addClass("focus");
    }, function () {
        $(this).parent().removeClass("focus");
    })

    //计算文件数量
    $("#fileNumber").text($("#fileList span[class='file']").length);

    //左边点击
    $("#fileList a").click(function () {
        var obj = $(this);
        $("#fileList a").removeClass("active");
        obj.addClass("active");

        $("#loadBox").hide();
        $("#rightBox").show();

        $("#fileName").html("" + obj.attr("href") + " <span class=\"X NoB\">( 最后修改时间：" + obj.attr("addtime") + ")</span>");
        $("#savaTemplateFile").attr("file", obj.text());
        $("#savaTemplateFile").attr("path", obj.attr("href"));

        $.ajax({
            type: "POST",
            url: "Ajax/TemplateDispose.ashx",
            data: { com: 'gettemplatefile', filename: obj.attr("href") },
            datatype: "json",
            beforeSend: function (data, textStatus) {
                $("#code_Text").val("文件载入中..");
            },
            success: function (data, textStatus) {
                $("#code_Text").val(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); }
        })
        return false;
    })

    //保存模板
    $("#savaTemplateFile").click(function () {
        var obj = $(this);

        if (confirm("确定保存 [ " + obj.attr("file") + " ] 模板文件吗？")) {
            $.ajax({
                type: "POST",
                url: "Ajax/TemplateDispose.ashx",
                data: { com: 'settemplatefile', filename: obj.attr("path"), code: replaceSpecial($("#code_Text").html()) },
                datatype: "json",
                beforeSend: function (data, textStatus) {
                    obj.next("span").attr("class", "point ico-remind V").html("[ " + obj.attr("file") + " ] 文件保存中");
                },
                success: function (data, textStatus) {
                    if (data == "1") {
                        obj.next("span").attr("class", "point ico-via Q").html("[ " + obj.attr("file") + " ] 保存成功 ， 时间 <span class=R>" + new Date().toLocaleTimeString() + "</span>");
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); }
            })

        }
        return false;
    })

    function replaceSpecial(str) {
        var strTemp = str;
        strTemp = strTemp.replace(/\&amp/g, "&");
        strTemp = strTemp.replace(/\&lt;/g, "<");
        strTemp = strTemp.replace(/\&gt;/g, ">");
        strTemp = strTemp.replace(/\&apos/g, "\'");
        strTemp = strTemp.replace(/\&quot/g, "\"");
        return strTemp;
    }


    //导出备份
    $("#derivedBtn").click(function () {
        var obj = $(this);
        location.href = obj.attr("rel") + "?file=" + encodeURIComponent($("#savaTemplateFile").attr("file")) + "&path=" + encodeURIComponent($("#savaTemplateFile").attr("path"));
        return false;
    })

    //快捷代码窗口
    $("#keycodeBtn").click(function () {
        $("#ouBoxTemplate").show();
        return false;
    })

    //控制弹层
    $("#ouBoxTemplate a[class='close']").click(function () {
        $("#ouBoxTemplate").hide();
    })
});