var u_name		= null;
var tasklist 	= null;
var calendar 	= null;
var mailclient	= null;
var mobile		= null;


$(function(){
});

window.onload = function(){
	update_genral_info();
	update_delete_button_visibility();
	$("form#commentsform").livequery(function(){ $(this).submit(post_comment); } );	
};

function update_genral_info()
{
	u_name		= $("#frm_customdisplayname").val();
	tasklist 	= $("#frm_tasklist").val();
	calendar 	= $("#frm_calendar").val();
	mailclient	= $("#frm_mailclient").val();
	mobile		= $("#frm_mobile").val();
}

function update_delete_button_visibility()
{
	// hiding the 'delete' button from the parent comment
	$('.comments .comment .del').css('display', 'inline');
	$('.comments .commentparentid').each(function(){
		if($(this).text() != '')
		{
			$('#commentid_' + $(this).text() + ' .del').css('display', 'none');
		}
	});
}

//this is for commenting
function post_comment(){
	
	dojo_form = dijit.byId("commentsform");
	
	if(dojo_form.isValid())
	{
		var text = $("#commentsform").serializeArray();
		$.php({
				type		: 'POST',
				url			: './new_jquery_comment.php',
				data		: text,
				dataType 	: "json",
				success		: function(returned_data){
								returned_data = $.parseJSON(returned_data.b);
								if(returned_data.status == 'success')
								{
									$div = $(returned_data.comment);
									$div.addClass('new_comment').css('display', 'none');
									$('#new_comment_pointer').after($div);
									
									window.location = "#comment_begins"; //putting the comment section on view
									
									$(".new_comment").slideDown("normal", function(){
										$(".new_comment").removeClass("new_comment");
									});
									
									$("#commenttitle").val("");
									$("#commentbody").val("");
									$("#commentsform input[name='parentid']").val("");
								}
								else
								{
									alert("sorry the comment was not posted!! \nplease try again later." + "\nPossible error:\n" + returned_data.error_msg);
								}
		
							} 
			});
		
		return false;
	}
	else
	{
		alert('Please correct the missing fields first.');
		return false;
	}	
}


//comment delete part starts from here.......
function new_comment_delete( obj_id ){	
	if( confirm("Are you sure you want to delete this comment?") ){
		var current_deleted_comment = obj_id;
		
		var text = $("#" + obj_id).serializeArray();
		
		$.php({
				type		: 'POST',
				url			: './new_jquery_comment.php',
				data		: text,
				dataType 	: "json",
				success		: function(returned_data){
								returned_data = $.parseJSON(returned_data.b);
								if(returned_data.status == 'success')
								{
									var deleted_div = $("div.comment:has(form[id='"+ current_deleted_comment +"'])");
									deleted_div.css("background-color", "#F03");
									deleted_div.fadeOut(500, function(){										
										deleted_div.remove();
										update_delete_button_visibility();
									}); 
								}
								else
								{
									alert("sorry the comment was not deleted!! \nplease try again later." + "\nPossible error:\n" + returned_data.error_msg);
								}
		
							}
			});
		

	}
	
	return false;
}





