$(function() {
	// Load
	$("#comments_container #comments_entries").load("/index.php/competitions/showcomments/" + competitionId + "/" + Math.random(),
		function() {
			$("#comments_ajaxwait").hide();
			$("#comments_content").show();
		}
	);

	// Submit
	$("#comments_form input[type='submit']").click(
		function() {
			$('#comments_form input').attr('disabled', 'true');
			$('#comments_sysmsg').attr('class', 'sysmsg_true').show().html('Posting your comment...');

			$.post('/index.php/competitions/postcomment/' + competitionId, 'comment=' + document.getElementById('comments_input').value,
				function(msg) {
					result = (msg.indexOf('competitions_comments:true') >= 0) ? true : false;
					$('#comments_sysmsg').attr('class', 'sysmsg_' + result).show().html(msg);
					$("#comments_entries").load("/index.php/competitions/showcomments/" + competitionId + "/" + Math.random());
					if(result) {
						$('#comments_sysmsg').fadeOut(3000,
							function() {
								$(this).html('').hide(); // empty and hide
								$('#comments_form').slideToggle('fast'); // slide up form
							}
						);
					}
				}
			);
			$('#comments_form input').removeAttr('disabled'); // enable buttons
 			return false;
 		}
 	);

	// Toggle and reset form
	// Slide comments form up and down
	$("#comments_container a.dhtmlLink, #comments_form input[type='reset']").click(
		function() {
			$("#comments_container a.dhtmlLink").toggleClass('arrowOn');
			$("#comments_form").slideToggle("fast");
			$("#comments_input").val('');
			$("#comments_sysmsg").html('').hide(); // clear system message
			return false;
		}
	);
});