/*
$(document).ready(function(){
$("#right-cell img").attr("src", "images/img-right-event.jpg");
$("#left-cell img").attr("src", "images/img-left-event.jpg");
});
*/

$(document).ready(function(){
	
	if($("#banner").children().size() != 0){
		 setTimeout(function() {
			$("#banner").fadeIn('slow');
		  }, 1250);
		
/*		
		$("#main").toggle(function () {
			$("#banner").fadeOut();
			},
			function () {
			$("#banner").fadeIn();
		}
		);
		
		$("#banner img").click(function(){
			location.href="event.html";								
		});
*/
	}
	
		////////
	$(document.body).append('<div id="tabelog" style=""><img src="imgTop/ban_tabelog.gif" width="116" height="24"></div>');
	$("#tabelog img").click(function(){
			window.open("http://r.tabelog.com/chiba/A1201/A120103/12020307/", "_blank");
	});
	
	
	if($("h3#title").html() == 'Welcome to KavaKava by the sea!'){

		$(document.body).append('<div id="twitter"><img src="images/twitarrow.png" id="twitarrow" /><a href="http://www.twitter.com/suu_bartender" title="Follow us on Twitter" target="_blank" id="followbtn"><img src="images/follow_twitter_button_a.png" alt="Follow us on Twitter" width="121" height="24" /></a><div id="tweet"></div></div>');
		loadTweetData();
		$("#twitarrow").hide();
		$("#twitarrow").css({ left: "-1000px", width:"474px", height:"204px"});
		
		setTimeout(function() {
			$("#twitarrow").animate(
			{width: "120px", height: "53px", opacity: "100%", left:"-100px"},
			{duration: "slow", easing: "swing"})}, 1600);
		
			$("#twitter").toggle(
				function () {
					$(this).animate({right: "10px" }, 500 );
				},
				function () {
					$(this).animate({right: "-225px"}, 500 );
				}
				);
		
		$("#twitter a").click(function(){
							window.open($(this).attr("href"));
						});
		
				
			
		
	
	}

	
	
/*	var url = "images/eventImage.png";
	
	var element = new Image();
	element.src = url;
	element.id = "eventImg";
	$(document.body).append(element);
	
	$(document.body).toggle(function () {
		$("#eventImg").animate({right:"-100px"}, 700 );
		},
		function () {
		$("#eventImg").animate({right:"10px"}, 700 );
	}
	);
$(document.body).append('<style type="text/css"><!--#eventImg{z-index:1000;position:fixed!important;position:absolute;bottom:10px;right:10px;} --></style>');
*/
});



var recentID = 0;
var twitterID = 'suu_bartender'


function init(data){
		
	$.each(data, function(i, val){
		//console.log(val.text);
		
		if(val.id > recentID){		
			var _d = new Date(val.created_at);
			var _ymd = new Array(_d.getYear()+1900, zero(_d.getMonth()), zero(_d.getDate()));
			var _his = new Array(zero(_d.getHours()), zero(_d.getMinutes()), zero(_d.getSeconds()));
			var date = _ymd.join("/") + " " + _his.join(":");
			
			var img = $("<img/>");
			img.attr("src", val.user.profile_image_url);
			
			var div = $("<div/>");
			div.attr("id", "user");
			var txt = $("<div/>").html(val.text)
			var username =  $("<a/>").html(val.user.name).attr({
												href: 'http://twitter.jp/' + val.user.screen_name,
												target: "_blank"
												});
			username.click(function(){
							window.open($(this).attr("href"));
							});
			
			var span = $("<span/>").html(" : "+date);
			span.prepend(username);
			
			txt.prepend(img);
			div.append(txt);
			div.append(span);
			div.hide();
	
			if(recentID === 0){
				$("#tweet").append(div);
			}else{
				$("#tweet").prepend(div);
			}
			div.fadeIn("slow");
		}
		
	});
	
	//recentID = data[0].id;
	//console.log(recentID);
	//setTimeout("loadTweetData()", 10000);
	
}

function zero(data){

	return ("0"+data).slice(-2);

}

function loadTweetData(){
	//console.log("loading");
  $.ajax({
    type:"GET",
    //cache: false,
	url:'http://api.twitter.com/1/statuses/user_timeline/'+twitterID+'.json?callback=init',
    dataType:"jsonp",
	jsonp : 'init',
    //success: function(xml){ init(xml);},
	error: function(xml){ alert("error"); }
	});
}

