function init()
{
	if ($('commentcontent'))$('commentcontent').observe('focus', clear);
	if ($('commentform'))$('commentform').observe('submit', validateComment);	
	if ($$('li.comment') && $$('li.comment').size() > 0)$$('li.comment').each(regCommand);
}
function regCommand(item)
{
	item.select('a.command').each(function(link){
		link.observe('click', commentClicked);
	})	
}
function validateComment(event) {
  var validated = true;
  var msg = '';
  if($('commentcontent').value.empty())
  {
  	msg = 'The comment could not be empty!';
  	validated = false;
  }
  else if($('commentcontent').value.toLowerCase() == 'please input your comments here!')
  {
  	msg = 'Useless comment is not welcome :)';
  	validated = false;
  }
  if(!validated)
  {
  	warnMessage($('commentcontent'), msg);
  	Event.stop(event);
  }
}
function clear(event)
{
	if ($('commentcontent')._cleared)return;
  	$('commentcontent').clear();
  	$('commentcontent')._cleared = true;
}

function warnMessage(sender, msg)
{	
	var warn = document.createElement('div');
	warn.addClassName('warning');
	warn.appendChild(document.createTextNode(msg));	
	sender.parentNode.appendChild(warn);		
}
function commentClicked(event){
	var element = Event.element(event);
	var li = element.parentNode.parentNode;	// li element
	var cid = li.id.split('_')[1];
	var action = element.innerHTML;
	var url = '/rpc?action='+action+'&target=comment&id='+cid;
	
	// notice the use of a proxy to circumvent the Same Origin Policy.
	new Ajax.Request(url, {
  		method: 'get',
  		onSuccess: commentClickedDone
	});
}
function commentClickedDone(transport)
{
	result = transport.responseText.evalJSON();
	if (result.success || result.success.toLowerCase()=='true')
	{
		var id = result.target+'_'+result.id;
		if($(id)) $(id).remove();
	}
}
function codeAddClicked(event){
	var element = Event.element(event);
	var action = element.innerHTML;
	var url = '/rpc?action=add&target=codefullname';
	// notice the use of a proxy to circumvent the Same Origin Policy.
	new Ajax.Request(url, {
  		method: 'get',
  		onSuccess: codeAddClickedDone
	});
}
function codeAddClickedDone(transport)
{
	result = transport.responseText.evalJSON();
	if (result.success || result.success.toLowerCase()=='true')
	{
		alert("Add Code Successful");
	}
	else
	{
		alert("Add Code Failed");
	}
}