$(document).ready(function(){

	var blogs = [
		{
			id:"blog4",
			url:"http://feedblog.ameba.jp/rss/ameblo/ebyoshida/rss20.xml"
		}
		,{
			id:"blog1",
			url:"http://feedblog.ameba.jp/rss/ameblo/ebgc/rss20.xml"
		}
		,{
			id:"blog2",
			url:"http://feedblog.ameba.jp/rss/ameblo/ebpros/rss20.xml"
		}
		,{
			id:"blog3",
			url:"http://feedblog.ameba.jp/rss/ameblo/proshopeb/rss20.xml"
		}
	];

	var speed = 5000;
	var stop = 1000;

	var wrapp = $("#newsTicker");
	var items = wrapp.find("li");




	var width = wrapp.width();
//	var width = 815;
	var height = wrapp.css("height");

	var max = items.length;
	var now= -1;
	var oldNow = 0;
	
	wrapp.css({"width":width, "height":height,"position":"relative","whiteSpace":"nowrap","overflow":"hidden"});
	items.css({"position":"absolute" ,"left":width});

	view(items.eq(0));

	function view(){
		now++;
		$(items.eq(now)).animate({"left": "-="+width}, speed,null,dilay);
	}
	function dilay(){
		setTimeout(unView,stop);
	}
	
	function unView(){
		oldNow = now;
		$(items.eq(oldNow)).animate({"left": "-="+width}, speed,null,reset);

		if(now+1 == max)
			now = -1;
		view();
		
	}
	function reset(){
		$(items.eq(oldNow)).css({"left":width});
	}



	$(blogs).each(function(i){
		getAjax(this);
	});
	
	function getAjax(blog){
		$.ajax({
			url:"/js/lib/cd.php",
			type:'post',
			data:{u:blog.url},
			dataType:'xml',
			timeout:35000,
			success:function(xml,status){
				if(status!='success')
					return;

				var blogTitle = $(xml).find("title:first").text();
				var blogLink = $(xml).find("link:first").text();
				var postTitle = $(xml).find("item title:first").text();
				var postLink = $(xml).find("item link:first").text();
				var postDate = $(xml).find("item pubDate:first").text();
				postDate = new Date(postDate);
				postDate = structureDateString(postDate);


				var li = wrapp.find("#"+blog.id);

				li.find(".post").text(postTitle);
				li.find(".post").attr("href",postLink);
				li.find(".blog").text(blogTitle);
				li.find(".blog").attr("href",blogLink);
				li.find(".date").text(postDate);
			}
		});
	}

	

});

function structureDateString(date){
	var year = 0;
	if (date.getFullYear) {
		year = date.getFullYear();
	} else {
		year = date.getYear();
		if (year < 1900) year += 1900;
	}
	return year + "/"+ (date.getMonth()+1) + "/"+ date.getDate();
}
