﻿//从getNews.asp取出新闻放入#rolllatestnews里并滚动显示
//需要jQuery

function switchNews(){
	var div=$("#rolllatestnews");
	var count=$("#rolllatestnews a").length;
	if (count<2) return;
	var height=parseInt(div.css("height"));
	if (isNaN(height)) height=26;
	var current=0;
	var setps=height;
	var timer=null;
	var flag=false;
	div.html(div.html()+div.html());
	var next=function(step){
		if (step==setps) flag=true;
		if (step==0){
			flag=false;
			if (current>=count) current=0;
			div[0].scrollTop=height*current;
			return;
		}
		div[0].scrollTop+=1;
		window.setTimeout(function(){next(step-1);},30);
	};
	var stop=function(){
		if (flag) return;
		window.clearInterval(timer);
		timer=null;
	};
	var start=function(){
		if (flag) return;
		stop();
		timer=window.setInterval(function(){if (flag) return;current++;next(setps);},5000);
	};
	div.bind("mouseover mousemove",stop).bind("mouseout",start);
	start();
}

$(function(){
	var url="",re=/(?:\?|\&)url\=(.+)(?:\&|$)/i;
	$("script[src*='getNews.js']").each(function(){
		if (re.test(this.src)) url=RegExp.$1;
	});
	if (url=="") url="getNews.asp";
	$.ajax({
		url:url,
		dataType:"json",
		cache:false,
		success:function(json){
			var i,html='';
			for (var i in json)
				html+='<a href="'+json[i].link+'" '+(json[i].link.substring(0,4)=='http'?'target="blank"':'')+'>'+unescape(json[i].title)+'</a>';
			$("#rolllatestnews").html(html);
			switchNews();
		},
		error:function(){}
	});
});

