﻿var Resource = {
    Common: {
        getResourceData: function(postData, fnCallback) {
            $("#resList").hide();
            $("#tbloading").show();

            $.ajax({
                type: "post",
                data: postData,
                url: "/ResourceList.List.Data",
                dataType: "json",
                success: function(returnValue) {
                    fnCallback.call(null, returnValue);
                }
            });
        }
    },
    DefaultPage: {
        changeGrade: function(grade) {
            var postData = {};
            postData['grade'] = grade;
//            $.post('/ResourceList.GetSubject.Data', postData, function(returnValue) {
//                $("#divSubject ul:first").html(returnValue);
//            });

            $("#divGrade a").removeClass("on");
            $("#divGrade a[gradeid=" + grade + "]").addClass("on");
            Resource.DefaultPage.changeSubject('-');
        },
        changeSubject: function(subject) {
            var postData = {};
            postData['grade'] = $("#divGrade a.on").attr('gradeid');
            postData['subject'] = subject;
//            $.post("/ResourceList.GetCourseBarDir.Data", postData, function(returnValue) {
//                $("#divDir ul:first").html(returnValue);
//            });
            $("#divSubject a").removeClass("on");
            $("#divSubject a[subjectid=" + subject + "]").addClass("on");
            Resource.DefaultPage.changeCourseBarDir('-');
        },
        changeCourseBarDir: function(courseBar) {
            $("#divDir a").removeClass("on");
            $("#divDir a[dirid=" + courseBar + "]").addClass("on");
            Resource.DefaultPage.goPageIndex(1);
        },
        goPageIndex: function(pageIndex) {
            var postData = {};
            postData['grade'] = $("#divGrade a.on").attr("gradeid");
            postData['subject'] = $("#divSubject a.on").attr("subjectid");
            postData['category'] = $("#divDir a.on").attr("dirid");
            postData['pageindex'] = pageIndex;
            postData['keyword'] = "";

            Resource.Common.getResourceData(postData, Resource.DefaultPage.refreshPage);
        },
        refreshPage: function(returnValue) {
            var temp = [];
            $.each(returnValue['Data']['Data'], function() {
                temp.push("    <tr>");
                temp.push("      <td class=\"li_tit\">");
                temp.push("        <a  target=\"_blank\" href=\"" + this["downloadUrl"] + "\" title=\"" + this["Name"] + "\">" + this["shortName"] + "</a>");
                temp.push("      </td>");
                temp.push("      <td>" + this["strGrade"] + "</td>");
                temp.push("      <td>" + this["strSubject"] + "</td>");
                temp.push("      <td>" + this["shortTime"] + "</td>");
                temp.push("      <td>");
                temp.push("         <a target=\"_blank\" href=\"" + this["spaceUrl"] + "\">" + this["UserName"] + "</a>")
                temp.push("      </td>");
                temp.push("      <td class=\"down\">");
                temp.push("        <a target=\"_blank\" href=\"" + this["downloadUrl"] + "\">下载</a>");
                temp.push("      </td>");
                temp.push("    </tr>");
            });

            if (returnValue["Pager"] != "") {
                temp.push("    <tr>");
                temp.push("      <td colspan=\"6\">");
                temp.push("        <div class=\"pages\">");
                temp.push("            <ul id=\"pager\" >");
                temp.push(returnValue["Pager"]);
                temp.push("            </ul>");
                temp.push("        </div>");
                temp.push("      </td>");
                temp.push("    </tr>");
            }
            $("#resList tr:gt(0)").remove();
            $("#resList tr").after(temp.join(""));
            $("#resList").show();
            $("#tbloading").hide();
        }
    },
    Search: {
        goPageIndex: function(pageIndex) {
            var postData = {};
            postData['grade'] = "-";
            postData['subject'] = "-";
            postData['category'] = "-";
            postData['pageindex'] = pageIndex;
            postData['keyword'] = search_keyword;

            Resource.Common.getResourceData(postData, Resource.Search.refreshPage);
        },
        refreshPage: function(returnValue) {
            Resource.DefaultPage.refreshPage(returnValue);
            $("#spanKeyword").html('检索关键字 <span style="color:red">' + returnValue['Keyword'] + '</span>');
            $("#spanCount").html('共找到 <span style="color:red">' + returnValue['Count'] + '</span> 个资源 ');
            if (returnValue['Keyword'].length < 1) {
                $("#spanKeyword,#spanCount").hide();
            }
            else {
                $("#spanKeyword,#spanCount").show();
            }
        }
    }
};

