
/*
 * Comment Form - FC2Prof
 *
 * Copyright (c) 2007
 * 
 * @Author : mizuki
 * @Created: 2007/10/29
 */
 
function sendComment()
{
	$(".comment_submit").attr("disabled","true");
	
	$.post(base_url + '&mode=send&' + Math.random(), {
		id   : $("input[@name=id]").val(),
		name : $("input[@name=name]").val(),
		body : $("textarea[@name=body]").val()
	}, 
	function(id){
		// save name
		if ($("input[@name=name]").val() != null)
		{
			$.cookie('form_name', $("input[@name=name]").val(), { expires: 7 });
		}
		
		if (isNaN(parseInt(id)))
		{
			if (id.match(/^p[0-9]+$/))
			{
				showForm(id, $("input[@name=name]").val());
			}
		}
		else{
			showForm(id, $("input[@name=name]").val());
		}
	});
	$(".comment_submit").attr("disabled","false");
	
}

function showForm(id, name)
{
	url = base_url + '&mode=display&process=list&item_id=' + id + '&' + Math.random();
	
	$.getJSON(url, function(json)
	{
		//var form_name = (name)? name : '';
		var form_name = ($.cookie('form_name') != null)? $.cookie('form_name') : '';
		var v = '';
		l = h = '';
		if (json)
		{
			$.each(json, function(i, n)
			{
				n = (this.url)? '<a href="' + this.url + '"><img src="' + this.icon + '" width="16" height="16" border="0" align="absmiddle" alt="' + this.name + '" /></a> ' : 
								'<img src="' + this.icon + '" width="16" height="16" border="0" align="absmiddle" alt="' + this.name + '" /> ';
				v = v + '<li>' + n + '<span title="' + this.date + '">' + this.name + '</span> : ' + this.body + '</li>\n';
			});
		}
		
		
		
		if (v != '') l = '<hr size="1" color="#cccccc"><ul>' + v + '</ul>';
		h   = l + '<br /><form method="post" action="' + base_url + '&mode=send" class="comment_form">' + 
				  '名前:<input type="text" name="name" size="20" value="' + form_name + '" class="comment_name" /><br /> ' + 
				  '<textarea name="body" cols="40" rows="4" class="comment_body"></textarea><br />' + 
				  '<input type="button" value=" コメントする " onClick="javascript:sendComment();" class="comment_submit" / >&nbsp;|&nbsp;' + 
				  '<a href="javascript:;" onClick="closeForm(\''+ id + '\');">フォームを閉じる</a><br />' + 
				  '<input type="hidden" name="id" value="' + id + '" />' + 
				  '<input type="hidden" name="profile_id" value="' + profile_id + '" />' + 
				  '</form>';

		setElement(id, h);
	});
}

function closeForm(id)
{
	$.getJSON(base_url + '&mode=display&process=count&item_id=' + id + '&' + Math.random(), function(json)
	{
		setElement(id, '<a href="javascript:;" onClick="javascript:showForm(\'' + id + '\');">コメントする : ' + json + '</a>');
	});
}

function setElement(id, h)
{
	var ele = 'comment_form' + id;
	
	// IE用
	if(document.all)
	{
		var p = document.all(ele);
		p.innerHTML = h;
	}
	// FireFox/ネスケ6用
	else if(document.getElementById)
	{
		var p = document.getElementById(ele);
		p.innerHTML = h;
	}
	// ネスケ用
	else if (document.layers)
	{
		var p = document.layers[ele];
		p.document.open();
		p.document.write(h);
		p.document.close();
	}
}
