﻿$(document).ready(function(){
    var _Page = 1;
    var _PageSize=25;
    var _NewsType = Request("NewsType");
    var _Country = Request("Country");
    var _key = Request("key");
    
    $.post('/Json/SetNewsList.aspx?PageSize='+_PageSize+'&Page='+_Page+'',{NewKey:_key,Country:_Country,NewsType:_NewsType}, function(data) {
        $.each(data, function(entryIndex, entry) {;
            $('#NewsList').html(entry['Html']);
            Page(1,entry['PageCount']);
        
        });
    },'json');
    
    $("#Search").click(function(){
        $("#NewsList").html("<img src='/images/index/loading_wh.gif' />");
        $.post('/Json/SetNewsList.aspx?PageSize='+_PageSize+'&Page='+_Page+'',{NewKey:$('#key').val(),Country:$('#Country').val(),NewsType:Request("NewsType")}, function(data) {
            $.each(data, function(entryIndex, entry) {;
                $('#NewsList').html(entry['Html']);
                Page(1,entry['PageCount']);
            
            });
        },'json');
    });

    function Page(page,pagecount)
    {
        $.post('/Json/pagination.aspx', { Page: page, PageCount:pagecount}, function(data) {
            $('#pagination1').html(data);
            $("*[name='Page']").each(function(index){
                $('#'+$(this).attr('id')).click(function(){
                    Page($(this).attr('id'),pagecount);

                      GetHtml(_PageSize,$(this).attr('id'),null)
                });
                
            });
            
            $('#Next').click(function() {
                var page = _Page + 1;
                Page(page,pagecount);
                GetHtml(_PageSize,page,null)
            });
            
            $('#prev').click(function() {
                var page = _Page - 1;
                Page(page,pagecount);
                GetHtml(_PageSize,page,null)
            });
        });
    }
    
    function GetHtml(PageSize,PageIndex,isNext)
    {
        $("#NewsList").html("<img src='/images/index/loading_wh.gif' />");
        $.post('/Json/SetNewsList.aspx?PageSize='+PageSize+'&Page='+PageIndex+'',{NewKey:$('#key').val(),Country:$('#Country').val(),NewsType:Request("NewsType")}, function(data) {

            $.each(data, function(entryIndex, entry) {
                
                $('#NewsList').html(entry['Html']);
               
                _Page = new Number(PageIndex);
            });
        },'json');
    }
    
    $.getJSON('/Json/GetCountry.aspx', function(data){
        var html = '';
        $.each(data, function(entryIndex, entry){
            html = '<option value=' + entry['id'] + '>' + entry['CountryNameChs'] + '</option>';
            $('#Country').append(html);
        });
    })
    
    $("#Search1").click(function(){
        window.open("/News/index.aspx?Country="+$('#Country').val()+"&key="+$('#key').val()); 

    });
    
});


function Request(strName)
{
    //获取当前的Url地址
    var strHref =document.location.href;
    //document.getElementById("searchInput").value=strHref;
    var intPos = strHref.indexOf("?");
    var strRight = strHref.substr(intPos + 1);

    var arrTmp = strRight.split("&");
    for(var i = 0; i < arrTmp.length; i++)
    {
        var arrTemp = arrTmp[i].split("=");

        if(arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1];
    }
    return "";
}