$(document).ready(function() {
    if (typeof pageFlag == 'undefined') {
        return;
    }
    switch (pageFlag) {
        case "resourceDefault":
            $("#btnSearch").click(function() {
                var key = $.trim($(this).prev().val());
                if (key == '请输入关键字') {
                    alert('请输入关键字');
                }
                else {
                    window.location = 'search.htm?keyword=' + escape(key);
                }
            });
            Resource.DefaultPage.goPageIndex(1);
            break;
        case "resourceSearch":
            $("#btnSearch").click(function() {
                var key = $.trim($(this).prev().val());
                if (key == '请输入关键字') {
                    alert('请输入关键字');
                }
                else {
                    search_keyword = key;
                    Resource.Search.goPageIndex(1);
                }
            });
            Resource.Search.goPageIndex(1);
            break;
    }
});




//resource zhou


try {
  
    var layerWin = document.getElementById("repalylayer");
    var ft = parseInt(document.documentElement.scrollTop + (document.documentElement.clientHeight - layerWin.offsetHeight) / 2);
    layerWin.style.top = ft + "px";
    var flag = 0;
    var speed = 10;
    var acc = 2;
    var ResourceID = $("#ResourceID").val();
} catch (e) { }

function move() {
    var t = layerWin.style.top;
    var top = parseInt(t.substring(0, t.length - 2));
    var myhigh = parseInt(document.documentElement.scrollTop + (document.documentElement.clientHeight - layerWin.offsetHeight) / 2);
    if (top < myhigh - speed) { layerWin.style.top = top + speed + "px"; }
    else if (top > myhigh + speed) { layerWin.style.top = top - speed + "px"; }
    setTimeout("move()", 2);
}

function scall() { if (flag == 0) { move(); flag = 1; } }

function Score(ID, Point) {
    $.post("/ResourceList.Score.data", { Point: Point, ID: ID }, function(data) {
    if (data == "false")
            alert("你已经评过分了!")
        else {
            alert(data);
            location.reload();
        }
    });
}

function closerepaly() {
    $(layerWin).hide();

}

function Comment(ResourceID) {
    if ($("#Content").val() == "" || $("#Content").val() == "在此发表评论！") {
        alert("请输入评论内容");
        $("#Content").focus();
        return;
    }
    if ($("#Content").val().length > 5000) {
        alert("评论内容过长");
        $("#Content").focus();
        return;
    }
    var Info = {};
    Info.Content = $("#Content").val();
    if ($("#hidename").length == 0) {
        Info.Hidename = false;
    }
    else {
        Info.Hidename = $("#hidename")[0].checked;
    }
    Info.ResourceID = ResourceID;
    $.post("/ResourceList.Comment.data", Info, function(data) {
        if (data == "true") {
            alert("发表成功");
            location.reload();
        } else {
            alert(data);
        }
    })
}


function Support(ID) {
    $.post("/ResourceList.Support.data", { ID: ID}, function(data) {
        self.location.reload();
    })
}

function Del(ID) {
    $.post("/ResourceList.Del.data", { ID: ID }, function(data) {
        if (data == "true") {
            alert("删除成功");
            self.location.reload();
        } else {
            alert(data);
        }
    })
}

function Against(ID) {
    $.post("/ResourceList.Against.data", { ID: ID }, function(data) {
        self.location.reload();
    })
}


function PreRepaly(id) {
    repalyid = id;
    $(layerWin).show();
    $("#repalycontent").focus();
}

function Repaly(ResourceID) {
    if ($("#repalycontent").val() == "" || $("#repalycontent").val() == "在此发表评论！") {
        alert("请输入评论内容");
        $("#repalycontent").focus();
        return;
    }
    $("rbt").hide();
    var Info = {};
    Info.Content = $("#repalycontent").val();
    //Info.hidename=$("#repalyhidename")[0].checked;
    Info.ResourceID = ResourceID;
    Info.ParentID = repalyid
    $.post("/ResourceList.Comment.data", Info, function(data) {
        $("rbt").show();
        if (data == "true") {
            alert("发表成功");
            location.reload();
        } else {
            alert(data);
        }
    })
}

window.onscroll = scall;

$(".score li").hover(function() { $(".score li.on").attr("class", "noton"); $(this).addClass("hover").prevAll().addClass("hover"); }, function() { $(".score li.hover").removeClass("hover"); $(".score li.noton").attr("class", "on"); });

//resource end zhou
