/* 
 * Expects variables: 
 * wait_img_url: the img to show while comment is being posted
 * ajax_post_comment_url
 * ajax_get_comment_html_url
 * comment_add_before: true to prepend new comments, false to append to the end
 */
var previewed; 
var commentBusy; 

$(document).ready(function() {
	previewed = false; 
	commentBusy = false;
});

function show_reply_form(comment_id) {
	// show the form 
	var reply_form = $('#' + comment_id).children('.comment_reply'); 
	reply_form.show('normal');
	
	// change the retort link to hide
	var reply_link = $('#' + comment_id + ' .threaded_button'); 
	reply_link.text('hide'); 
	reply_link.attr("href", "javascript:hide_reply_form('" + comment_id + " ')"); 
}
function hide_reply_form(comment_id) {
	// show the form 
	var reply_form = $('#' + comment_id).children('.comment_reply'); 
	reply_form.hide('normal');
	
	// change the retort link to hide
	var reply_link = $('#' + comment_id + ' .threaded_button'); 
	reply_link.text('retort'); 
	reply_link.attr("href", "javascript:show_reply_form('" + comment_id + " ')"); 
}

function commentvote(id, direction) {
	$.post('/films/comments/'+id+'/'+direction+'vote/', {HTTP_X_REQUESTED:'XMLHttpRequest'},
		function(data) {
			if (data.success == true) {
				$('#commentscore' + id).text(data.score.score);
				
				if(direction == 'up') {
					$('#linkuparrow' + id).attr('src', '/site_media/img/thumbupgray.png');
					$('#commentup' + id).attr('href', 'javascript:void(0)');
					
					$('#linkdownarrow' + id).attr('src', '/site_media/img/thumbdown.png');
					$('#commentdown' + id).attr('href', 'javascript:commentvote(' + id +', \'clear\')');
				}
				
				if(direction == 'down') {
					$('#linkdownarrow' + id).attr('src', '/site_media/img/thumbdowngray.png');
					$('#commentdown' + id).attr('href', 'javascript:void(0)');
					
					$('#linkuparrow' + id).attr('src', '/site_media/img/thumbup.png');
					$('#commentup' + id).attr('href', 'javascript:commentvote(' + id +', \'clear\')');
				}
				
				if (direction == 'clear') {
					$('#linkuparrow' + id).attr('src', '/site_media/img/thumbup.png');
					$('#commentup' + id).attr('href', 'javascript:commentvote(' + id +', \'up\')');
					
					$('#linkdownarrow' + id).attr('src', '/site_media/img/thumbdown.png');
					$('#commentdown' + id).attr('href', 'javascript:commentvote(' + id +', \'down\')');
				}
			} else { alert('ERROR: ' + data.error_message); }
		}, 
	'json');
}


function commentAjax(form_id) {
	
	var comment_form = $('#' + form_id); 
	var error_div = comment_form.children('.comment_error'); 
	
	// clear error div
	error_div.html('');
	error_div.css('display', 'none'); 
	
	if (commentBusy) {
		error_div.html('Your comment is currently in the process of posting.');
		error_div.fadeOut(2000);
		return false;
	}
	 
	var comment_text = comment_form.children('#id_comment').val();
	if ($.trim(comment_text) == ''){ 
		error_div.css('display', 'none'); 
		error_div.html('Please enter some text to post.');
		error_div.fadeIn(1000);	
		return false;
	}
	

	var comment = comment_form.serialize();
	var message_div = comment_form.children("#submit_message"); 
	var parent_id = comment_form.children('.hidden').children('#id_parent').val();
	
	
	// Add a wait animation and text
	message_div.css('display', 'none'); 
	message_div.html('\
		<img src="' + wait_img_url + '" alt="Please wait..." class="ajax-loader" />\
		One moment while the comment is posted. . .\
	');
	message_div.fadeIn(1000);
	
	commentBusy = true;
	
	// Use AJAX to post the comment.
	$.post(post_comment_url, comment, function(data) {
		commentBusy = false;
		
		// Remove the wait animation and message
		message_div.hide();
		
		if (data.status == "success") {
			commentSuccess(data, parent_id, message_div);
		} else {
			commentFailure(data, error_div);
		}
	}, "json");
	
	return false;
}

function commentSuccess(data, comment_parent, message_div) {  
	var content_type = $('#id_content_type').val();
  
	// Use AJAX to get the comment html
	var comment_html = ''; 
	$.post(get_comment_html_url, {
												'comment_id':data.comment_id, 
												'ctype':content_type
												}, function(html_data) {
		if (html_data.status == "success") {
			comment_html = html_data.html; 	
		} else { 
		  comment_html = "Error displaying comment. Please reload the page. ";
		}
	
		// clear all form comment fields 
	  	$('textarea[name=comment]').val('');
	  
		// add and show the new comment
		if (comment_parent &&  comment_parent != "") {
			var parent_div = $("#c" + comment_parent);
			parent_div.after(comment_html); 
			hide_reply_form('c' + comment_parent);
		}
		else {
			if (comment_add_before) {
				$('#comments').prepend(comment_html);
			}
			else {
				$('#comments').append(comment_html);
			}
		}
		
		var comment_div = $('#c' + data.comment_id); 
		comment_div.css('display', 'none');
	  comment_div.show(1000);
	  
		// show message 
		//message_div.css('display', 'none'); 
	  message_div.html('Thank you for your comment!');
		message_div.fadeOut(4000);
		
	}, "json"); 
}

function commentFailure(data, error_div) {
    error_div.html(data.error);
    error_div.fadeIn(1000);
}