﻿var _category1Id=0,_newsId=0,_keyword='';
var currentPage=1;
var startAt=0;

function setVar(c1,nid,k)
{
	_category1Id=c1;
	_newsId=nid;
	_keyword=k;
}

function getHotKeywords()
{
	var id="#HotKeywords";
	loading(id);
    var url = "/XHttpService/XHttp.ashx?t=hotkeywords&f=get&rn="+Math.random();    
	$.get(url, function(data)
    {
	var v=$.trim(data).split('%|%');
		if(v[0]=="succeed")
		{
			$(id).html(v[1]);		
		}
		else if(v[0]=="failed")
		{
		$(id).html("暂时没有相关内容..");
		}
    });
};

function getRelatedNews()
{
	var id="#RelatedNewsList";
	loading(id);
    var url = "/XHttpService/XHttp.ashx?t=news&f=related&cid="+_category1Id+"&nid="+_newsId+"&num=10&k="+escape(_keyword)+"&rn="+Math.random();    
	$.get(url, function(data)
    {
		var v=$.trim(data);
		if(v=="")
		{
		$(id).html("暂时没有相关内容..");
		}
		else
		{
			$(id).html(data);
		}
    });
};

function getHotNews(id,bound)
{
	loading(id);
    var url = "/XHttpService/XHttp.ashx?t=news&f=hot&bound="+bound+"&cid="+_category1Id+"&num=10&rn="+Math.random();   
	$.get(url, function(data)
    {
		var v=$.trim(data);
		if(v=="")
		{
			$(id).html("暂时没有相关内容..");
		}
		else
		{
		$(id).html(data);
		}
    });
};

function getCommendNews()
{
	var id="#CommendNewsList";
	loading(id);
    var url = "/XHttpService/XHttp.ashx?t=news&f=commend&cid="+_category1Id+"&num=10&rn="+Math.random();    
	$.get(url, function(data)
    {
		var v=$.trim(data);
		if(v=="")
		{
		$(id).html("暂时没有相关内容..");
		}
		else
		{
		$(id).html(data);
		}
    });
};


function getComments(newsid)
{
    var url = "/XHttpService/XHttp.ashx?t=news&f=comments&newsid="+newsid+"&startat="+startAt+"&rn="+Math.random();
    $.get(url, function(data) {
        var v = $.trim(data).split('%|%');
        if (v[0] == "succeed") {
            var list = eval('(' + v[2] + ')');
            var count = list.Nickname.length;
            $(".comments > ul").empty();
            $("#commentcount").html("已有评论" + count + "条");
            for (var i = 0; i < count; i++) {
                $(".comments ul").append("<li><span class=\"name\">" + list.Nickname[i] + "</span> 发表于 <span class=\"date\">" + list.AddDate[i] + "</span><div class=\"comment\">" + list.Content[i] + "</div></li>");
            }
            if (v[1] > 10) {
                commentPaing(newsid, currentPage, v[1]);
            }
        }
        else if (v[0] == "failed") {
        $(".comments > ul").html("很抱歉，现在无法读取评论...");
        }
        else if (v[0] == "none") {
            $(".comments > ul").html("<span class=red>当前没有评论，快去抢沙发，发表第一篇评论！</span>");
        }
    }); 
};

function commentPaing(newsid,cur,total)
{
	$("#CommentPaing").pagefoot({
	pagesize:10,
	current:currentPage,
	count:total,
	css:"paing",
	paging:function(page){
	currentPage=page;		
		startAt=(currentPage-1)*10;
		getNewsComments(newsid);
		}
	})
};

function checkLayout()
{
	var w=500;
	var h=500;
	$("#description table").each(function(){
        var t = $(this);
        if(t.width()>w)
        {   t.width(w);
        }
    });
    
    $("#description img").each(function(){
        var img = $(this);
        var s_width=img.width();
        var s_height=img.height();
        if(img.width()>w)
        {
            img.width(w);
            img.height(w/s_width*s_height);
        }
        if(img.height()>h)
        {
            img.height(h);
            img.width(h/s_height*s_width);
        }
    });
};

function getUserInfo()
{
    var url = "/XHttpService/XHttp.ashx?t=user&f=userinfo&rn="+Math.random();
	$.get(url,function(data)
	{	var v=$.trim(data).split('%|%');
		if(v[0]=="succeed")
		{
			$("#nickname").val(v[1]);
			$("#H_Userid").val(v[2]);		
		}
		else if(v[0]=="failed")
		{
		$("#nickname").val("匿名");
		$("#H_Userid").val("0");
		}
	 }); 
};

function zoomDoc(size){
$("#description").css("font-size",size);
};


$(document).ready(function(){
checkLayout();
//getHotNews("#WeekFocusNewsList","week");
//getHotNews("#AllHotNewsList","all");
//getCommendNews();
//getRelatedNews();
//getUserInfo();
//getHotKeywords();
});