
// Current comments page in global variable
var curpage = 1;

// Preload image spinner
var spinner = new Image();
spinner.src = "/lib/img/spinner.gif";

// Fetch a page of comments and insert into page
function getComments(itemid,pageno,numperpage) {
	var oldcomments = $('#comments').html();
	$('#commentblocks').html('<div class=\"spinner\" style=\"height:'+$('#commentblocks').height()+'px\">Getting page '+pageno+'</div>');
	$.getJSON('/lib/ajx/comments',{itemid:itemid,pageno:pageno,numperpage:numperpage},
		function(json) {
			if (json.errmsg) {
				$('#comments').html(oldcomments);
				alert(json.errmsg);
			} else {
				$('#commentblocks > .spinner').remove()
				$('#commentblocks').html('');
				for (i=0;i<json.numcomments;i++) {
					$('#commentblocks').append("<table class=\"pagecomment\"><tr class=\"comnamedate\"><td class=\"commentauthor\">"+decodeURIComponent(json.comments[i].authorname)+"</td><td class=\"commentdate\">"+decodeURIComponent(json.comments[i].datesubmitted)+"</td></tr><tr><td colspan=\"2\">"+decodeURIComponent(json.comments[i].title)+"</td></tr><tr><td class=\"commentcontent\" colspan=\"2\">"+decodeURIComponent(json.comments[i].content)+"</td></tr></table>");
				}
				$('#commentschoosepage span.comcurpage').removeClass('comcurpage').addClass('comdiffpage');
				$('#commentschoosepage span:nth-child('+(pageno+1)+')').removeClass('comdiffpage').addClass('comcurpage');
				curpage = pageno;
			}
		});
	return true;
}

// Submit a comment
function submitComment(itemid,numperpage) {
	$('#commentvalidation').remove();
	var postdata = new Object();
	$('#submitcommentform input, #submitcommentform textarea').each(function(){
		if ($(this).attr("type") != 'submit') postdata[$(this).attr("name")] = $(this).val();
	});
	var oldform = $('#commentform').html();
	$('#commentform').html('<div class=\"spinner\" style=\"height:'+$('#commentform').height()+'px\">Submitting your comment</div>');
	$.post('/lib/ajx/submitcomment',postdata,function(response) {
		var json = eval('('+response+')');
		if (json.result == 'ok' && json.cstatus == 'moderated') {
			$('#commentform').html('<h2>Thanks!</h2><p>Your comment was posted successfully, and is awaiting moderation.  It will be made live on the site as soon as moderation is complete.</p>');
		} else if (json.result == 'ok') {
			$('#commentform').html('<h2>Thanks!</h2><p>Your comment was posted successfully.</p>');
		} else if (json.result == 'fail') {;
			$('#commentform').html(oldform);
			$('#commentform').children('h1:first').after('<div id=\'commentvalidation\' class=\'alert\'><p>Unfortunately there were the following errors procesing your comment.  Please correct them and try again:</p><ul><li>'+json.errors.join('</li><li>')+'</li></ul></div>');
		} else {
			// TODO:WV:20070914:Link 'get in touch'
			$('#commentform').html('<h2>Sorry</h2><p>Unfortunately there was a problem submitting your comment.  Please <a href=\"javascript:void(0);\" onclick=\"window.location.reload();\">try again</a>, and if this problem persists, <a href=\"/content/contact-us/\">get in touch</a>.</p>');
		}

		// Repopulate form
		for (postvar in postdata) {
			$("#commentform input,#commentform textarea").each(function() {
				if($(this).attr("name") == postvar) $(this).val(postdata[postvar]);
			})
		}

		// Add a comments section if there were no comments before
		if (json.result == "ok" && json.cstatus == "public" && json.numtotal == 1) {
			$('#comments').before('<a name=\'comments\'></a><h1 id=\"commenthead\">Comments</h1><div id=\'comments\'><div id=\'commentblocks\'></div></div>');
		}

		// Create an additional page link if necessary
		var numlinks = $('#commentschoosepage > span').size();
		if (numlinks > 0 && json.numpages > numlinks ) {
			if (numlinks) $('#commentschoosepage').append('<span class=\'comdiffpage\'><a href=\'/content/'+json.tag+'/?compage='+json.numpages+'#comments\' onclick=\'return !getComments('+itemid+','+json.numpages+','+numperpage+')\'>'+json.numpages+'</a></span>');
			else $('#commentblocks').before('<div id=\'commentschoosepage\'><span class=\'comcurpage\'><a href=\'/content/'+json.tag+'/?compage=1#comments\' onclick=\'return !getComments('+itemid+',1,'+numperpage+')\'>1</a></span><span class=\'comdiffpage\'><a href=\'?/content/'+json.tag+'/?compage=2#comments\' onclick=\'return !getComments('+itemid+',2,'+numperpage+')\'>2</a></span></div>');
		}

		// Refresh first page if user is currently viewing it and there were no validation errors
		if (curpage == 1 && json.result=='ok') getComments(itemid,1,numperpage,100);

		});
	return true;
}
