//// EVENT HELPERS
$(document).ready(function(){ 
	$('#preview_comment_submit').click(function(){						   
		previewComment();
		return false;
	});
	
	$('#view_all_comments .action, #view_comments').click(function(){						   
		$('#view_all_comments').hide();
		$('.comment_entry').slideDown();
		return false;			   
	});
});




//// AJAX HANDLERS

function userLoginCheck(p_nUserId){
	$('#no_comments').hide();
	
	l_axArguments = [];
	l_axArguments.data = "a=logincheck";
	InterviewAjax.makeAjaxCall("/ajax/ajax.php", l_axArguments, signInOrAddComment);
}


function flagComment(p_nCommentId){
	l_axArguments = [];
	l_axArguments.data = "a=flagComment&comment_id="+p_nCommentId;
	InterviewAjax.makeAjaxCall("/ajax/ajax.php", l_axArguments, onCommentFlagged);
}


function postComment() {
	f = document.getElementById('comment_box_form');
	var l_nItemId = f.elements['item_id'].value;
	var l_nItemType = f.elements['item_type'].value;
	var l_sCommentText = f.elements['comment_text'].value;
	
	var errors = validateComment(f);
		if(errors.length > 0)	{  
			$('#comment_errors').html(_xGlobal.printErrors(errors));
			$('.comment_preview').hide(400);
			return;
		}
		$('#comment_errors').html('');
		
	disableCommentForm();	
	
	l_axArgs = [];
	l_axArgs.data = "a=postComment&item_id="+l_nItemId+"&item_type="+l_nItemType+"&comment_text="+l_sCommentText;
	
	InterviewAjax.makeAjaxCall("/ajax/ajax.php", l_axArgs, onCommentPosted);
}


function previewComment() {
	f = document.getElementById('comment_box_form');
	var l_sCommentText = f.elements['comment_text'].value;

	var errors = validateComment(f);
		if(errors.length > 0)	{  
			$('#comment_errors').html(_xGlobal.printErrors(errors));
			$('.comment_preview').hide(400);
			return;
		}
		$('#comment_errors').html('');
		
	l_axArgs = [];
	l_axArgs.data = "a=previewComment&comment_text="+l_sCommentText;

	InterviewAjax.makeAjaxCall("/ajax/ajax.php", l_axArgs, onCommentPreviewed);
}




//// CALLBACKS

function onCommentPosted( p_sJsonResponse ) {
	var l_xResponse = eval("(" + p_sJsonResponse + ")");
	var l_sCommentHtml = l_xResponse.comment_html;

	l_xHolderRef = $('.comment_preview');

	$('.comment_preview, .comment_box_wrapper, #be_first_comment').hide();
	$('.comment_preview').html('');
	$(l_xHolderRef).after(l_sCommentHtml);
	$('.comment_entry:first').fadeIn(400);

	// Update listing
	if ($('#comments_listing').length) {
		l_sListing = $('#comments_listing').html();
		var l_asSplitListing = l_sListing.split(" ");
		var l_nShowing = parseInt(l_asSplitListing[1]);
		var l_nTotal = parseInt(l_asSplitListing[3]);

		l_nShowing += 1;
		l_nTotal += 1;
	
		l_sNewListing = 'Showing '+l_nShowing+' of '+l_nTotal+' Comments';
		$('#comments_listing').html(l_sNewListing);
	}
	
	resetCommentForm();
	enableCommentForm();
}


function onCommentPreviewed( p_sJsonResponse ) {
	//if it was empty no reason to go further	
	if(p_sJsonResponse == '') return;
	
	var l_xResponse = eval("(" + p_sJsonResponse + ")");
	var l_sCommentHtml = l_xResponse.comment_html;
	
	$('.comment_preview').fadeIn(400).html(l_sCommentHtml);
}

function onCommentFlagged( p_nId ) {
	l_xHolderRef = $("#comment_"+p_nId+" .comment_report");
	$(l_xHolderRef).removeAttr('onclick');
	$(l_xHolderRef).addClass("flagged");
	
}

function signInOrAddComment(p_sResponse){
	if (p_sResponse == "true") {
		$('#no_comments').hide();
		$('.comment_box_wrapper').slideDown(400);
		setTimeout(function(){$('#comment_text').focus()}, 500);
		
	} else {
		g_bOpenLoginFlag = true;
		$.scrollTo("#site_container_dropshadow", { duration:500, onAfter:function () { 
			$('#no_comments').hide();
			$('#signin_popup').load('/templates/popup_signin.php',openSignIn); } });
	}	
}



//// MISC/UTILITY



/*$('.add_comment').click(function(){
	$('#comments_module').addClass("transparent");							 
	$('#comment_signin').slideDown(400);	
	
	$('#share_popup').slideUp(400);
	$('#email_popup').slideUp(400);
});*/

function disableCommentForm(){
	f = document.getElementById('comment_box_form');
	if(f){
		f.elements['comment_text'].disabled = true;
	}
	submitButton = document.getElementById('post_comment_submit');
	if(submitButton){
		submitButton.disabled = true;
	}
	$('#post_preview').removeAttr('onclick');
}

function enableCommentForm(){
	f = document.getElementById('comment_box_form');
	if(f){			
		f.elements['comment_text'].disabled = false;	
	}
	submitButton = document.getElementById('post_comment_submit');
	if(submitButton){
		submitButton.disabled = false;		
	}
}

function resetCommentForm(){
	enableCommentForm();
	f = document.getElementById('comment_box_form');
	if(f){
		f.elements['comment_text'].value = "";
	}
}

function validateComment(p_aValues){
	r_asErrors = new Array();			
	if(/[-a-zA-Z0-9]+/.test(p_aValues.elements['comment_text'].value) == false)
		r_asErrors.push("Please enter a comment.");	
		
	return r_asErrors;
}

