var image_id = 0;

jQuery(document).ready(function() {
	
	image_id = $('#image_comment_image_id').attr("value");
	
	$("#image_comment_name").focus( function() { 
			if ($(this).attr("value") && $(this).attr("value") != "Enter Name"){
				return false;
			} else {
				$(this).attr({ value: ""}); 
			}
		});
	$("#image_comment_email").focus( function() { 
			if ($(this).attr("value") && $(this).attr("value") != "Enter Email (not made public)"){
				return false;
			} else {
				$(this).attr({ value: ""}); 
			}
		});
	
	//Handle the Image Comment form
	var options = {
		beforeSubmit: validateImageComment,
		success: imageCommentProcessed,
		dataType: 'json'
		};
	$("#form_post_comment").ajaxForm(options);
	
	updateComments();
	
	
});

function validateImageComment(formData, jqForm, options){
	var form = jqForm[0]; 
	if (!form['image_comment[comment]'].value || !$.trim(form['image_comment[comment]'].value).length > 0) {
		alert("Please provide a Comment");
		form['image_comment[comment]'].focus();
		return false;
	}
	if ($.trim(form['image_comment[name]'].value) == 'Enter Name' || !form['image_comment[name]'].value || !$.trim(form['image_comment[name]'].value).length > 0) {
		alert("Please provide your Name");
		form['image_comment[name]'].focus();
		return false;
	}
	if ($.trim(form['image_comment[email]'].value) == 'Enter Email (not made public)' || !form['image_comment[email]'].value || !$.trim(form['image_comment[email]'].value).length > 0) {
		alert("Please provide your Email address");
		form['image_comment[email]'].focus();
		return false;
	}
}
function imageCommentProcessed(response, statusText) {
	$('#post_comment').empty();
	$('#post_comment').append(response.message);
	if (response.success == true){
		updateComments();
	}
}
function updateComments() {
	$('#user_comments').empty();
	$('#user_comments').append('<img align="top" alt="" src="../../images/ajax-loader.gif" /> Loading Comments');
	$('#user_comments').load("../../image_comment/list_html/?image_id=" + image_id);
}