﻿// JavaScript Document
function googleFeed(feedUrl,itemNum,InsId) {
google.load("feeds", "1");

function initialize() {
var feed = new google.feeds.Feed(feedUrl);
feed.setNumEntries(itemNum);

feed.load(function(result) {
	if (!result.error) {
		var container = document.getElementById(InsId);
		$('#'+InsId).prepend("<ul></ul>");	
		for (var i = 0; i < result.feed.entries.length; i++) {
			var entry = result.feed.entries[i];	
			$('#'+InsId+'>ul').append("<li><a href ='" + entry.link + "'>" + entry.title + "<\/a><\/li>");
		}
		container.innerHTML += "<\/ul>";
		$("#"+InsId).fadeIn('slow');
	}
	else {
		var container = document.getElementById(InsId);
		container.innerHTML += "<p>アイテムがありません<\/p>";
	}
	
});
}
google.setOnLoadCallback(initialize);
}

function getUserTl(user,resultNum,targetId) {
	$.ajaxSetup({scriptCharset:'utf-8'});
  $.ajax({
    type: "GET",
    url: "http://twitter.com/statuses/user_timeline/"+user+".json",
    data: {
      "count": resultNum
    },
    dataType: "jsonp",
    success: function(data) {
	  $(targetId).prepend("<ul></ul>");
      $.each(data, function(i, item) {
				var tweetDate = item.created_at;
				var tweetDates = tweetDate.split(" ");
				tweetDate = tweetDates[1]+" "+tweetDates[2]+", "+tweetDates[5]+" "+tweetDates[3];
				tweetDate = Date.parse(tweetDate);				
				var currentDate = new Date();
				currentDate = currentDate.getTime();			
				var deltaMiliSec = currentDate - tweetDate - 9*60*60*1000;
				var tweetDateAgo = "";
				if (deltaMiliSec<=60*1000){
				tweetDateAgo = "1min";
				}
				else if (deltaMiliSec<60*60*1000){
				tweetDateAgo = Math.floor(deltaMiliSec/60/1000)+"min";
				}
				else if (deltaMiliSec<24*60*60*1000){
				tweetDateAgo = Math.floor(deltaMiliSec/60/60/1000)+"hour";
				}
				else if (deltaMiliSec<7*24*60*60*1000){
				tweetDateAgo = Math.floor(deltaMiliSec/24/60/60/1000)+"day";
				}
				else if (deltaMiliSec<4*7*24*60*60*1000){
				tweetDateAgo = Math.floor(deltaMiliSec/7/24/60/60/1000)+"week";
				}
				else if (deltaMiliSec<12*4*7*24*60*60*1000){
				tweetDateAgo = Math.floor(deltaMiliSec/4/7/24/60/60/1000)+"month";
				}
				else {
				tweetDateAgo = Math.floor(deltaMiliSec/12/4/7/24/60/60/1000)+"year";			
				}				
        $(targetId+">ul").append($("<li />")
             .append($("<a href='http://twitter.com/"+user+"/status/"+item.id+"' />").append(item.text))
						 .append($("<span />").append(" "+tweetDateAgo))
        );
      $(targetId).fadeIn('slow');
	  });
    },
	error: function() {
	  $(targetId).append("<p>now busy</p>");
	}	
  });
}

googleFeed("http://www.a-magic-web.com/feed?123",5,"notes");
googleFeed("http://www.google.com/reader/public/atom/user/16895966356877178393/state/com.google/starred",5,"star");
googleFeed("http://feeds.delicious.com/v2/rss/vohedge",5,"delicious");
getUserTl("vohedge",4,"#twitter");

$(function(){
	var sliderInner = ".slider>.inner>.container>div";
	var ulBlock = ".slider ul";
	var leftBottun = ".slider_left";
	var rightBottun = ".slider_right";
	var sliderWidth = 828;
	
	$(sliderInner).css("width",sliderWidth*($(ulBlock).size()+1)+"px");
	$(ulBlock+":last").clone(true).prependTo(sliderInner);
	$(sliderInner).css("margin-left","-"+sliderWidth+"px");

	$(leftBottun).click(function(){
		$(leftBottun+","+rightBottun).hide();
		$(sliderInner).animate({
			marginLeft : parseInt($(sliderInner).css("margin-left"))+sliderWidth+"px"
		},"slow","swing" , 
		function(){
			$(ulBlock+":last").remove();
			$(sliderInner).css("margin-left","-"+sliderWidth+"px");
			$(ulBlock+":last").clone(true).prependTo(sliderInner);
			$(leftBottun+","+rightBottun).show();
		})
	})
	
	$(rightBottun).click(function(){
		$(leftBottun+","+rightBottun).hide();
		$(sliderInner).animate({
			marginLeft : parseInt($(sliderInner).css("margin-left"))-sliderWidth+"px"
		},"slow","swing" , 
		function(){
			$(ulBlock+":first").remove();
			$(sliderInner).css("margin-left","-"+sliderWidth+"px");
			$(ulBlock+":first").clone(true).appendTo(sliderInner);
			$(leftBottun+","+rightBottun).show();
		})
	})	
})