//show/hide box
function showHideBox(id, ex) {
	
	var selection = '';
	if(ex != undefined) {
		selection = ':not(:first)';
	}
	
	if($('#commentBox'+id).is(':visible')) {
		$('#commentBox'+id).slideUp("normal", function() {
			$("#commentIcon"+id).attr('src', '/data/images/icon_plus.jpg');
		});
	}
	else {
		$(".comments:not(#commentIcon"+id+")"+selection).slideUp("normal", function() {
			var thisID = $(this).attr('id').substr(10);
			$("#commentIcon"+thisID).attr('src', '/data/images/icon_plus.jpg');
		});
		$('#commentBox'+id).slideDown("normal", function() {
			$("#commentIcon"+id).attr('src', '/data/images/icon_minus.jpg');
		});
	}
}

//set textarea focus
$(document).ready(function() {
	$(".replyTextarea").focus(function() {
		if($(this).val() == defaultReplyText) {
			$(this).val('');
		}
	});	
	$(".replyTextarea").blur(function() {
		if($(this).val() == '') {
			$(this).val(defaultReplyText);
		}
	});
	$("#searchText").focus(function() {
		if($(this).val() == defaultSearchText) {
			$(this).val('');
		}
	});	
	$("#searchText").blur(function() {
		if($(this).val() == '') {
			$(this).val(defaultSearchText);
		}
	});

	$(".close").click(
		function () {
			$(this).parent().fadeTo(400, 0, function () { // Links with the class "close" will close parent
				$(this).slideUp(400);
			});
			return false;
		}
	);

});

//add reply
function addReply(module, id) {
	var reply = $("#replyText").val();
	if(reply == defaultReplyText) {
		reply = '';
	}
	
	$.post("/modules/"+module+"/ajax/addReply.php", { reply: reply, id: id },
	   function(data){		
	    if(data.status == 'complete') {
	    	$("#allReply").append(data.div);
	    	$("#replyText").val(defaultReplyText);
	    }
	    else if(data.status == 'login_error') {
	    	jQuery.facebox("Your session has expired. Please sign in and try again.");
	    }
	    else if(data.status == 'add_error') {
	    	jQuery.facebox("An error occurred when adding the comment.");
	    }
	  }, 
	  "json"
	);
}

//delete reply
function deleteReply(module, id) {
	$.post("/modules/"+module+"/ajax/deleteReply.php", { id: id },
	   function(data){
	    if(data.status == 'complete') {
	    	$("#replyN"+id).fadeTo("slow",0.01).slideUp("slow");
	    }
	    else if(data.status == 'login_error') {
	    	jQuery.facebox("Your session has expired. Please sign in and try again.");
	    }
	    else if(data.status == 'fail') {
	    	jQuery.facebox("An error occurred when deleting the comment.");
	    }
		
	   }, 
	   "json"
	);
}

//report forum
function reportForum(module, id, type) {
	$.post("/modules/"+module+"/ajax/reportAbuse.php", { id: id, type: type },
	   function(data){
	    if(data.status == 'complete') {
	    	jQuery.facebox("Thanks for your report abuse.");
	    }
	    else if(data.status == 'login_error') {
	    	jQuery.facebox("Your session has expired. Please sign in and try again.");
	    }
	    else if(data.status == 'fail') {
	    	jQuery.facebox("An error occurred when deleting the comment.");
	    }
		
	   }, 
	   "json"
	);
}
