$(function(){
	/* サブメニュー
	============================ */
	var snav = $('#sub-nav');
	//$('>div', snav).hide();
	$('#nav li').mouseover(function(){
		$('>h2',snav).hide();
		$('>div.section',snav).hide();
		var nextNav = $('div.section',snav).eq($(this).index());
		$(nextNav).fadeIn();
	});
	
	$('>div>div',snav).hover(function() {
		$(this).css(  'background-color', '#eee' )
	},function() {
		$(this).css('background-color', 'transparent')
	});
	
	
	/* タブメニュー
	============================ */
	//tab1以外は非表示
	/*
	$("#view>div:not(#tab1)").hide();
	$("#tab-nav li").click(function(){
		$("#view>div").hide();
		var nextTab = "#tab"+($(this).index()+1);
		//console.log(nextTab);
		$(nextTab).fadeIn();
		//alert(nextTab);
	});
	*/
	
	/* Twitter
	============================ */
	var view = $("#tab1 table");
	function replaceTwTimeStr(time){
		var tArray = time.split(" ");
		var timeStr = tArray[0] + " " + tArray[1] + " " + tArray[2] + " " + tArray[5] + " " + tArray[3] + " " + tArray[4];
		return timeStr;
	}
	function viewTweet(json) {
		for(var i in json){
			//console.log(json[i].user.profile_image_url);
			var text = json[i].text;
			//URLにリンクを付ける
			text = text.replace(/(s?https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:@&=+$,%#]+)/gi,'<a href="$1" target="_blank">$1</a>');
			//ハッシュタグにリンク
			text = text.replace(/#(\w+)/gi,'<a href="http://twitter.com/search?q=%23$1" target="_blank">#$1</a>');
			//リプライにリンク
			text = text.replace(/@(\w+)/gi,'<a href="http://twitter.com/$1" target="_blank">@$1</a>');
			
			view.append("<tr>"+
				'<th><img src="'+json[i].user.profile_image_url+'" width="48" height="48" /></th>'+
				'<td>'+
					'<p><a href="http://twitter.com/'+json[i].user.screen_name+'" target="_blank">'+json[i].user.name+'</a>　'+text+'</p>'+
					'<p class="date">'+dateObj.format('Y-m-d H:i', replaceTwTimeStr(json[i].created_at))+'</p>'+
				'</td>'+
				"</tr>");
		}
	}
	
	var dateObj = new Date;
	//PickUp
	//$.getJSON('http://twitter.com/statuses/user_timeline/shutokupickup.json?callback=?', function (json) {
	$.getJSON('http://twitter.com/statuses/user_timeline/shutokupickup.json?callback=?', function (json) {
		viewTweet(json);
	});
	//タブがクリックされたら
	$("#tab-nav li").click(function(){
		var btnID = $(this).attr("id");
		view.html('<tr class="loading"><td><img src="img/home/loader.gif" /></td></tr>');
		var getUrl;
		getUrl = 'http://twitter.com/statuses/user_timeline/'+btnID+'.json?callback=?';
		$.getJSON(getUrl, function (json) {
			view.children().remove();
			viewTweet(json);
		});
	});
	
});