var coach_mode;
//comment update starts from here...............................................
function new_comment_update(c_id, coach){
	coach_mode 	= coach;

	//getting all the properties....
	var edited_div = $("div.comment:has(form[id='deletecommentform_"+ c_id +"'])");
	edited_div.addClass("edited");
	
	
	//hiding and showing the coach checkbox and brand according to the level...
	if(coach){
		$("#frm_coach_label").css("display", "inline");
		$("#frm_brand").css("display", "block");
	}else{
		$("#frm_coach_label").css("display", "none");
		$("#frm_brand").css("display", "none");
	}
	
	
	////////////////this is for branding//////////////////
	brand = $(".edited .commentbrand").text();
	brand = (brand != '') ? brand : 'none';
	$('#' + brand).attr("checked", true);

	
	var c_title = $(".edited h3").text();
	//removing the end (coach) and (private) text from the title
	if(edited_div.hasClass("privatecomment") && edited_div.hasClass("coachcomment")){
			len = c_title.lastIndexOf("(private)(coach)");
			c_title = c_title.substr(0, len );
			$("#frm_private").attr("checked", true);
			$("#frm_coach").attr("checked", true);
	}else if( edited_div.hasClass("privatecomment") ){
			len = c_title.lastIndexOf("(private)");
			c_title = c_title.substr(0, len );
			$("#frm_private").attr("checked", true);
			$("#frm_coach").attr("checked", "");
	}else if( edited_div.hasClass("coachcomment") ){
			len = c_title.lastIndexOf("(coach)");
			c_title = c_title.substr(0, len );
			$("#frm_private").attr("checked", "");
			$("#frm_coach").attr("checked", true);
	}else{
			$("#frm_private").attr("checked", "");
			$("#frm_coach").attr("checked", "");		
	}
	
	var c_name 		= $(".edited .commentuser").text();
	var c_body 		= $(".edited .commentbody").text();
	var c_section	= $(".edited .commentsection").text();
	var c_fold		= $(".edited .commentfold").text();
	var c_parentid	= $(".edited .commentparentid").text();
	var c_level 	= $(".edited .commentlevel").text();
	
	
	
	//setting all the properties.......
	$("#update_comment_box .box-title").text("Update Your Comment");
	if (coach){
		$("#frm_customdisplayname").val(c_name);
	}else{
		if( (c_name.indexOf("@") > -1) && ( c_name.indexOf(".") > -1 ) ){
			$("#frm_email").attr("checked", true);
		}else if( c_name.indexOf("Anonymous") > -1 ){
			$("#frm_anon").attr("checked", true);
		}else{
			$("#frm_real").attr("checked", true); 
		}
	}
	$("#frm_title").val(c_title);
	$("#frm_comment").text(c_body);
	$("#frm_commentid").val(c_id);
	$("#frm_expectcoach").val(coach);
	$("#frm_section").val(c_section);
	$("#frm_fold").val(c_fold);
	$("#frm_parentid").val(c_parentid);
	$("#frm_actiontype").val("update");
	$("#frm_tasklist").val("");
	$("#frm_calendar").val("");
	$("#frm_mailclient").val("");
	$("#frm_mobile").val("");
	$("#frm_current_level").val(c_level);
	$("#frm_update #frm_update_label").text("Update Comment");
	
	
	//showing the modal box...
	$("#update_comment_box").showModalBox();
	
	//when the update button is clicked.......
	$("#update_comment_box #frm_update").click(function(){
		
		//checking whether data is validated or not...
		if (coach){
			var l_name  = $("#frm_customdisplayname").val().length;
			if( l_name < 1){
				alert("Please fill in all the information..");
				return false;
			}
		}
		var l_title = $("#frm_title").val().length;
		
		var l_body	= $("#frm_comment").val().length;
		if( l_title < 1 || l_body < 1 ){
			alert("Please fill in all the information..");
			return false;
		}														
			
			
		var text = $("#update_comment_form").serializeArray();	
		$.php({
				type		: 'POST',
				url			: './new_jquery_comment.php',
				data		: text,
				dataType 	: "json",
				success		: function(returned_data){
								returned_data = $.parseJSON(returned_data.b);
								if(returned_data.status == 'success')
								{
									$edited_comment = $(returned_data.comment);
									$comment = $('#commentid_' + c_id);
									$comment.replaceWith($edited_comment);	
									
									$("#update_comment_box").removeModalBox(function(){
										$(".edited").removeClass("edited");
									});								
								}
								else
								{
									alert("sorry the comment was not edited!! \nplease try again later." + "\nPossible error:\n" + returned_data.error_msg);
								}
		
							}, 
			});												
				
	
		return false;
	});
	
	
	
	
	
	
	
	//when the cancel button is clicked........
	$("#update_comment_box #frm_cancel").click(function(){
		$("#update_comment_box").removeModalBox(function(){
			$(".edited").removeClass("edited");
		});
		
	});

	return false;
}




///////////////////////////////////// Comment Update Section Ends Here ////////////////////////////////////////////////////////
























///////////////////////////////////////////comment Process part starts from here ///////////////////////

var current_process_comment = null;
var process = null;


function new_comment_process( obj_id ){
	
		process 		= $("." +obj_id).attr("checked");
	
		current_process_comment = obj_id;
		var text = $("#" + obj_id).serializeArray();


		$.php({
				type		: 'POST',
				url			: './new_jquery_comment.php',
				data		: text,
				dataType 	: "json",
				success		: function(returned_data){
								returned_data = $.parseJSON(returned_data.b);
								if(returned_data.status == 'success')
								{
									var process_div = $("div.comment:has(form[id='"+ current_process_comment +"'])");
									if( window.location.toString().indexOf("allcomments=on") > -1 )
									{										
										if(process)
										{
											process_div.removeClass("unprocessedcomment");
											process_div.addClass("processedcomment");	
											$("#" + current_process_comment + " input[name = 'processed']").val("off");
										}
										else
										{
											process_div.addClass("unprocessedcomment");
											process_div.removeClass("processedcomment");			
											$("#" + current_process_comment + " input[name = 'processed']").val("on");
										}
													
									}
									else
									{
										process_div.css("background-color", "#0C6")
										process_div.fadeOut(500, function()
										{
											process_div.remove();
										}); 
								
										
										// this part is for removing the reply comment along with its parent comment when processing.
										var reply_div = $("div." + current_process_comment);
										reply_div.css("background-color", "#0C6")
										reply_div.fadeOut(500, function(){
											reply_div.remove();
										}); 
									}						
								}
								else
								{
									alert("sorry the comment was not processed!! \nplease try again later." + "\nPossible error:\n" + returned_data.error_msg);
									if(process){
										$("." +current_process_comment).attr("checked", "");
									}else{
										$("." +current_process_comment).attr("checked", true);
									}
								}		
							}, 
			});	
		
	return false;
}


