/// <reference path="jquery/jquery126.js" />
/// <reference path="jquery/JQuery.Intellisense.js" />

var _parentId = 4;
var _channelId = -100;
var _pagesize = 15;
var _pageno = 1;

$(function() {
    $("#pagelist").hide();
    var pId = GetQuery("parentId");
    var chId = GetQuery("channelId");
    if (chId != "") { _channelId = chId; }
    if (pId != "") { _parentId = pId; }

    showcurChannel.json("Sys/GetDic/dicId=" + _parentId);
    showChannel.json("Sys/GetDics/parentId=" + _parentId);


});

function showcurChannel(json) {
    if (json.DicID) {
        document.title = "绵阳市气象局--" + json.DicName;
        $("#leftchannel .setitle").html(json.DicName);
        $(".main .mtitle").html(json.DicName);
    }
}

function showChannel(json) {
    var html = '<ul>';
    $.each(json, function(i, item) {
        html += '<li><a href="javascript:;" onclick="changeChannel(' + item.DicID + ',\'' + item.DicName + '\')">' + item.DicName + '</a></li>';
    });
    $("#leftchannel .content").html(html);
    if ($("#leftchannel .content")[0].offsetHeight < 120) $("#leftchannel .content").css("height", 120);

    if (json.length == 1) {
        $("#newslist").hide();
        $("#pagelist").show();
        _channelId = json[0].DicID;
        doPages();
    }
    else {
        $.each(json, function(i, item) {
            showtops(item);
        });
    }

    /*if (json.length > 0) {
    _channelId = json[0].DicID;
    doPages();
    }*/
}
function showtops(channel) {
    if ($("#newslist .wzlist").size() > 0) {
        $("#newslist").show();
        $("#pagelist").hide();
    }
    else if (channel) {
        showtop.json("Article/GetArticleTop/kindId=" + channel.DicID + "&top=5", null, channel);
    }
}
function showtop(listjson, channel) {
    var sb = new Array();
    sb.push('<dl class="caption" onclick="changeChannel(' + channel.DicID + ',\'' + channel.DicName + '\')"><dt class="mtitle">' + channel.DicName + '</dt></dl>');
    sb.push('<div class="wzlist">');
    sb.push('<ul>');
    $.each(listjson, function(i, item) {
        if (item.Http == "") { item.Http = "article.shtml?Id=" + item.ArticleID }
        item.Title = item.Title.subString(94, true);
        if (item.ImgUrl != "") item.Title = '<span style="color:#ff0000">[图]</span>' + item.Title;
        sb.push('<li>');
        sb.push('<a target="_blank" href="' + item.Http + '">');
        if (item.TitleColor != "")
        { sb.push('<span style="color:' + item.TitleColor + '">' + item.Title + '</span>') }
        else
        { sb.push(item.Title) }
        sb.push('</a><span class="date">' + item.AddDate.getDatePart() + '</span>');
        sb.push('</li>');
    });
    sb.push('</ul></div>');
    $("#newslist").append(sb.join(""));
}


function changeChannel(chId, chname) {
    $(".main .mtitle").html(chname);
    $("#newslist").hide();
    $("#pagelist").show();
    _channelId = chId;
    _pageno = 1;
    doPages();
}

function doPages() {
    $("#wzlist").html('<img src="images/wait.gif" />');

    var pras = 'kindId=' + _channelId;
    pras += '&pageno=' + _pageno + '&pagesize=' + _pagesize;

    var url = "api/?mod=Article&action=GetArticles";
    $.ajax({
        url: url,
        type: 'POST',
        data: pras,
        dataType: 'json',
        error: function(xhr) { },
        success: function(json) {
            showNewspageList(json);
        }
    });
}


function showNewspageList(json) {
    if (json.length == 0) {
        $("#wzlist").html("无数据")
        return;
    }
    var rsCount = json[0].RsCount;

    var html = '<a href="news.shtml?channelId=' + json[0].ParentChannelID + '">' + json[0].ParentChannelName + '</a>';
    html += '&gt;' + json[0].ChannelName;
    //$("#topchannel .dcchannel").html(html);

    if (json[1].length > 0) {
        html = '<ul>';
        $.each(json[1], function(i, item) {
            if (item.Http == "") { item.Http = "article.shtml?Id=" + item.ArticleID }
            item.Title = item.Title.subString(94, true);
            if (item.ImgUrl != "") item.Title = '<span style="color:#ff0000">[图]</span>' + item.Title;
            html += '<li';
            if (i > 0 && (i + 1) % 5 == 0) {
                html += ' style="margin-bottom:30px"';
            }
            html += '>';
            html += '<a target="_blank" href="' + item.Http + '">';
            if (item.TitleColor != "")
            { html += '<span style="color:' + item.TitleColor + '">' + item.Title + '</span>' }
            else
            { html += item.Title }
            html += '</a><span class="date">' + item.AddDate.getDatePart() + '</span>';
            html += '</li>';
        });
        html += '</ul>';
    } else {
        html = "无数据";
    }


    $("#wzlist").html(html);
    if (!window.paged) {
        $('#pager').mypagination(rsCount, {
            perPage: _pagesize,
            callback: function(page) {
                if (page != _pageno) {
                    _pageno = page;
                    doPages();
                    window.scrollTo(0, 0);
                }
            }
        });
        window.paged = true;
    }
}
