﻿var CurrentNewsTypeID = "";
var CurrentSearchContent = "";
var CurrentPageNum = 1;
var RowsShowNum = 5;
var StringShowLength = 10;
var IsLoad = false;
var IsSearch = false;
var CurrenttbSearchID = "";
var CurrentbtnSearchID = "";
IsNewsPage = true;

function SetNewsPageConfig(ShowNum,ShowLength,btnSearch,tbSearch)
{
    RowsShowNum = ShowNum;
    StringShowLength = ShowLength;
    CurrenttbSearchID = tbSearch;
    CurrentbtnSearchID = btnSearch;
}

function DrawNewsTypeList()
{
    var TypeList = document.getElementById("TypeList");
    RemoveAllNodes("TypeList");
    var LI = document.createElement("LI");
    LI.innerHTML = "<a href=\"javascript:ShowNewsByType('All')\">所有新闻</a>";
    LI.className = "actives";
    LI.id = "All";
    TypeList.appendChild(LI);
    for(i = 0; i < NewsTypeList.length; i++)
    {
        var LI = document.createElement("LI");
        LI.innerHTML = "<a href=\"javascript:ShowNewsByType('" + NewsTypeList[i].NewsTypeID + "')\">"+NewsTypeList[i].NewsTypeName+"</a>";
        LI.id = NewsTypeList[i].NewsTypeID;
        TypeList.appendChild(LI);
    }
}

function ShowNewsByType(TypeID)
{
    var ID = ReadCookie("NewsTypeID");
    if(ID != null && ID != "" && ID != "null")
    {
        CurrentNewsTypeID = ID;
        SetCookie("NewsTypeID",null);
    }
    else
    {
        CurrentNewsTypeID = TypeID;
    }
    if(!IsLoad)
    {
        IsLoad = true;
        IsSearch = false;
        SetNewsTypeButtonState();
        var LI = document.getElementById(CurrentNewsTypeID);
        LI.className = "actives";
        if(CurrentNewsTypeID == "All")
        {CurrentNewsTypeID="";}
        ChangeContent(1);
    }
}

function SetNewsTypeButtonState()
{
    document.getElementById("All").className = "";
    for(i = 0; i < NewsTypeList.length; i++)
    {
        var LI = document.getElementById(NewsTypeList[i].NewsTypeID);
        LI.className = "";
    }
}

function ChangeContent(PageNum)
{
    var Message = document.getElementById("Message");
    var NewsList = document.getElementById("NewsList");
    var Pagination = document.getElementById("Pagination");
    Message.style.display = "";
    Message.innerHTML = "正在读取新闻，请稍候！";
    NewsList.style.display = "none";
    Pagination.style.display = "none";
    CurrentPageNum = PageNum;
    if(!IsSearch)
    {
        TyPage.GetNewsListByTypeID(CurrentNewsTypeID,StringShowLength,CurrentPageNum,RowsShowNum,DrawList);
    }
    else
    {
        TyPage.GetNewsListBySearch(CurrentSearchContent,StringShowLength,CurrentPageNum,RowsShowNum,DrawList);
    }
}

function DrawList(Data)
{
    if(Data != null)
    {
        if(Data.value != null)
        {
            if(Data.value.IsSuccess)
            {
                var Message = document.getElementById("Message");
                var NewsList = document.getElementById("NewsList");
                RemoveAllNodes("NewsList");
                var i = 0;
                var Row = Data.value.DataList[i];
                while(Row != null)
                {
                    var LI = document.createElement("LI");
                    var HTML = "<span class=\"Date\">" + Row[3] + "</span>";
                    HTML += "<span class=\"Sort\">[" + GetNewsTypeNameByID(Row[1]) + "]</span>";
                    HTML += "<a href=\"news.aspx?nid=" + Row[0] + "&tid=" + Row[1] + "\" title=\""+Row[5]+"\" target=\"_blank\">" + Row[2] + "</a>";
                    HTML += Row[4] == "1" ? "<img src=\"../Images/new.gif\" align=\"absmiddle\" />" : "";
                    LI.innerHTML = HTML;
                    NewsList.appendChild(LI);
                    i++;
                    Row = Data.value.DataList[i];
                }
                var SumRowNum = 0;
                var SumRowNum = Data.value.Tag;
                var SumPageNum = Math.ceil(SumRowNum/RowsShowNum);
                var CurrentRowNum = i;
                initPagingation(SumPageNum,CurrentPageNum,SumRowNum,CurrentRowNum,"ChangeContent(_pn_)","");
                if(i == 0)
                {   
                    Message.style.display = "";
                    Message.innerHTML = "无新闻！";
                }
                else
                {
                    Message.style.display = "none";
                    NewsList.style.display = "";
                }
                if(SumPageNum > 1)
                {
                    var Pagination = document.getElementById("Pagination");
                    Pagination.style.display = "";
                }
            }
        }
    }
    IsLoad = false;
}

function WriteInput()
{
    var tbSearch = document.getElementById(CurrenttbSearchID);
    tbSearch.value = "";
    tbSearch.style.color = "black";
}

function LeaveInput()
{
    var tbSearch = document.getElementById(CurrenttbSearchID);
    if(GetStringLenth(tbSearch.value) == 0)
    {
        tbSearch.value = "请输入搜索条件";
        tbSearch.style.color = "Gray";
    }
}

function Search()
{
    var tbSearch = document.getElementById(CurrenttbSearchID);
    if(tbSearch.value != "请输入搜索条件" && tbSearch.value != "")
    {
        IsSearch = true;
        CurrentSearchContent = tbSearch.value;
        ChangeContent(1);
        SetNewsTypeButtonState();
        document.getElementById("All").className = "actives";
    }
    else
    {
        tbSearch.value = "请输入搜索条件";
        tbSearch.style.color = "Gray";
        tbSearch.onblur();
        alert("请输入搜索条件！");
    }
}