/* Film comment functions. */

/**
 * commentSubmitID must be set in the template which calls these functions. It
 * should be the result of this statement from the controller:
 *
 * $this->generateSubmitID('/pageGroup/commentajax');
 */
jQuery(function($){
	$('#postForm #comment.default').toggleVal();
});

function ajaxPostComment(refID, elComment, elToSwap, parentId, skipChild) {

	if(jQuery(elComment).val() == "Start a conversation...") {
		comment = '';
	} else {
		comment = jQuery(elComment).val();
	}
	
	if(skipChild == 0) {
		skipChild = 0;
	}
	else {
		skipChild = 1;
	}

	//var skipChild = skipChild ? skipChild : true;
	var parent = parentId ? parentId :0;
	var url = siteURL + '/' + pageGroup + '/commentajax'

	jQuery.ajax({ url: url,
		data: {'action': 'post_comment', 'refID' : refID, 'comment': comment, 'submitID' : commentSubmitID, 'step' : '1', 'parentID' : parent, 'skipChildren' : skipChild},
		type: 'post',
		success: function (responseText) {

			// Mysterious IE7 bug with inserting response text forces manual js calls.
			//jQuery(elToSwap).html(responseText);
			document.getElementById(elToSwap.replace('#', '')).innerHTML = responseText;

			loadReplyFormData();
		}
	});

	jQuery(elToSwap).html(loadingGraphic);

	//trackClick(url);

}

function ajaxFetchCommentsList(page, refID, elToSwap, skipChild) {

	if(skipChild == 0)
		skipChild = 0;
	else
		skipChild = 1;

	//var skipChild = skipChild ? skipChild : true;
	var url = siteURL + '/' + pageGroup + '/commentajax'

	jQuery.ajax({ url: url,
		data: {'action': 'list_comments', 'refID' : refID, 'page' : page, 'skipChildren' : skipChild},
		type: 'post',
		success: function (responseText) {

			// Mysterious IE7 bug with inserting response text forces manual js calls.
			//jQuery(elToSwap).html(responseText);

			document.getElementById(elToSwap.replace('#', '')).innerHTML = responseText;

		}
	});

	jQuery(elToSwap).html(loadingGraphic);

	//trackClick(url);

}

function ajaxDeleteComment(refID, commentID, elToSwap, skipChild) {

	if(skipChild == 0)
		skipChild = 0;
	else
		skipChild = 1;

	//var skipChild = skipChild ? skipChild : true;
	var url = siteURL + '/' + pageGroup + '/commentajax'

	jQuery.ajax({ url: url,
		data: {'action': 'delete_comment', 'refID' : refID, 'commentID' : commentID, 'submitID' : commentSubmitID, 'step' : '1', 'skipChildren' : skipChild},
		type: 'post',
		success: function (responseText) {

			// Mysterious IE7 bug with inserting response text forces manual js calls.
			//jQuery(elToSwap).html(responseText);
			document.getElementById(elToSwap.replace('#', '')).innerHTML = responseText;
			
			loadReplyFormData();
		}
	});

	jQuery(elToSwap).html(loadingGraphic);

	//trackClick(url);

}

function loadReplyFormData() {	
	jQuery(function($){
		$('#postForm #comment.default').toggleVal();
		$('#discussComments .discussThread').bind('showReplyForm', function(){
			$(this).children('.placeholderReplyForm').hide();
			$(this).children('.postReplyForm').show().find('textarea').focus();
		});
		$('.placeholderReplyForm').click(function(){
			if (typeof promptLoginURL == 'string') {
				promptLogin(promptLoginURL);
			} else {
				$(this).parents('.discussThread').trigger('showReplyForm');
			}
		});
		$('.postReplyLink').click(function(){
			if (typeof promptLoginURL == 'string') {
				promptLogin(promptLoginURL);
			} else {
				$(this).parents('.discussThread').trigger('showReplyForm');
			}
		});
		
	});
}
