$(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;
	var modes = new Array();
	$("address").before('<div id="relateds"><h2>関連</h2><button id="relateds_toggle" class="toggle">×</button><ul id="related_list"></ul></div>');
	$("link").not("link[@rel='alternate']").not("link[@rel='stylesheet']").each(function(){
		var active = new Image();
		active.src = "/images/"+mode+".active";
		var mode = $(this).attr("rel") ? $(this).attr("rel") : $(this).attr("rev");
		modes.push(mode);
		$("#related_list").append('<li><a href="'+$(this).attr("href")+'" rel="'+mode+'"><img src="/images/'+mode+'" alt="'+mode+": "+$(this).attr("title").replace(/&/g,"&#38;").replace(/</g,"&#60;").replace(/>/g,"&#62;").replace(/\"/g,"&#34;")+'" width="24" height="24" /></a></li>');
	});
	if($(".hentry").parent("html").size() == 1 && $(".not_found").size() == 0) {
		$("#related_list").append('<li><img src="/images/reply" alt="コメントをする" width="24" height="24" id="comment_icon" /></li>');
	}
	modes.push("reply");
	var width = $("#relateds").innerWidth();
	$("#related_list img").each(function(i){
		$(this).mouseover(function(event){
			$(this).attr("src","/images/"+modes[i]+".active");
			$('<div id="related_popup">'+$(this).attr("alt").replace(/&/g,"&#38;").replace(/</g,"&#60;").replace(/>/g,"&#62;").replace(/\"/g,"&#34;")+'</div>').appendTo("#relateds");
			$("#related_popup").css("width", width);
		});
		$(this).mouseout(function() {
			$(this).attr("src","/images/"+modes[i]);
			$("#related_popup").remove();
		});
	});
	$("#relateds_toggle").toggle(function(){
		$("#related_list").hide("slow");
		$("#related_comment_form").hide("slow");
		$("#relateds_toggle").text("□");
	}, function(){
		$("#related_list").show("slow");
		$("#related_comment_form").show("slow");
		$("#relateds_toggle").text("＿");
	});
	//$("#relateds").draggable();
	$("h1").css("padding-right", $("#relateds").width()+10);
	$("#comment_icon").toggle(function(){
		$("#related_comment_form").remove();
		$("#relateds").append('<form method="post" action="'+window.location.pathname+'" id="related_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></form>');
		$(".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();
		$("#related_popup").remove();
	}, function(){
		$("#related_comment_form").remove();
	});
});