//show login in header
function showLogin() {
	$("#searchBox").fadeOut("normal", function() {
		$("#profileForm").slideDown("normal");
		$("#prB1").hide();
		$("#prB2").show();
	});
}

//hide login in header
function hideLogin() {
	$("#profileForm").slideUp("normal", function() {
		$("#prB2").hide();
		$("#prB1").show();
		$("#searchBox").fadeIn("normal");
	});
}


//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');
			$.scrollTo('#commentBox'+id, 1000);
		});
	}
}

//set textarea focus
$(document).ready(function() {
	$(".commentTextarea").focus(function() {
		if($(this).val() == defaultCommentText) {
			$(this).val('');
		}
	});	
	$(".commentTextarea").blur(function() {
		if($(this).val() == '') {
			$(this).val(defaultCommentText);
		}
	});

	$(".replyTextarea").focus(function() {
		if($(this).val() == defaultReplyText) {
			$(this).val('');
		}
	});	
	$(".replyTextarea").blur(function() {
		if($(this).val() == '') {
			$(this).val(defaultReplyText);
		}
	});
	
	$("#searchInput").focus(function() {
		if($(this).val() == defaultSearchText) {
			$(this).val('');
		}
	});	
	$("#searchInput").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 comment
function addComment(module, id) {
	var comment = $("#commentText").val();
	if(comment == defaultCommentText) {
		comment = '';
	}
	
	$.post("/modules/"+module+"/ajax/addComment.php", { comment: comment, id: id },
	   function(data){
	    if(data.status == 'complete') {
	    	$("#allComments").append(data.div);
	    	$("#commentText").val(defaultCommentText);
	    	jQuery.facebox("Your comment was added successfully.");
	    }
	    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 comment
function deleteComment(module, id) {
	$.post("/modules/"+module+"/ajax/deleteComment.php", { id: id },
	   function(data){
	    if(data.status == 'complete') {
	    	$("#commentN"+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"
	);
}

//show all comments
function showAllComments(id) {
	$("#showComments"+id).slideDown("normal");
}

//check textarea characters count
function checkCharactersCount(textarea) {
	if($("#questionText").val().length > 750) {
		$("#questionText").val($("#questionText").val().substr(0, 750));
		$("#charactersCount").html('0');
	}
	else {
		$("#charactersCount").html(parseInt(750 - $("#questionText").val().length));
	}
}

//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);
	    	jQuery.facebox("Your reply was added successfully.");
	    }
	    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"
	);
}



/*
 * CHAT FUNCTIONS
 */
function addChatMessage() {
	var msg = $("#chatMessage").val();
	
	$.post("/modules/chat/ajax/addChatMessage.php", { text: msg },
	   function(data){		
	    if(data.status == 'complete') {
	    	$("#chatMessage").val("");
	    }
	    else if(data.status == 'login_error') {
	    	jQuery.facebox("Your session has expired. Please sign in and try again.");
	    }
	  }, 
	  "json"
	);
}

function getChatMessages() {
	$.post("/modules/chat/ajax/getChatMessages.php", {  },
	  function(data){		
	    $("#chatList").html(data);
	  }
	);
	
	setTimeout("getChatMessages()", "1000");
}

$(document).ready(function() {
	getChatMessages();
});
