
(function($){

	$(function(){

		var $news = $("#topics tr.topic");
		var length = $news.length;
		var EffectDuration = 500;
		var IntervalTime = 4000;

		$("#headline").append("<ul></ul>");
		$news.each(function(){
			var texts = $(".texts",this).html();
			$("#headline ul").append("<li>"+texts+"</li>");
			var category = $(".icon img",this).attr("alt");
			if(texts.indexOf("</a>") != -1 || texts.indexOf("</A>") != -1){
				$("#headline li:last>a").append(" ("+category+")");
			}else{
				$("#headline li:last").append(" ("+category+")");
			}
		});

		var newsChange = function(step){
			clearTimeout(newstimer);
			newstimer = setTimeout(rotation,IntervalTime+EffectDuration);
			if(step==1){
				//next
				$("#headline li:first").clone(true).appendTo("#headline ul");
				targetY = -27;
				targetIndex = 2;
			}else{
				//prev
				$("#headline li:last").clone(true).css("margin-top","-27px").prependTo("#headline ul");
				targetY = 0;
				targetIndex = 1;
			}
			$("#headline li:first").animate( { marginTop: targetY+"px" }, EffectDuration, 'swing', function(){
				if(step==1){
					$("#headline li:first").remove();
				}else{
					$("#headline li:last").remove();
				}
			});
		}
		
		// ローテーション
		function rotation(){
			newsChange(1);
		}
		newstimer = setTimeout(rotation,IntervalTime);
		

			   
	});

})(jQuery);
