﻿$(function(){
	var max = 0;
	if(document.documentElement.scrollWidth) max = document.documentElement.clientWidth || document.body.clientWidth || document.body.scrollWidth;
	else if(window.innerWidth) max = window.innerWidth;
	$(".entry-meta").each(function(i){
		var reply_uri = $(".entry-reply a", $(this)).attr("href");
		var reply_title = $(".entry-reply a", $(this)).text();
		var entry_uri = $(".entry-uri a", $(this)).attr("href");
		$(".entry-reply", $(this)).html('<span class="popup-label">'+reply_title+'</span>');
		$(".entry-reply span", $(this)).hover(function(){
			$(this).css("cursor","pointer").css("color","#66f");
			window.defaultStatus = "選択するとこの投稿のレスを表示します。";
		}, function(){
			$(this).css("cursor","auto").css("color","#00f");
			window.defaultStatus = "";
		});
		$(".entry-reply span", $(this)).click(function(){
			$(".popup").remove();
			$(this).parent().append('<div id="reply_popup" class="popup"><p>このエントリへのリプライを読み込んでいます……</p></div>');
			var position = $(this).position();
			if(max > 0 && position.left > max * 3/4) 
				$("#reply_popup").css("right", (max - position.left - 10)+"px").css("left", "auto");
			else 
				$("#reply_popup").css("left", (position.left - 10)+"px").css("right", "auto");
			$.getJSON(reply_uri+".json", function(json) {
				if(json.count == '0') $("#reply_popup").html('<button id="reply_toggle" class="toggle">×</button><p id="no_reply">まだリプライはありません。</p>');
				else {
					$("#reply_popup").html('<button id="reply_toggle" class="toggle">×</button><dl class="comment"></dl>');
					for(i in json.items) {
						var each = json.items[i];
						if(each.type == "comment")
							$("#reply_popup dl").append('<dt id="D'+each.num+'">'+each.num+': <span class="nickname">'+each.name+'</span> : '+each.date+'</dt><dd>'+each.body+'</dd>');
						else
							$("#reply_popup dl").append('<dt id="D'+each.num+'">'+each.num+': <a rel="nofollow" href="'+each.uri+'">'+each.name+'</a> : '+each.date+'</dt><dd>'+each.body+'</dd>');
					}
				}
				$("#reply_toggle").click(function(){
					$("#reply_popup").remove();
				});
				$("#related_comment_form").remove();
				$("#reply_popup").append('<form method="post" action="'+reply_uri+'" id="reply_comment_form" class="comment_form"><fieldset><legend>コメントを書く</legend><ul><li><label for="name">名前: </label><input type="text" class="check" name="name" size="24" value="名無しさん" id="name" /></li><li><label for="body">本文: </label><textarea name="body" class="must check" rows="5" cols="24" id="body">255字まで。タグは使えません。</textarea></li><li>投稿: <input type="submit" value="投稿" /></li></ul></fieldset><fieldset><legend>トラックバックを送る</legend><ul><li><label for="tb">この記事へのトラックバックURI</label>: <input type="text" size="48" id="tb" readonly="readonly" value="http://txqz.net'+entry_uri+'/tb" /></li><li>txqz.net内へのアンカーがないエントリからのトラックバックは受信しません。</li></ul></fieldset></form>');
				$("#reply_comment_form input[@type='submit']").click(function(){
					var name = $("#name").val();
					var body = $("#body").val();
					$("#reply_comment_form").remove();
					$("#reply_popup").append('<p id="reply_message" class="message">投稿しています……</p>');
					$.post(reply_uri+".json", {
						 "name" : name
						,"body" : body
					},function(data){
						result = eval("("+data+")");
						if($("#reply_popup dl").size() == 0) {
							$("#no_reply").remove();
							$("#reply_toggle").after('<dl class="comment"><dt id="D'+result.num+'">'+result.num+': <span class="nickname">'+result.name+'</span> : '+result.date+'</dt><dd>'+result.body+'</dd></dl>');
						} else {
							$("#reply_popup dl").append('<dt id="D'+result.num+'">'+result.num+': <span class="nickname">'+result.name+'</span> : '+result.date+'</dt><dd>'+result.body+'</dd>');
						}
						$("#reply_message").html("投稿に成功しました。<button id='exit_button'>閉じる</button>");
						
						$("#exit_button").click(function(){
							$("#reply_popup").remove();
						});
						$(".popup-label", $("#reply_popup").parent("li")).text($(".comment dt").size()+"件のリプライ");
					});
					return false;
				});
				$(".check").each(function(){
					$(this).focus(function(){
						if(this.value == this.defaultValue) this.value = "";
					});
					$(this).blur(function(){
						if(this.value == "") this.value = this.defaultValue;
					});
				});
				$("#body").get(0).focus();
			});
		});
	});
});
