/************************************************************************
 * Copyright (C) 2009 Thomas Krehbiel
 * http://uvteksoftware.com, mailto:tom@uvteksoftware.com
 * Distributed under the terms of the GNU GPL version 3
 ************************************************************************/

/* Javascript for lists of posts with comment boxes */

var watermark; // can share because they are all the same

function focusComment(id)
{
	var q = $('#ctext'+id);
	if(q.attr('className') == 'inactivetext')
	{
		// save the watermark text generated by server
		watermark = q.val();
		q.val('');
	}
	q.attr('className','activetext');
	$('#cpost'+id).attr('className','activebutton');
}

function blurComment(id)
{
	var q = $('#ctext'+id);
	if( q.val() == '' )
	{
		$('#cpost'+id).attr('className','inactivebutton');
		q.attr('className','inactivetext');
		q.val(watermark);
	}
}

function resetComment(id)
{
	var q = $('#ctext'+id);
	q.attr('className','inactivetext');
	q.val('comment...');
	$('#cpost'+id).attr('className','inactivebutton');
}

function reloadComments(id)
{
	// Refresh the list of comments.
	var q = $('#postc'+id);
	$.get(mapPath('callback.php')+'?f=getpostcommentsummary&id='+id,
		null, 
		function(data){ q.html(data); },
		'text' );
	
	// Also update the post's dateline to show the new number of comments.
	var r = $('#postdateline'+id);
	$.get(mapPath('callback.php')+'?f=getpostdateline&id='+id,
		null, 
		function(data){ r.html(data); },
		'text' );
}

function postComment(id)
{
	var nameval = getCookie('commentname');
	if( nameval == null ) nameval = '';
	
	var commentval = $('#ctext'+id).val();
	if( commentval == '' ) return false;

	var q = $('#cstatus'+id);
  	q.html('<img src="'+staticPath('images/load_spinner.gif')+'" alt="" width="16" height="16" border="0" />');

    $.ajax({
  	  type:"POST",
  	  url:mapPath('callback.php')+'?f=postcomment',
  	  data:{
  	  	id: id,
  	  	name: nameval,
  	  	comment: commentval
    	},
      dataType:'html',
      cache:false,
      success:function(data){
          q.html('');
          reloadComments(id);
    		},
      error:function(XMLHttpRequest, textStatus, errorThrown){
    			q.html('<span class="error">'+XMLHttpRequest.responseText+'</span>');
    		}
    });
    
	return true;
}

