/*
 * 前頁共通関数
 */


// DomContentLoaded
$(function()
{	
	/* グローバル変数定義 */
	
	__vals = 
	{
		"thred_length"  : 10,
		"time_mentions" : 60000,
		"time_updateed" : 60000,
		"time_botreply" : 5000,
		"time_dispmentions" : 5000,
		"message_delete": "この投稿を削除します。よろしいですか？" ,
		"message_user_delete" : "このユーザーを削除します。よろしいですか？" ,
		"message_reply_delete": "この返信を削除します。よろしいですか？" ,
		"error_default" : "投稿に失敗しました" ,
		"error_wordover": "140文字以内で投稿して下さい"
	};
	
	window.__vals    = __vals;
	window.__webroot = $("#webroot").val();
	
	var mainArea = $("#content div.maincontentsArea:first");
	var sideArea = $("#content div.sidecontentsArea:first");
	
	// 通知メッセージ表示
	showNotify();
	
	// Twitterでの反応 
	var mentionBox = $("#twitterReaction");
	// 初期表示
	mentionBox.find("ul:first li:gt(5)").show();
	// 定期表示
	mentionBox.everyTime( __vals.time_dispmentions, "disp", dispMentions);
	// 定期更新
	//mentionBox.everyTime( 60000, getMentions);

	// 投稿削除
	mainArea.find("p.deleteBtnContainer").live("click", delNote);
	
	// 新着RTアニメーション */
	mainArea.find("li.new span.icon img").animate({opacity:0},500,'easeOutExpo').animate({opacity:100},500,'easeOutExpo').animate({opacity:0},500,'easeOutExpo').animate({opacity:100},500,'easeOutExpo');
	
	// イイカモボタン
	mainArea.find("div.iikamo p.btn").live("click", iikamoUp);
	
	// 発言単位のボタンロールオーバー
	dispUserBoxBtn();

	// サイドバーにあわせてリサイズ
	$.event.add(window, "load", fitMainContentsArea);
	
});

/*
 	twitterでの反応を徐々に表示 
 */

function dispMentions() {
	
	var li = $(this).find("ul:first li:hidden:last");
	
	if (li.size()>0) {
		li.slideDown("slow");
	} else {
		// 非表示要素がなくなったら停止して定期取得を発動
		$(this).stopTime("disp",dispMentions);
		$(this).everyTime( __vals.time_mentions, "get" ,getMentions);
	}
}

/*
 	twitterでの反応定期更新 
 */
function getMentions() {
	
	var this_  = $(this);
	var lastList = this_.find("ul:first li:fast");
	
	if (lastList.size()<=0) return false;
	
	var lastStatus = Number(lastList.attr("class").replace("status",""));
	
	$.get(__webroot + "javascripts?", {
		"func": "get_twitter_reaction",
		"time": (new Date).getTime()
	},
	
	function(data) {
		
		var matches = data.match(/<ul>([\s\S]*)<\/ul>/);
		var html = matches[1]||"";
		var ul = this_.find("ul:first");
		var before_size = ul.find("li").size();
		
		html = html.replace(/[\t\n]+/g,"");
		
		// 取得済みStatusIDより若い物を削除
		if ( html != "" ) {
			ul.find("li:first").before(html);
			ul.find("li:hidden").each(function() 
			{
				var status = Number($(this).attr("class").replace("status",""));
				if (status <= lastStatus) {
					$(this).remove();
				}
			});
			
			if ( before_size<ul.find("li").size() ) {
				// 新規発言取得後にリスト要素が増えていたら表示機能発動
				this_.everyTime( __vals.time_dispmentions, "get", dispMentions);
			}
		}
	});	

}

/* 投稿削除 */

function delNote() {
	
	var noteId   = $(this).children(".hide_note_num").val();
	var redirect = ($("#redirect").size()>0) ? true : false ;
	
	if ( window.confirm( __vals.message_delete ) ) {	
		$.get( __webroot + "javascripts?",{
				"func":"del_note", "note_id":noteId
			},
			function(){if(redirect) window.location.href = __webroot;}
		);
		
		$(this).parents(".commentContainer").fadeOut("slow");
	}
	return false;
}

/* イイカモ */

function iikamoUp() {
		
	var score = $(this).parent().find("p.count span:first");
	var scoreVal = new Number(score.html());
	var noteId = $(this).parent().find("input.note_reply_id").val();
	
	score.html(scoreVal + 1);
	score.attr("class","")
	
	$(this).after('<p class="btn_after"><img src="'+__webroot+'images/btn_iikamo_check.gif" /></p>');
	$(this).remove();
	$.get( __webroot+"javascripts/" , { func : "update_iikamo", note_reply_id : noteId});
}


/* もっとよむ */

function getNotes(readmoreUrl) {
	
	var readBox = $("#btnMoreContainer");
	var parentForm = readBox.parent();
	var timerHtml = '<div id="js_timer" style="text-align:center;"><img src="'+ __webroot +'images/timergrey.gif" /></div>';
	
	// もっと読むボタンを読み込み中ボタンに書き換える
	readBox.html(timerHtml);
	
	$.get(readmoreUrl, function(data) {
		// 取得したHTMLを挿入
		parentForm.after(data);
		parentForm.remove();
	});
}

/*
	通知メッセージを表示(1回限り)
*/
function showNotify() {

	var notify   = $("#notify");
	
	if ( notify.size() > 0 && notify.val() != "" ) {
		$.notifyBar({
			html: notify.val(),
			delay: 3000,
			animationSpeed: "normal",
			jqObject : null,
			cls : "oauth"
		});
		notify.remove();
	};
}

/*
	サイドバーの長さに合わせてメイン領域を動的に変更
 */

function fitMainContentsArea() {
	
	var content    = $("#content");
	
	var naviBox    = content.find("div.pagenaviBlock:first");
	var naviHeight = naviBox.find("p:first").attr("offsetHeight");
	
	var dcBlock    = content.find("div.doubleColumnBlock:first");
	var mainArea   = dcBlock.find("div.maincontentsArea:first");
	
	var mainHeight = mainArea.attr("offsetHeight");
	var sideHeight = dcBlock.find("div.sidecontentsArea:first").attr("offsetHeight");
	var resizeHeight = 0;
	
	// サイドバー＋カモの高さとメイン領域の高さを比較
	if ((sideHeight + naviHeight) > mainHeight) {
		// サイドとメインの差分を計算してメインブロックの高さを調整
		resizeHeight += Math.abs(sideHeight + naviHeight - mainHeight);
		mainArea.css("min-height", mainHeight + resizeHeight + "px");
	}
}


/*
	削除アイコンとチェーンアイコンをマウスオーバー時のみ表示
	ふきだしに乗ったら灰色アイコン、アイコン上にきたら水色アイコン
 */

function dispUserBoxBtn() {
	
	var mainArea = $("#content div.maincontentsArea");
	var userBox  = mainArea.find("div.userBox, div.itemUserBox");
	
	// IE対処
	userBox.find("p.deleteBtnContainer span.text").css("visibility","hidden");
	
	userBox.live("mouseover", function() {
		var rollover = $(this).find("p.rollover, span.rollover");
		rollover.find("img, span.text").css({"visibility":"visible"});
	});
	
	userBox.live("mouseout", function() {
		var rollover = $(this).find("p.rollover, span.rollover");
		rollover.find("img, span.text").css({"visibility":"hidden"});
	});
}


