$(function () {

 	$('.submit_comment').livequery('click', function(e) {
		var gossip = $(this).attr('gid');
		e.stopImmediatePropagation();
		if( $("input#name" + gossip).val() == "") {alert("Name is required!");return false;}
		if( $("textarea#comment" + gossip).val() == "") {alert("A comment is required!");return false;}	
		$("#comment_button" + gossip).attr("disabled","true").attr("value", "Submitting comment...");		
		var queryString = $('#gossip_comment_form' + gossip).formSerialize();
		queryString = queryString + '&sid=' + Math.random();
		$.ajax({
			type: 'POST',
			cache: false,
			url: '/includes/process_gossip_comment.php',
			data:  queryString,
			success: function(results) {
				if(results == 'success') {
						$('#contents' + gossip).html('<p>Ripper!  Your comment has been submitted!</p><p><em>All comments are moderated before inclusion &minus; there will be a delay before comments appears on the page</em></p>');
					} else if (results == 'pass') {
						$('#contents' + gossip).html('<p>Comment submitted</p>');										
					} else if (results == 'words') {
						$('#contents' + gossip).html('<p style="color:#FF0000;font-weight:bold;background-color:#FFFFFF;padding:10px;">Crikey!  You have exceeded the number of allowed words.  Refresh page to try again!</p>');										
					} else {
						$('#contents' + gossip).html('<p>Crikey!  Comment submission error!</p>');				
				}
			}
		});
		return false;
	});	
	
	$('.post_comment').livequery('click', function(e){
		var gossip = $(this).attr('gid');	
		$("#comment_form" + gossip).slideToggle(600);	
		$(this).text($(this).text() == 'Hide comment form' ? 'Comment on this news item' : 'Hide comment form');
		return false;
	});		
	$('.show_comments').livequery('click', function(e){
		var gossip = $(this).attr('gid');
		$("#commentlist" + gossip).slideToggle(600);	
		$(this).text($(this).text() == 'Hide comments' ? 'Show comments' : 'Hide comments');
		return false;
	});	

	$('.morecomments').livequery('click', function(e){
		var gossip = $(this).attr('gid');
		var shown = $(this).attr('shown');
		var total = $(this).attr('total');
		$.ajax({
			type: 'GET',
			cache: false,
			url: '/includes/process_more_comments.php?gid=' + gossip + '&shown= ' + shown + '&total=' + total,
			success: function(msg) {
				if(msg == 'error') {
						$('#commentlist' + gossip).append('<p><em>Crikey! Script error - refresh page to try again</em></p>');
					} else {
						$('#commentlist' + gossip).append(msg);
				}
			}
		});
		$("a#" + shown).hide();
		return false;
	});

});