//页面加载完成时
$(function () {
    //检测IE
    if ('undefined' == typeof (document.body.style.maxHeight)) {
        window.location.href = "/XymAdmin/Error/ie6update/";
    }
    $("#nav li").click(function () {
        $("#nav li").removeClass("selected");
        $(this).addClass("selected");
        var i = $(this).attr("id").replace("nav-item", "");
        $(".list-group").hide();
        $("#list-group" + i).show();
        menuCookieWrite();
    });
    $(".logo").click(function () {
        location.href = "/XymAdmin/admin/Index/";
    });
    $(".list-group .pack").click(function () {
        var tag = $(this);
        //tag.parent().siblings().find("ul").slideUp();
        tag.next().slideToggle(function () {
            var isH = tag.next().css("display");

            if (isH == "none") {
                tag.removeClass("mu");

                tag.find(".expandable").removeClass("open")
                tag.find(".expandable").addClass("close");
            }
            else {
                tag.addClass("mu");

                tag.find(".expandable").removeClass("close")
                tag.find(".expandable").addClass("open");
            }
            menuCookieWrite();
        });
    });
    //读取网站信息
    $.ajax({
        type: "post",
        url: "/XymAdmin/GetJson/GetSiteConfig/",
        dataType: "json",
        success: function (data) {
            if (data != null) {
                //遍历菜单显示状态
                $("#logowebname").text(data.webname);
            } else {
                //jsprint('读取网站配置信息出错', "", "Error", function () { });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //jsprint('读取配置信息出错，错误信息：' + errorThrown, "", "Error", function () { });
        }
    });
    //查询按钮事件
    $("#lbtnSearch").click(function () {
        var cont = $("#txtKeywords").val();
        var url = location.href.split('keywords')[0];

        if (cont != "") {
            $.ajax({
                type: "post",
                url: "/XymAdmin/Base/ReturnSearchUrl/",
                data: { "url": url, "cont": cont },
                success: function (data) {

                    if (data != "no") {
                        location.href = data;
                    } else {
                        jsprint('查询操作出错，请尝试刷新页面后操作！', "", "Error", function () { });
                        return false;
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //jsprint('读取配置信息出错，错误信息：' + errorThrown, "", "Error", function () { });
                    return false;
                }
            });
        } else {
            jsprint('请输入查询内容！', "", "Error", function () { });
            return false;
        }
    });
    //查询输入框回车事件
    $("#txtKeywords").keydown(function () {
        if (event.keyCode == "13") {//keyCode=13是回车键
            $('#lbtnSearch').click();
        }
    });
    GetSearchCont();

    menuCookie();
});

/*
显示查询内容
*/
function GetSearchCont() {
    $.ajax({
        type: "post",
        url: "/XymAdmin/Base/GetSearchCont/",
        success: function (data) {
            if (data != "no") {
                $("#txtKeywords").val(data);
            }
            var tip = $("#txtKeywords").attr("tip");
            $("#txtKeywords").attr("placeholder", tip);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //jsprint('读取配置信息出错，错误信息：' + errorThrown, "", "Error", function () { });
            return false;
        }
    });
}
/*
读取菜单的Cookie
*/
function menuCookie() {
    $.ajax({
        type: "post",
        url: "/XymAdmin/GetJson/GetMenuCookie/",
        data: {},
        success: function (data) {
            if (data == "") {
                //本地没有保存Cookie,需重新写入
                menuCookieWrite();
            } else {
                //遍历菜单显示状态
                menuCookieShow(data);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            jsprint('删除失败，错误信息：' + errorThrown, "", "Error", function () { });
        }
    });
}

function menuCookieWrite() {
    var list = "";
    list = list + $("#nav .selected").attr("id") + ",";

    $(".mu").each(function () {
        list = list + $(this).attr("navid") + ",";
    });
    if (list != "")
        list = list.substring(0, list.length - 1);

    $.ajax({
        type: "post",
        url: "/XymAdmin/GetJson/WriteMenuCookie/",
        data: { menu: list },
        success: function (data) {
            if (data == "yes") {
                //遍历菜单显示状态
                //menuCookieShow(list);
            } else {
                //jsprint('Cookie写入失败', "", "Error", function () { });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //jsprint('Cookie写入失败，错误信息：' + errorThrown, "", "Error", function () { });
        }
    });
}
function menuCookieShow(str) {
    $(".pack").find("expandable").removeClass("open")
    $(".pack").find("expandable").removeClass("close");
    //$(".NavItem").prev().find("folder").removeClass("open")
    //$(".NavItem").prev().find("folder").removeClass("close");

    $(".NavItem").hide();
    $(".pack").find("expandable").addClass("close");
    //$(".NavItem").prev().find("folder").addClass("open");
    if (str != "") {

        var arr = str.split(',');
        $(".pack").each(function () {
            var t = $(this);
            var navid = t.attr("navid");
            if (navid != "") {
                $.each(arr, function (i, value) {
                    var n = value
                    if (n == navid) {
                        t.find(".expandable").removeClass("close");
                        t.find(".expandable").addClass("open");
                        t.next().show();
                        t.addClass("mu");
                    }
                });
            }
        });

        $("#nav li").each(function () {
            var t = $(this);
            var navid = t.attr("id");
            if (navid != "") {
                $.each(arr, function (i, value) {
                    var n = value
                    if (n == navid) {
                        $("#nav li").removeClass("selected");
                        t.addClass("selected");
                        var i = t.attr("id").replace("nav-item", "");
                        $(".list-group").hide();
                        $("#list-group" + i).show();
                    }
                });
            }
        });
    }
}