
	var current_type = "top";
	
	function showCommentOptions() {
		var position = $("#node_sort_hold").position();
		var width = $("#node_sort_hold").width();
		if (width < 100) {
			width = "100";
		}
		$("#show_comment_options").css('top',position.top);
		$("#show_comment_options").css('left',position.left);
		$("#show_comment_options").css('width',width);
		$("#show_comment_options").show('fast');
		
	}
	
	function selectCommentOption(option) {
		if (option == "new") {
			$("#node_sort").html('Newest');
		   	$("#top_comments").hide('fast');
		   	$("#new_comments").show('fast');
		   	current_type = "new";
		} else {
			$("#node_sort").html('Highest Rated');
		   	$("#new_comments").hide('fast');
		   	$("#top_comments").show('fast');
		   	current_type = "top";
		}
		$("#show_comment_options").hide('fast');
	}

	function showHiddenComment(id) {
		$("#hidden_show_" + id).toggle('fast');
		$("#hidden_" + id).toggle('fast');
	}
	
	function showFormatting() {
	     $().colorbox({ width:"600px;", inline:true, href:"#show_formatting" });
	}
	
	function showReply(id) {
		$("#reply_" + id).toggle('fast');
	}
	
	function voteComment(id,type) {
		$('#loading').show();
		// -----
		// Get & organize data
		// type = 1 (upvote) 2 (downvote)
		var send_data = "id=" + id + "&type=" + type + "&content_id=" + $("#content_id").val();
		// -----
		$.post("functions/comment_upvote.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				var current_score = $("#cur_score_" + id).val();
				if (type == "1") {
					$('#up_comment_' + id).addClass('upvoted');
					$('#down_comment_' + id).removeClass('downvoted');
					newscore = parseFloat(current_score) + 1;
				} else {
					$('#up_comment_' + id).removeClass('upvoted');
					$('#down_comment_' + id).addClass('downvoted');
					newscore = parseFloat(current_score) - 1;
				}
				$('#comment_score_' + id).html(newscore);
				$("#cur_score_" + id).val(newscore);
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	};

	function postReply(id,marginleft) {
		$('#loading').show();
		// -----
		// Get & organize data
		// type = 1 (upvote) 2 (downvote)
		var send_data = "margin=" + marginleft + "&comment=" + $("#reply_text_" + id).val() + "&comment_id=" + id + "&content_id=" + $("#content_id").val();
		// -----
		$.post("functions/post_comment.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				$('#show_reply_' + id).css('display','none');
				$('#show_reply_' + id).html(returned['1']);
				$('#show_reply_' + id).show('fast');
				$('#reply_' + id).hide();
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
	
	function deleteComment(id) {
		del_text = "Please confirm deletion: <a href=\"#\" onClick=\"deleteCommentFinal('" + id + "');return false;\">Proceed</a> | <a href=\"#\" onClick=\"closeDelete('" + id + "');return false;\">Cancel</a>";
		$('#del_comment_' + id).html(del_text);
	}
	
	function closeDelete(id) {
		del_text = "<a href=\"#\" onClick=\"deleteComment('" + id + "');return false;\">Delete</a>";
		$('#del_comment_' + id).html(del_text);
	}
	
	function deleteCommentFinal(id) {
		$('#loading').show();
		var send_data = "id=" + id + "&content_id=" + $("#content_id").val();
		$.post("functions/del_comment.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				$('#comment_id_' + id).hide();
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
	
	function editComment(id) {
		var content = $("#comment_data_" + id).html();
		content = content.replace('<b>', '[b]');
		content = content.replace('</b>', '[/b]');
		content = content.replace('<i>', '[i]');
		content = content.replace('</i>', '[/i]');
		content = content.replace('<strike>', '[s]');
		content = content.replace('</strike>', '[/s]');
		content = content.replace('<span class="quote">', '[q]');
		content = content.replace('</span>', '[/q]');
		content = content.replace('<br>', "\n");
		$('#edit_' + id).val(content);
		$('#show_edit_' + id).show('fast');
	}
	
	function cancelEdit(id) {
		$('#edit_' + id).val('');
		$('#show_edit_' + id).hide('fast');
	}
	
	function finalizeEdit(id) {
		$('#loading').show();
		// -----
		// Get & organize data
		// type = 1 (upvote) 2 (downvote)
		var send_data = "comment=" + $("#edit_" + id).val() + "&id=" + id + "&content_id=" + $("#content_id").val();
		// -----
		$.post("functions/edit_comment.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				$("#comment_data_" + id).html(returned['1']);
				$('#edit_' + id).val();
				$('#show_edit_' + id).hide();
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
	
	function postComment(id) {
		$('#loading').show();
		// -----
		// Get & organize data
		// type = 1 (upvote) 2 (downvote)
		var send_data = "comment=" + $("#comment").val() + "&content_id=" + $("#content_id").val();
		// -----
		$.post("functions/post_comment.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				$('#next_comment_place').css('display','none');
				$('#next_comment_place').html(returned['1']);
				$('#next_comment_place').show('fast');
				$('#nocomments1').hide();
				$('#nocomments2').hide();
				$('#comment_box').hide();
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
	
	// ADD TO FAVORITES FEATURE
	function addFavorite() {
		$('#loading').show();
		var send_data = "id=" + $("#cid").val();
		$.post("functions/favorite.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			handle_error(returned['1']);
			$('#loading').hide();
		});
		return false;
	}
	
	// ADD FRIEND FEATURE
	function addFriend() {
	     $().colorbox({ width:"350px;", inline:true, href:"#show_friend" });
	}
	function sendFriend() {
		$('#loading').show();
		var send_data = "u=" + $("#username").val() + "&uid=" + $("#uid").val() + "&msg=" + $("#friend_msg").val();
		$.post("functions/friend_req.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				$.fn.colorbox.close();
				// $('#show_friend').html('Your friend request has been sent.');
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
	
	// "INFORM A FRIEND" FEATURE
	function tellFriend() {
	     $().colorbox({ width:"350px;", inline:true, href:"#show_inform" });
	}
	
	function emailFriend() {
		$('#loading').show();
		var send_data = "id=" + $("#cid").val() + "&emails=" + $("#inform_emails").val() + "&msg=" + $("#inform_msg").val();
		$.post("functions/inform_friend.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				$.fn.colorbox.close();
				// $('#show_inform').html('We have emailed your friend(s) about this image!');
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
	
	// SEND PM FEATURE
	function showPM() {
		$().colorbox({ width:"350px;", inline:true, href:"#show_pm" });
	}
	// SEND PM FEATURE - NON VIEW_USER SCREEN
	function showPMx(username,uid) {
		$("#username").val(username);
		$("#uid").val(uid);
		$().colorbox({ width:"350px;", inline:true, href:"#show_pm" });
	}
	function sendPM() {
		$('#loading').show();
		var send_data = "u=" + $("#username").val() + "&uid=" + $("#uid").val() + "&msg=" + $("#pm_msg").val();
		$.post("functions/send_pm.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				$.fn.colorbox.close();
				// $('#show_pm').html('Your PM has been sent.');
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
	
	// DELETE A PM
	function deletePM(id) {
		$('#loading').show();
		var send_data = "id=" + id;
		$.post("functions/delete_pm.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				removeDiv = "pm_" + id;
				$("#" + removeDiv).hide('fast');
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
	
	// Mark a PM as read
	function markPMread(status,id) {
		$('#loading').show();
		var send_data = "id=" + id + "&status=" + status;
		$.post("functions/mark_pm_read.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				removeDiv = "pm_" + id;
				spanDiv = "pm_" + id + "_status";
				// Marking it unread
				if (status == "1") {
					$("#" + removeDiv).addClass('unread');
					$("#" + spanDiv).html('<a href="#" onClick="markPMread(\'0\',\'' + id + '\');return false;">Mark as read</a>');
				}
				// Marking it read
				else {
					$("#" + removeDiv).removeClass('unread');
					$("#" + spanDiv).html('<a href="#" onClick="markPMread(\'1\',\'' + id + '\');return false;">Mark as unread</a>');
				}
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
	
	// SUGGEST ALTERNATIVE NAME FEATURE
	function suggestName() {
	     $().colorbox({ width:"350px;", inline:true, href:"#show_suggest" });
	}
	function sendSuggest() {
		$('#loading').show();
		var send_data = "id=" + $("#cid").val() + "&name=" + $("#alt_name").val();
		$.post("functions/suggest.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				$.fn.colorbox.close();
				// $('#show_suggest').html('Thanks! We have received your suggestion and will review it when we get a chance.');
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
	
	// REPORT PROBLEM FEATURE
	function reportProblem() {
	     $().colorbox({ width:"350px;", inline:true, href:"#show_report" });
	}
	function sendReport() {
		$('#loading').show();
		var send_data = "id=" + $("#cid").val() + "&issue=" + $("#report_issue").val() + "&comments=" + $("#report_comment").val();
		$.post("functions/report.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				$.fn.colorbox.close();
				// $('#show_report').html('Thanks! We have received your report and will look into this as soon as we can.');
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
	
	// FRIENDSHIP APPROVE/REJECT
	function acceptFriendship(id,type) {
		$('#loading').show();
		var send_data = "id=" + id + "&type=" + type;
		$.post("functions/friendship.php", send_data, function(theResponse) {
	     	var returned = theResponse.split('+++');
			theDiv = "freq" + id;
			theDiv1 = "links" + id;
			if (returned['0'] == "1") {
				if (type == "1") {
					$("#" + theDiv).addClass('friendship_approved');
					$("#" + theDiv).fadeTo('fast', 0.3);
					$("#" + theDiv1).html('Accepted!');
				} else {
					$("#" + theDiv).addClass('friendship_rejected');
					$("#" + theDiv).fadeTo('fast', 0.3);
					$("#" + theDiv1).html('Rejected');
				}
			} else {
				handle_error(returned['1']);
			}
			$('#loading').hide();
		});
		return false;
	}
