// DomContentLoaded
$(function()
{	
	/* 新しい投稿がないか定期的にチェック */
	
	// 投稿数の初期値
	updated_count = 0;
	latest_count = 0;
	
	$.get(__webroot + "javascripts/?func=get_notes_count", function(data){
		updated_count = parseInt(data.replace(/<.+>/,""));
		$("#updateCount").html(parseInt(data.replace(/<.+>/,"")));
	});
	
	
	$("#updateCount").everyTime( __vals.time_updateed , function() {
		
		// 総投稿数を取得
		$.get(__webroot + "javascripts/?func=get_notes_count"+"&time="+(new Date).getTime(), function(data){
			latest_count = parseInt(data.replace(/<.+>/,""));
			
			// 総投稿数が増えていた場合
			if ( updated_count < latest_count ) {
				
				$("#updateCount").html(latest_count - updated_count);
				$("#updateMessageWrapper > *").slideDown("slow");
			}
		});
	});
	
	/* メッセージクリックでリロード */
	$("#updateMessage").live("click", function() {
		updated_count = latest_count;
		reloadThread();
		$(this).fadeOut();
	});
	
	/* もっと読む */
	
	$("#NoteIndexForm #btnMoreContainer img").live("click", function(){
		getNotes(
			__webroot + "javascripts/?func=get_top_notes"
			+ "&thread_sequence_id=" + $(this).parents().find("#lastSequenceId").val()
		);
	});
	
	kCommand();	
});

// スレッド更新
function reloadThread(threadId) {
	
	var id = threadId||0;
	
	$.get( __webroot + "javascripts/?func=get_top_notes&thread_sequence_id="+id+"&time="+(new Date).getTime(), 
	
		function(data){
			var main = $("#content div.maincontentsArea");
			$("#updateMessageWrapper > *").hide();
			main.find("#btnMoreContainer").remove();
			main.find("div.commentContainer").not("#updateArea").remove();
			main.append(data);
			$("#updateArea").hide();
		}
	);
}


function kCommand() {
	
	var kkeys = [], knm = "38,38,40,40,37,39,37,39,66,65";
	
	$(document).keydown(function(e) {
		
		kkeys.push( e.keyCode );
		
		if ( kkeys.toString().indexOf( knm ) >= 0 ) {
			$(document).unbind('keydown',arguments.callee);
			
			var chara = $("#content div.headMessageBlock:first p.chara img:first");			
			var chara2 = chara.clone(true).insertAfter("#content");
			var l = chara.offset().left;
			var t = chara.offset().top;
			
			chara2.css({"position":"absolute","z-index":"999","left":l,"top":t});
			chara.hide();
			
			chara2
				.animate({left:l-20+"px",top:t-10+"px"},"slow")
				.animate({left:l-40+"px",top:t+10+"px"},"slow")
				.animate({left:l-60+"px",top:t-10+"px"},"slow")
				.animate({left:l-80+"px",top:t+10+"px"},"slow")
				.animate({left:l-100+"px",top:t-30+"px"},"slow")
				.animate({left:l-1300+"px",top:t-200+"px",width:"150%",height:"150%",opacity:0},"slow")
				.animate({left:l+"px",top:t+"px",width:"1px",height:"1px"},"fast")
			;
		}
	});
	
}