/////////////////////////////////Comment Process ends here //////////////////////////////////////////////////



























/////////////////////////////////// Reply Comments Starts Here /////////////////////////////////////////////


function reply_comment(p_id, coach){
	coach_mode 	= coach;
	
	// chapter loading via ajax might overright previous info
	// so we are collecting the information again
	update_genral_info();
	
	//hiding and showing the coach checkbox and brand according to the level...
	if(coach){
		$("#frm_coach_label").css("display", "inline");
		$("#frm_brand").css("display", "block");
	}else{
		$("#frm_coach_label").css("display", "none");
		$("#frm_brand").css("display", "none");
	}
	
	//retrieving values 
	var section	= $("#commentid_" + p_id + " .commentsection").text();
	var fold 	= $("#commentid_" + p_id + " .commentfold").text();
	var brand	= $("#commentid_" + p_id + " .commentbrand").text();
	var c_level = parseInt($("#commentid_" + p_id + " .commentlevel").text()) + 1;
	var title	= $("#commentid_" + p_id + " h3").text();
	if( title.substr(0,4) != "Re: "){
		title	= "Re: " + title;
	}
	brand = (brand != '') ? brand : 'none';
	//changing the <h5> tag
	$("#update_comment_box .box-title").text("Reply to The Comment")
	
	
	//// setting the box empty or to the defaults.
	if (coach){
		$("#frm_customdisplayname").val(u_name);
	}else{
		$("#frm_real").attr("checked", true); 
	}
	
	$("#update_comment_box #frm_coach").attr("checked", ""); 
	$("#update_comment_box #frm_private").attr("checked", ""); 
	$('#update_comment_box #' + brand).attr("checked", true);
	$("#update_comment_box #frm_title").val(title);
	$("#update_comment_box #frm_comment").text("");
	$("#update_comment_box #frm_commentid").val("");
	$("#update_comment_box #frm_expectcoach").val(coach);
	$("#update_comment_box #frm_section").val(section);
	$("#update_comment_box #frm_fold").val(fold);
	$("#update_comment_box #frm_parentid").val(p_id);
	$("#update_comment_box #frm_actiontype").val("add");
	$("#update_comment_box #frm_tasklist").val(tasklist);
	$("#update_comment_box #frm_calendar").val(calendar);
	$("#update_comment_box #frm_mailclient").val(mailclient);
	$("#update_comment_box #frm_mobile").val(mobile);	
	$("#update_comment_box #frm_current_level").val(c_level);	
	$("#update_comment_box #frm_update #frm_update_label").text("Reply");
	
	//Creating the input hidden field for comment processing (For Admin/Coach Only)
	if(coach_mode){
		var process_field = $("<input type='hidden' id='frm_process' name='processed' value='1'>");
		$("#update_comment_form").append(process_field);
	}
	
	//showing the modal box...
	$("#update_comment_box").showModalBox();	
	
	
	
	
	
	
	
		
	//when the update button is clicked.......
	$("#update_comment_box #frm_update").click(function(){
		
		//checking whether data is validated or not...
		if (coach){
			var l_name  = $("#frm_customdisplayname").val().length;
			if( l_name < 1){
				alert("Please fill in all the information..");
				return false;
			}
		}
		var l_title = $("#frm_title").val().length;
		
		var l_body	= $("#frm_comment").val().length;
		if( l_title < 1 || l_body < 1 ){
			alert("Please fill in all the information..");
			return false;
		}														
														
														
		var text = $("#update_comment_form").serializeArray();
		$.php({
				type		: 'POST',
				url			: './new_jquery_comment.php',
				data		: text,
				dataType 	: "json",
				success		: function(returned_data){
								returned_data = $.parseJSON(returned_data.b);
								if(returned_data.status == 'success')
								{
									$new_comment = $(returned_data.comment);
									$new_comment.addClass('new_comment').css('display', 'none');
									
									$p_comment = $('#commentid_' + p_id);
									$p_comment.after($new_comment);	
									
									$("#update_comment_box").removeModalBox();
									
									$(".new_comment").slideDown("normal", function(){
										$(".new_comment").removeClass("new_comment");
										update_delete_button_visibility();
									});									
									
								}
								else
								{
									alert("sorry the comment was not posted!! \nplease try again later." + "\nPossible error:\n" + returned_data.error_msg);
								}
		
							}, 
			});	
		

	
		return false;
	});
	
	
	
	//when the cancel button is clicked........
	$("#update_comment_box #frm_cancel").click(function(){
		$("#update_comment_box").removeModalBox(function(){
			$("#frm_process").remove();									
		});
		
	});
	
	
	
	
	
	return false;
}






/////////////////////////////////// Reply Comments Ends Here /////////////////////////////////////////////

























