$(function() {
	$('#addcommentdialog').jqm({modal: true, overlay:40});
	
	$('.addcomment').click( function() {
		addcommentdialog=$('#addcommentdialog');
	
		$('#addcommentbutton', addcommentdialog).hide();
		$('#addcommentdisabled', addcommentdialog).show();
		$('#addcommentsubject',addcommentdialog).val('').focus();
		$('#addcommenttext',addcommentdialog).val('');
		$('#addcommentsuccess', addcommentdialog).hide();
		$('#addcommentfailure', addcommentdialog).hide();
		$('#addcomment-fields', addcommentdialog).show();
		
		addcommentdialog.jqmShow();

		return false;
	}
	);
	
	$('#addcommentsubject').keyup(enableCommentSubmit);
	$('#addcommenttext').keyup(enableCommentSubmit);
	
	$('#addcommentbutton').click( function() {
		addcommentdialog=$('#addcommentdialog');
		
		$('#addcommentfailure', addcommentdialog).hide();
		$('#addcommentsuccess', addcommentdialog).hide();
		
		if (''==$('#addcommenttext', addcommentdialog).val())
		{
			return;
		}
		
		$('#addcommentbutton', addcommentdialog).hide();
		$('#addcommentdisabled', addcommentdialog).show();
	
		$.post("index.php", {inpSubject: $('#addcommentsubject').val(), inpComment: $('#addcommenttext').val(), inpContentID: $('#commentformcontentid').val()}, function(xml)
		{
			if (1==parseInt($('Success', xml).text(), 10))
			{
				$('#addcomment-fields', addcommentdialog).hide();
				$('#addcommentsuccess', addcommentdialog).show();
			}
			else
			{
				$('#addcommentfailure', addcommentdialog).show();
			
				$('#addcommentdisabled', addcommentdialog).hide();
				$('#addcommentbutton', addcommentdialog).show();
			}
		});
		
		return false;
	}
	);
	
	$('.togglecomments').click( function() {
		comments=$('#comments');
		
		if ($('ul', comments).is(':visible'))
		{
			$('ul', comments).slideUp('fast');
			$('.togglecomments', comments).text('Show Comments');
			$('#bottomcommentcontrollinks').hide();
		}
		else
		{
			$('ul', comments).slideDown('fast');
			$('.togglecomments', comments).text('Hide Comments');
			$('#bottomcommentcontrollinks').show();
		}
		
		return false;
	}
	);
}
);


function enableCommentSubmit()
{
	addcommentdialog=$('#addcommentdialog');

	if (''!=$('#addcommentsubject' ,addcommentdialog).val() && ''!=$('#addcommenttext' ,addcommentdialog).val())
	{
		$('#addcommentdisabled', addcommentdialog).hide();
		$('#addcommentbutton', addcommentdialog).show();
	}
	else
	{
		$('#addcommentbutton', addcommentdialog).hide();
		$('#addcommentdisabled', addcommentdialog).show();
	}
};
