/*
###################################
#          FusionCMS v5           #
# - - - - - - - - - - - - - - - - #
#   Made by Jesper of raxezdev    #
###################################
*/

function makeThread() {
	$("#headline").html("Create thread")
		
	$("#category_content").hide(300, function() {
		$("#create_thread").show(300);
	});
}

function previewThread() {
	var title = $("#thread_title").val();
	var content = $("#thread_editor").val();
	var user = $("#thread_user").val();
	
	if(title.length != 0 && content.length != 0) {
	
		$("#preview_spot").html('<div class="right_box"><div class="right_box_top" id="preview_title">Loading...</div><div class="right_box_body"><div id="preview_content"></div><div class="clear"></div></div></div>');
		
		$("#preview_content").html('<center><img src="theme/' + theme + '/images/ajax.gif" /></center>');
		
		$.post("inc/forum_ajax.php", { ajax: 7, content: content, user: user }, function(data){ 
			$("#preview_title").html(title);
			$("#preview_content").html(data);
		});
	} else {
		alert("Please fill all fields");
	}
}

function submitThread() {
	var title = $("#thread_title").val();
	var content = $("#thread_editor").val();
	var category = $("#thread_category").val();
	var user = $("#thread_user").val();
	
	if(title.length != 0 && content.length != 0) {
		$("#forum_ajax").html('<center><img src="theme/' + theme + '/images/ajax.gif" /></center>');
		
		$.post("inc/forum_ajax.php", { ajax: 1, title: title, content: content, category: category, user: user }, function(data){
			$("#title").html(title);
			$("#forum_ajax").html(data);
		});
	} else {
		alert("Please fill all fields");
	}
}

var sending = false;

function submitPost() {
	if(!sending) {
		var post = $("#thread_editor").val();
		var thread = $("#thread").val();
		var user = $("#user").val();
		
		if(post.length != 0 && user.length != 0) {
			$("#my_post").html('<center><img src="theme/' + theme + '/images/ajax.gif" /></center>');
			sending = true;
			
			$.post("inc/forum_ajax.php", { ajax: 4, thread: thread, content: post, user: user }, function(data){
				$("#my_post").html(data);
				sending = false;
			});
		} else {
			alert("Please fill all fields");
		}
	} 
}

function reportPost(id, is_thread) {
	var reason = prompt("What are you reporting this user for?", "");
	
	if(reason != "" && reason != null) {
		$.post("inc/forum_ajax.php", { ajax: 2, reason: reason, id: id, is_thread: is_thread}, function() {
			alert("Report was successfully submitted! \n A moderator will take care of your report soon!");
		});
	}
}

function addReputation(user,id,is_thread) {
	$("#rep_ajax_" + id + "_" + is_thread).html(" ...");
	
	$.post("inc/forum_ajax.php", { ajax: 5, id: id, is_thread: is_thread, user: user}, function(data) {
		alert(data);
		$("#rep_ajax_" + id + "_" + is_thread).html("");
	});
}

/*---------------
USER PM FUNCTION
HAS BEEN DISABLED
DUE TO LACK OF
DEVELOPMENT TIME
-----------------

function sendPm(user) {
	var msg = prompt("Compose a private message to " + user + ":", "");
	
	if(msg != "" && msg != null) {
		alert("Message has been submitted to " + user + "!");
	}
}*/

function removePost(id, is_thread, type, report_id) {
	if(type == 1) {
		if(is_thread == 1) {
			$("#forum_spot").html("<img src='theme/" + theme + "/images/ajax.gif' />");
			$.post("inc/forum_ajax.php", { ajax: 3, id: id, is_thread: is_thread }, function() {
				alert('This thread has been deleted! You will now be redirected...');
				$("#forum_spot").html("<script type='text/javascript'>window.location='?p=forum';</script>")
			});
		} else {
			$("#post_" + id).html("<img src='theme/" + theme + "/images/ajax.gif' />");
			$.post("inc/forum_ajax.php", { ajax: 3, id: id, is_thread: is_thread }, function() {
				$("#post_" + id).hide(500, function(){
					$("#post_" + id).remove()
				});
			});
			
		}
	} else {
		if(is_thread == 1) {
			$("#thread_" + id).html("<td><img src='theme/" + theme + "/images/ajax.gif' /></td><td></td><td></td><td></td>");
			$.post("inc/forum_ajax.php", { ajax: 9, id: id, is_thread: is_thread, report_id: report_id }, function() {
				$("#thread_" + id).remove()
			});
		} else {
			$("#post_" + id).html("<td><img src='theme/" + theme + "/images/ajax.gif' /></td><td></td><td></td><td></td>");
			$.post("inc/forum_ajax.php", { ajax: 9, id: id, is_thread: is_thread, report_id: report_id }, function() {
				$("#post_" + id).hide(500, function(){
					$("#post_" + id).remove()
				});
			});
			
		}
	}
}

function mod_ignore(report_id, id, is_thread) {
	if(is_thread == 1) {
			$("#thread_" + id).html("<td><img src='theme/" + theme + "/images/ajax.gif' /></td><td></td><td></td><td></td>");
			$.post("inc/forum_ajax.php", { ajax: 10, is_thread: is_thread, report_id: report_id }, function() {
				$("#thread_" + id).remove()
			});
		} else {
			$("#post_" + id).html("<td><img src='theme/" + theme + "/images/ajax.gif' /></td><td></td><td></td><td></td>");
			$.post("inc/forum_ajax.php", { ajax: 10, is_thread: is_thread, report_id: report_id }, function() {
				$("#post_" + id).hide(500, function(){
					$("#post_" + id).remove()
				});
			});
			
		}
}

function quoteMessage(msg_encoded) {
	$("#thread_editor").val("[quote]" + Base64.decode(msg_encoded) + "[/quote]")
}

function saveForumSettings() {
	var location = $("#forum_location").val()
	var color = $("#forum_color").val()
	var avatar = $("#forum_avatar").val()
	var signature = $("#forum_signature").val()
	
	$("#ajax_spot").html("<img src='theme/" + theme + "/images/ajax.gif' />")
	
	$.post("inc/forum_ajax.php", { ajax: 8, location: location, color: color, avatar: avatar, signature: signature }, function(data) {
		$("#ajax_spot").html(data)
	});
}

function moveThread(id)
{
	$("#mod_move").html("<div class='right_box'><div class='right_box_top'>Move thread</div><div class='right_box_body' id='move_body'><center><img src='theme/" + theme + "/images/ajax.gif' /></center></div>")
	
	$.post("inc/forum_ajax.php", { ajax: 11, id: id }, function(data) {
		$("#move_body").html(data);
	})
}

function do_moveThread()
{
	var id = $("#move_thread_id").val();
	var to = $("#move_thread_to").val();
	
	$.post("inc/forum_ajax.php", { ajax: 12, id: id, to: to }, function(data) {
		$("#move_body").html(data);
	})
}

function stickyThread(id, s)
{	
	$.post("inc/forum_ajax.php", { ajax: 13, id: id, s: s }, function(data) {
		alert(data);
	})
}

function closeThread(id, s)
{	
	$.post("inc/forum_ajax.php", { ajax: 14, id: id, s: s }, function(data) {
		alert(data);
	})
}
