//メンバー画像にマウスオーバーでツールチップを表示
function memberBallon(selector) {

	//要素を取得
	var elm = $(selector);

	//マウスオーバーで表示・非表示
	elm.hover(
		function(){$(this).find("span.memberInfoWrap").show();},
		function(){$(this).find("span.memberInfoWrap").hide();}
	);

	//マウスポジションからでツールチップのポジションを設定
	elm.mousemove(function(e){
		var ballon       = $(this).find("span.memberInfoWrap");
		var ballonHeight = ballon.innerHeight();
		ballon.css({
			left:(e.pageX - 25)  + "px",
			top:(e.pageY - ballonHeight - 5) + "px"
		});
	});
}

//ログインフォームの値が空ならコメントを表示する
function inputEmptyComment(selector) {
	var elm     = $(selector);
	var comment = elm.find("span");
	var input   = elm.find("input");

	//コメントの
	comment
		//位置調整
		.each(function(){
			var thisInput = $(this).next();
			var sukimaLeft = (thisInput.outerWidth({margin: true})-thisInput.width())/2;
			var sukimaTop  = (thisInput.outerHeight({margin: true})-thisInput.height())/2;
			$(this).css({
				position:"absolute",
				left:sukimaLeft + "px",
				top:sukimaTop + "px"
			});
		})
		//クリックしてコメント消してinputにフォーカス
		.click(function(){
			$(this)
				.hide()
				.next().focus();
		});

	//オンロードでinputのvalueが空ならコメントを表示
	input.each(function(){
		if($(this).attr("value")==""){
			$(this).prev().show();
		}
	});

	//inputにフォーカスしてコメント消す
	input.focus(function(){
		$(this).prev().hide();
	});

	//inputからフォーカス外れてvalueが空ならコメント表示
	input.blur(function(){
		if($(this).attr("value")==""){
			$(this).prev().show();
		}
	});
}

//コメントの三行以上は隠して、続きを読むをクリックで表示
function commentOpenClose(){
	var commentBody = $("div.commentBody");
	for(i=0;i<commentBody.length;i++){
		var commentBodyHeight = commentBody.eq(i).height();
		var commentBodyMaxHeight = commentBody.eq(i).css("maxHeight");
		var commentBodyInnerHeight = commentBody.eq(i).find(".commentBodyInner").height();
		if(commentBodyInnerHeight>commentBodyHeight+3){//+3は遊び
			commentBody.eq(i)
				.css({maxHeight:"none",height:commentBodyMaxHeight})
				.next()
					.append("<span class='commentMore'>続きを読む</span>")
					.children(".commentMore").toggle(
						function(){
							var targetElment = $(this).parent().prev();
							targetElment.animate({height:targetElment.children(".commentBodyInner").height() + "px"});
							$(this).text("閉じる");
						},
						function(){
							var targetElment = $(this).parent().prev();
							targetElment.animate({height:commentBodyMaxHeight});
							$(this).text("続きを読む");
						}
					);
		}
	}
}

//マイページのプロフィール詳細の表示・非表示
function button_memberDetail(){
	$("#button-memberDetail").toggle(
		function(){
			$("#memberDetail").slideUp();
			$(this).text("詳しいプロフィールを表示");
			return false;
		},
		function(){
			var memberDetail = $("#memberDetail");
			memberDetail.css({height:"auto",display:"block"});
			memberDetailHeight = memberDetail.height();
			memberDetail
				.css({height:"0px",display:"block"})
				.animate({height:memberDetailHeight+"px"});
			$(this).text("詳しいプロフィールを非表示");
			return false;
		}
	);
}

//実行
$(function(){
	inputEmptyComment("label.emptyComment");
	memberBallon("a.memberBallon");
	commentOpenClose();
	button_memberDetail();
});



