function edit()
{
	this.ajaxURL = "/ajax/forums_edit.php";
	this.container = null;
	this.sid = 0;
	
	/**
	 * Opens the Edit Pop-up
	 */
	this.open = function(sid)
	{
		if (this.container === null)
		{
			this.sid = sid;
			this.container = $(document.createElement("div"));
			$("body").append(this.container);
			this.container.addClass("popup");
			this.container.css({
				"display": "none",
				"width": "800px",
				"height": "400px",
				"margin-left": "-400px",
				"margin-top": "-200px"
			});
			
			var title = $(document.createElement("div"));
			this.container.append(title);
			title.addClass("title");
			title.html("" +
				"<div style='float: left; width: 70%;'>" +
					"Edit Post" +
				"</div>" +
				"<div style='float: left; width: 30%; text-align: right;'>" +
					"<img class='png' src='http://zacisa.info/images/icons/cancel.png' onclick='edit.close();' />" +
				"</div>" +
				"<div class='clear'></div>");
			
			var body = $(document.createElement("div"));
			this.container.append(body);
			body.addClass("body_nopadding");
			body.append("<img src='/images/ajax-loader.gif' style='margin-top: 170px; margin-left: 370px;' />");
			
			this.container.show(800);
			this.container.draggable({ containment: "body", handle: ".title" });
			
			$.post(this.ajaxURL,
				{ type: "grab", sid: this.sid },
				function(data)
				{
					edit.container.children(".body_nopadding").html(data);
				});
			
			hideYoutubeVideos(801);
		}
	};
	
	/**
	 * Closes the Edit Pop-up
	 */
	this.close = function()
	{
		if (this.container !== null)
		{
			this.container.hide(800);
			this.container.html("");
			setTimeout(function()
			{
				edit.container = null;
			}, 801);
			showYoutubeVideos(801);
		}
	};
	
	/**
	 * Updates the Post
	 */
	this.update = function()
	{
		var text = $("#posttext_edit").val();
		var title = $("#thread_name_edit").val();
		if (title == "" || title === undefined)
		{
			title = "";
		}
		
		$("#karma_"+edit.sid+"_post_show").html("<img src='/images/ajax-loader.gif' />");
		this.close();
		$.post(this.ajaxURL,
			{ type: "update", sid: this.sid, text: text, title: title },
			function(data)
			{
				$("#karma_"+edit.sid+"_post_show").html(data);
				if (title != "")
				{
					$("#thread_name").html(title);
				}
			});
	};
};
var edit = new edit();

function quick_reply()
{
	this.ajaxURL = "/ajax/forums_quick_reply.php";
	this.container = null;
	this.img = null;
	
	/**
	 * Submits the Post
	 */
	this.submit = function(sid, page)
	{
		var text = $("#posttext");
		text.attr("disabled", true);
		this.img = $(document.createElement("img"));
		this.img.attr("src", "/images/ajax-loader.gif");
		this.img.css({
			position: "absolute",
			left: (text.offset().left + Math.round(parseInt(text.css("width")) / 2) - 17) + "px",
			top: (text.offset().top + Math.round(parseInt(text.css("height")) / 2) - 17) + "px",
			"z-index": "20000"
		});
		$("body").append(this.img);
		
		$.post(this.ajaxURL,
			{ type: "submit", sid: sid, text: text.val() },
			function(data)
			{
				text.attr("disabled", false);
				quick_reply.img.remove();
				
				if (data.error == 0)
				{
					if (data.page == page)
					{
						$("#posttext").val("");
						$("#thread_"+sid).append(data.html);
					} else {
						var linkS = location.href.search(new RegExp("/"+sid));
						location.href = location.href.substr(0, linkS)+"/"+sid+"/"+data.page+"#"+data.post_sid;
					}
				} else {
					alert(data.reasons);
				}
			}, "json");
	};
	
	/**
	 * Opens the Quick Reply Preview
	 */
	this.preview = function()
	{
		var text = $("#posttext");
		if (this.container === null)
		{
			this.container = $(document.createElement("div"));
			$("body").append(this.container);
			this.container.addClass("popup");
			this.container.css({
				"display": "none",
				"width": "600px",
				"height": "300px",
				"margin-left": "-400px",
				"margin-top": "-200px"
			});
			
			var title = $(document.createElement("div"));
			this.container.append(title);
			title.addClass("title");
			title.html("" +
				"<div style='float: left; width: 70%;'>" +
					"Preview" +
				"</div>" +
				"<div style='float: left; width: 30%; text-align: right;'>" +
					"<img class='png' src='http://zacisa.info/images/icons/cancel.png' onclick='quick_reply.close();' />" +
				"</div>" +
				"<div class='clear'></div>");
			
			var body = $(document.createElement("div"));
			this.container.append(body);
			body.addClass("body");
			body.css({
				"height": "271px",
				"margin-top": "0px",
				"padding": "4px",
				"overflow": "auto"
			});
			body.append("<img src='/images/ajax-loader.gif' style='margin-top: 140px; margin-left: 270px;' />");
			
			this.container.show(800);
			this.container.draggable({ containment: "body", handle: ".title" });
		}

		$.post(this.ajaxURL,
			{ type: "preview", text: text.val() },
			function(data)
			{
				quick_reply.container.children(".body").html(data);
			});
	};
	
	/**
	 * Closes the Quick Reply Pop-up
	 */
	this.close = function()
	{
		if (this.container !== null)
		{
			this.container.hide(800);
			this.container.html("");
			setTimeout(function()
			{
				quick_reply.container = null;
			}, 801);
		}
	};
};
var quick_reply = new quick_reply();

/**
 * Small Class dealing with making a new Poll on the Add/Edit Thread Pages
 */
function PollNew()
{
	this.options_total = 2;
	
	/**
	 * Open/Closes the Poll Area
	 */
	this.changeOption = function(allow)
	{
		//Allow
		if (allow.val() == "y")
		{
			$("tr[type='pollNew']").show();
		} else {
			$("tr[type='pollNew']").hide();
		}
	};
	
	/**
	 * Adds a new Option for the Poll
	 */
	this.addNewOption = function()
	{
		if (this.options_total < 10)
		{
			this.options_total++;
			
			var options = $("#poll_options");
			var div = $(document.createElement("div"));
			div.attr("id", "option_"+this.options_total);
			div.css({
				"padding-bottom": "3px",
				"display": "none"
			});
			div.html("<span>" +
					this.options_total +
				"</span>: <input type='text' name='poll_options[]' maxlength='128' style='width: 200px;' /> " +
				"<a onclick=\"PollNew.removeOption($(this).parent().children('span').html());\">" +
					"[remove]" +
				"</a>");
			
			options.append(div);
			div.slideDown(250);
		}
	};
	
	/**
	 * Removes the Option and re-number all other below it
	 */
	this.removeOption = function(num)
	{
		var i,opt;
		var option = $("#option_"+num);
		option.attr("id", "option_old");
		
		option.slideUp(250);
		setTimeout(function()
		{
			option.remove();
		}, 251);
		
		//Updates all Options after this Option
		for(i=parseInt(num)+1; i<=this.options_total; i++)
		{
			opt = $("#option_"+i);
			opt.attr("id", "option_"+(i-1));
			opt.children("span").html(i-1);
		}
		
		this.options_total--;
	};
	
	/**
	 * Reorders all the Poll Options after the sortable update
	 */
	this.reorderAll = function()
	{
		$('#poll_options').children().each(function(i, div)
		{
			var j = i+1;
			div = $(div);
			
			div.find("span").html(j);
			div.attr("id", "option_"+j);
			div.find("a").remove();
			
			if (j >= 3)
			{
				div.append("<a onclick=\"PollNew.removeOption($(this).parent().children('span').html());\">" +
						"[remove]" +
					"</a>");
			}
		});
	};
};
var PollNew = new PollNew();
$('#poll_options').ready(function()
{
	$('#poll_options').sortable({
		update: function()
		{
			PollNew.reorderAll();
		}
	});
	$("#poll_options").disableSelection();
});

/**
 * Poll voting
 */
function Poll()
{
	this.ajaxURL = "/ajax/forums_poll.php";
	this.poll_sid = 0;
	this.totalvotes = 0;
	
	/**
	 * Poll Vote, also automatically changes the options
	 */
	this.vote = function(sid)
	{
		this.totalvotes++;
		
		$("#poll_totalvotes").html(this.totalvotes);
		$("#poll_option_"+sid+"_votes").html(parseInt($("#poll_option_"+sid+"_votes").html())+1);
		$("#poll_vote_help").remove();
		
		/**
		 * Go through every option and change their percents
		 */
		$("div[type='poll_option']").each(function(i, option)
		{
			option = $(option);
			
			var votes = parseInt($("#poll_option_"+option.attr("sid")+"_votes").html());
			option.css("width", ((votes/Poll.totalvotes)*100)+"%");
			option.children().html((((votes/Poll.totalvotes)*100)+"").replace(/([0-9]+?)\.([0-9]{0,2}).+/, "$1.$2")+"%");
			
			var a = option.parent().children("a");
			option.parent().prepend(a.html());
			a.remove();
		});
		
		/**
		 * User Vote
		 */
		$.post(this.ajaxURL,
			{ poll_sid: this.poll_sid, vote_sid: sid });
	};
};
var Poll = new Poll();

/**
 * Pop-up to move a Post to another Thread for a mod/admin
 */
function Post_Move()
{
	this.ajaxURL = "/ajax/post_move.php";
	this.container = null;
	this.img = null;
	this.isOpen = false;
	this.sid = 0;
	
	/**
	 * Opens the pop-up
	 * 
	 * @param int sid
	 */
	this.open = function(sid)
	{
		if (!this.isOpen)
		{
			this.isOpen = true;
			this.sid = sid;
			
			this.container = $(document.createElement("div"));
			$("body").append(this.container);
			this.container.addClass("popup");
			this.container.css({
				"display": "none",
				"width": "300px",
				"height": "140px",
				"margin-left": "-150px",
				"margin-top": "-70px"
			});
			
			var title = $(document.createElement("div"));
			this.container.append(title);
			title.addClass("title");
			title.html("" +
				"<div style='float: left; width: 70%;'>" +
					"Move Post" +
				"</div>" +
				"<div style='float: left; width: 30%; text-align: right;'>" +
					"<img class='png' src='http://zacisa.info/images/icons/cancel.png' onclick='Post_Move.close();' />" +
				"</div>" +
				"<div class='clear'></div>");
			
			var body = $(document.createElement("div"));
			this.container.append(body);
			body.addClass("body");
			body.css({
				"height": "111px",
				"margin-top": "0px",
				"padding": "4px",
				"overflow": "auto"
			});
			body.append("<img src='/images/ajax-loader.gif' style='margin-top: 25px; margin-left: 120px;' />");
			
			//Ajax call tog rab all the categories and threads for the category you are already in.
			$.post(this.ajaxURL,
				{ type: "get_cats", sid: this.sid },
				function(data)
				{
					if (data.error == 0)
					{
						body.html("<div style='padding-bottom: 3px;'>" +
								"Categories: " +
								"<select id='cat_sid' onchange='Post_Move.changeCategory(this.value);' style='width: 200px;'>" +
									data.cats +
								"</select>" +
							"</div>" +
							"<div style='padding-bottom: 3px;'>" +
								"Thread: " +
								"<select id='to_sid' style='width: 200px;'>" +
									data.threads +
								"</select>" +
							"</div>" +
							"<div style='padding-bottom: 3px;'>" +
								"Append Into: <select id='append'>" +
									"<option value='n'>No</option>" +
									"<option value='y'>Yes</option>" +
								"</select>" +
							"</div>" +
							"<div style='text-align: center;'>" +
								"<input type='button' class='button' value='Move Post' onclick='Post_Move.update();' />" +
							"</div>");
						$("#cat_sid").val(data.cat_sid);
					}
				}, "json");
			
			this.container.show(400);
			this.container.draggable({ containment: "body", handle: ".title" });
		}
	};
	
	/**
	 * Closes the pop-up
	 */
	this.close = function()
	{
		if (this.isOpen)
		{
			this.container.hide(400);
			
			setTimeout(function()
			{
				Post_Move.container.remove();
				Post_Move.isOpen = false;
			}, 401);
		}
	};
	
	/**
	 * Changes the Category Thread List
	 */
	this.changeCategory = function(cat_sid)
	{
		var to = $("#to_sid");
		to.html("<option value='0'>Loading...</option>");
		
		$.post(this.ajaxURL,
			{ type: "get_threads", sid: this.sid, cat_sid: cat_sid },
			function(data)
			{
				if (data.error == 0)
				{
					to.html(data.threads);
					Post_Move.container.find("input").attr("disabled", false);
				} else {
					to.html("<option value='0'>None available..</option>");
					Post_Move.container.find("input").attr("disabled", true);
				}
			}, "json");
	};
	
	/**
	 * Moves the Post
	 */
	this.update = function()
	{
		var to_sid = $("#to_sid").val();
		Post_Move.container.find("input").attr("disabled", true);

		$.post(this.ajaxURL,
			{ type: "update", sid: this.sid, to_sid: to_sid, append: $("#append").val() },
			function(data)
			{
				if (data.error == 0)
				{
					$("[post_sid='"+Post_Move.sid+"']").remove();
					Post_Move.close();
				} else {
					alert(data.reasons);
					Post_Move.container.find("input").attr("disabled", false);
				}
			}, "json");
	};
};
var Post_Move = new Post_Move();

/**
 * Report Pop-up to be viewed by Mods and Admins
 * 
 * @return Report
 */
function Report()
{
	this.ajaxURL = "/ajax/admin_report.php";
	this.container = null;
	this.isOpen = false;
	
	/**
	 * Opens the pop-up for the Report Viewer
	 */
	this.open = function()
	{
		if (!this.isOpen)
		{
			this.isOpen = true;
			this.container = $(document.createElement("div"));
			$("body").append(this.container);
			this.container.addClass("popup");
			this.container.css({
				"display": "none",
				"width": "400px",
				"height": "300px",
				"margin-left": "-200px",
				"margin-top": "-150px"
			});
			
			var title = $(document.createElement("div"));
			this.container.append(title);
			title.addClass("title");
			title.html("" +
				"<div style='float: left; width: 70%;'>" +
					"Issued Report" +
				"</div>" +
				"<div style='float: left; width: 30%; text-align: right;'>" +
					"<img class='png' src='http://zacisa.info/images/icons/cancel.png' onclick='Report.close();' />" +
				"</div>" +
				"<div class='clear'></div>");
			
			var body = $(document.createElement("div"));
			this.container.append(body);
			body.addClass("body");
			body.css({
				"height": "270px",
				"margin-top": "0px",
				"padding": "0px",
				"overflow": "auto"
			});
			body.append("<img src='/images/ajax-loader.gif' style='margin-top: 120px; margin-left: 175px;' />");
			
			this.container.show(400);
			this.container.draggable({ containment: "body", handle: ".title" });
			
			this.grabReports();
		}
	};
	
	/**
	 * Closes the pop-up
	 */
	this.close = function()
	{
		if (this.isOpen)
		{
			this.container.hide(800);
			
			setTimeout(function()
			{
				Report.container.remove();
				Report.isOpen = false;
			}, 801);
		}
	};
	
	/**
	 * Grabs all the issued reports
	 */
	this.grabReports = function()
	{
		$.post(this.ajaxURL,
			{ type: "grabReports" },
			function(data)
			{
				if (data.error == 0)
				{
					Report.container.children(".body").html(data.html);
				} else {
					alert(data.reasons);
				}
				
				if (data.script != "")
				{
					try
					{
						eval(data.script);
					} catch (err) {};
				}
			}, "json");
	};
	
	/**
	 * Opens the Report and closes all others
	 * 
	 * @param int sid
	 */
	this.openReport = function(sid)
	{
		this.container.find("[type='reports']").hide();
		this.container.find("[type='reports'][report_sid='"+sid+"']").show();
	};
	
	/**
	 * Does an action for the Report, and then reloads the Reports
	 */
	this.markAction = function(sid, type)
	{
		$.post(this.ajaxURL,
			{ type: "action", sid: sid, action: type },
			function()
			{
				Report.grabReports();
			});
	};
};
var Report = new Report();

function FC_Filter()
{
	this.ajaxURL = "/ajax/fc.php";
	this.listOpen = false;
	this.canFindMore = true;

	this.findGames = function(system, game)
	{
		if (game.length > 0)
		{
			$.post(this.ajaxURL,
				{ type: "filter", system: system, game: game },
				function(data)
				{
					if (data.count > 0)
					{
						FC_Filter.openList();
						$("#pm_users").html(data.html);
					} else {
						FC_Filter.closeList();
					}
				}, "json");
		} else {
			this.closeList();
		}
	};
	
	this.selectGame = function(game)
	{
		$("#fc_add").val(game);
		this.closeList();
	};
	
	this.openList = function()
	{
		if (!this.listOpen)
		{
			this.listOpen = true;
			$("#pm_users").css({
				top: $("#fc_add").offset().top+parseInt($("#fc_add").height())+5,
				left: $("#fc_add").offset().left
			});
			$("#pm_users").slideDown("800");
			
			var cancel = $(document.createElement("img"));
			cancel.addClass("png");
			cancel.attr("id", "fc_list_cancel");
			cancel.attr("src", "http://zacisa.info/images/icons/cancel.png");
			cancel.css({
				position: "absolute",
				top: $("#fc_add").offset().top+parseInt($("#fc_add").height())+8,
				left: $("#fc_add").offset().left+$("#pm_users").width()-16,
				"z-index": $("#pm_users").css("z-index")+1
			});
			cancel.attr("onclick", "FC_Filter.closeList();");
			$("body").append(cancel);
		}
	};
	
	this.closeList = function()
	{
		if (this.listOpen)
		{
			this.listOpen = false;
			$("#pm_users").slideUp("800");
			$("#pm_users").html("");
			$("#fc_list_cancel").remove();
		}
	};
};
var FC_Filter = new FC_Filter();

function FC_Add()
{
	this.ajaxURL = "/ajax/fc.php";
	this.container = null;
	this.isOpen = false;
	this.sid = 0;
	
	/**
	 * Opens the FC Dialog to add a user FC to your list
	 * 
	 * @param int sid
	 */
	this.open = function(sid)
	{
		if (!this.isOpen)
		{
			this.sid = sid;
			this.isOpen = true;
			this.container = $(document.createElement("div"));
			$("body").append(this.container);
			this.container.addClass("popup");
			this.container.css({
				"display": "none",
				"width": "400px",
				"height": "200px",
				"margin-left": "-200px",
				"margin-top": "-150px"
			});
			
			var title = $(document.createElement("div"));
			this.container.append(title);
			title.addClass("title");
			title.html("" +
				"<div style='float: left; width: 70%;'>" +
					"Add FC" +
				"</div>" +
				"<div style='float: left; width: 30%; text-align: right;'>" +
					"<img class='png' src='http://zacisa.info/images/icons/cancel.png' onclick='FC_Add.close();' />" +
				"</div>" +
				"<div class='clear'></div>");
			
			var body = $(document.createElement("div"));
			this.container.append(body);
			body.addClass("body");
			body.css({
				"height": "170px",
				"margin-top": "0px",
				"overflow": "auto"
			});
			body.append("" +
				"<div style='margin-bottom: 3px;'>" +
					"Send PM as Well? <select id='fc_add_send_pm'>" +
						"<option value='y'>Yes</option>" +
						"<option value='n'>No</option>" +
					"</select>" +
				"</div>" +
				"<div style='margin-bottom: 3px;'>" +
					"<textarea id='fc_add_text' style='width: 390px; height: 110px;'>" +
						"This is an automated message to let you know that [user]" +
							$("div[infoType='user_sid']").html() +
						"[/user] has added your Friend Code for the game [[GAME]]\n\n" +
						"If you have not already done so, please add this User back. Thank you." +
					"</textarea>" +
				"</div>" +
				"<div style='text-align: center;'>" +
					"<input type='button' class='button' value='Cancel' onclick='FC_Add.close();' />" +
					"&nbsp;" +
					"<input type='button' class='button' value='Add Friend Code' onclick='FC_Add.send();' />" +
				"</div>");
			
			this.container.show(400);
			this.container.draggable({ containment: "body", handle: ".title" });
		}
	};
	
	/**
	 * Closes the FC Add Dialog
	 */
	this.close = function()
	{
		if (this.isOpen)
		{
			this.container.hide(400);
			
			setTimeout(function()
			{
				FC_Add.container.remove();
				FC_Add.isOpen = false;
			}, 401);
		}
	};
	
	/**
	 * Adds the FC to your list
	 */
	this.send = function()
	{
		$.post(this.ajaxURL,
			{ type: "add", sid: this.sid, send_pm: $("#fc_add_send_pm").val(), text: $("#fc_add_text").val() },
			function(data)
			{
				if (data.error == 0)
				{
					$("#fc_add_"+FC_Add.sid).html("Added");
					statusUpdate.find("div[type='status']").html("Friend Code has been added to your List. User has been notified.");
					statusUpdate.slideDown(400);
				} else {
					alert(data.reasons);
				}
				
				FC_Add.close();
			}, "json");
	};
};
var FC_Add = new FC_Add();

/**
 * Class used to handle the Post Comments
 */
function PostComments()
{
	this.ajaxURL = "/ajax/post_comments.php";
	this.post_sid = 0;
	this.container = null
	this.isOpen = false;
	this.comment_count = 0;
	
	/**
	 * Opens the Comments for this Post
	 */
	this.open = function(sid)
	{
		if (!this.isOpen)
		{
			this.post_sid = sid;
			this.isOpen = true;
			
			var height = window.innerHeight > 400 ? 400 : 300;
			
			this.container = $(document.createElement("div"));
			$("body").append(this.container);
			this.container.addClass("popup");
			this.container.css({
				"display": "none",
				"width": "500px",
				"height": height+"px",
				"margin-left": "-250px",
				"margin-top": "-"+Math.floor(height/2)+"px"
			});
			
			var title = $(document.createElement("div"));
			this.container.append(title);
			title.addClass("title");
			title.html("" +
				"<div style='float: left; width: 70%;'>" +
					"Post Comments" +
				"</div>" +
				"<div style='float: left; width: 30%; text-align: right;'>" +
					"<img class='png' src='http://zacisa.info/images/icons/cancel.png' onclick='PostComments.close();' />" +
				"</div>" +
				"<div class='clear'></div>");
			
			var body = $(document.createElement("div"));
			this.container.append(body);
			body.addClass("body");
			body.css({
				"margin-top": "0px",
				"padding": "0px"
			});
			body.html("<img src='/images/ajax-loader.gif' style='margin-left: 230px; margin-top: 180px;' />");
			
			this.container.show(400);
			this.container.draggable({ containment: "body", handle: ".title" });
			
			this.setup();
		}
	};
	
	/**
	 * Closes the Comments Pop-up
	 */
	this.close = function()
	{
		if (this.isOpen)
		{
			this.container.hide(400);
			
			setTimeout(function()
			{
				PostComments.container.remove();
				PostComments.isOpen = false;
			}, 401);
		}
	};
	
	/**
	 * Displays the Comments and the Add field if the User is logged in
	 */
	this.setup = function()
	{
		var height = window.innerHeight > 400 ? 400 : 300;
		$.post(this.ajaxURL,
			{ type: "setup", post_sid: this.post_sid },
			function(data)
			{
				if (data.error == 0)
				{
					PostComments.container.find(".body").html(data.html);
					PostComments.comment_count = data.count;
					
					if (height < 400)
					{
						PostComments.container.find(".body").find("#post_comment_display").css("height", "138px");
					}
				}
			}, "json");
	};
	
	/**
	 * Adds a new Comment
	 */
	this.addComment = function()
	{
		this.container.find("input,textarea").attr("disabled", true);
		
		$.post(this.ajaxURL,
			{ type: "add", post_sid: this.post_sid, text: $("#post_comment_add").val() },
			function(data)
			{
				PostComments.container.find("input,textarea").attr("disabled", false);
				
				if (data.error == 0)
				{
					$("#post_comment_add").val("");
					PostComments.comment_count++;
					$("#post_"+PostComments.post_sid+"_comment_count").html(PostComments.comment_count);
					if (PostComments.comment_count == 1)
					{
						$("#post_comment_display").html(data.html);
					} else {
						$("#post_comment_display").append(data.html);
					}
					document.getElementById("post_comment_display").scrollTop += 10000;
				} else {
					alert(data.reasons);
				}
			}, "json");
	};
	
	/**
	 * Shows/Hides the Edit textarea
	 */
	this.editComment = function(sid)
	{
		var comment = $("#post_comment_"+sid);
		
		//Hide the Display Area and show the Edit Area
		if (comment.find("[type='display']").css("display") != "none")
		{
			comment.find("[type='display']").hide();
			comment.find("[type='edit']").show();
		} else {
			comment.find("[type='display']").show();
			comment.find("[type='edit']").hide();
		}
	};
	
	/**
	 * Updates the Comment and Displays the new Comment
	 */
	this.updateComment = function(sid)
	{
		var comment = $("#post_comment_"+sid);
		this.container.find("input,textarea").attr("disabled", true);
		
		$.post(this.ajaxURL,
			{ type: "update", post_sid: this.post_sid, sid: sid, text: comment.find("textarea").val() },
			function(data)
			{
				PostComments.container.find("input,textarea").attr("disabled", false);
				
				if (data.error == 0)
				{
					comment.find("[type='display']").html(data.html);
					comment.find("[type='display']").show();
					comment.find("[type='edit']").hide();
				} else {
					alert(data.reasons);
				}
			}, "json");
	};
	
	/**
	 * Deletes this Comment
	 */
	this.deleteComment = function(sid)
	{
		$.post(this.ajaxURL,
			{ type: "delete", post_sid: this.post_sid, sid: sid },
			function(data)
			{
				if (data.error == 0)
				{
					PostComments.comment_count--;
					$("#post_"+PostComments.post_sid+"_comment_count").html(PostComments.comment_count);
					$("#post_comment_"+sid).remove();
					
					if (PostComments.comment_count == 0)
					{
						$("#post_comment_display").html("<div style='padding: 4px;'>No Comments Found.</div>");
					}
				} else {
					alert(data.reasons);
				}
			}, "json");
	};
};
var PostComments = new PostComments();

function UploadAdd()
{
	this.ajaxURL = "/ajax/forums_thread_upload.php";
	this.container = null;
	this.isOpen = false;
	
	this.open = function(sid)
	{
		if (!this.isOpen)
		{
			this.isOpen = true;
			this.container = $(document.createElement("div"));
			
			this.container.addClass("popup");
			this.container.css({
				width: "310px",
				height: "80px",
				"margin-left": "-155px",
				"margin-top": "-40px",
				display: "none"
			});

			var title = $(document.createElement("div"));
			this.container.append(title);
			title.addClass("title");
			title.html("" +
				"<div style='float: left; width: 70%;'>" +
					"Upload File" +
				"</div>" +
				"<div style='float: left; width: 30%; text-align: right;'>" +
					"<img class='png' src='http://zacisa.info/images/icons/cancel.png' onclick='UploadAdd.close();' />" +
				"</div>" +
				"<div class='clear'></div>");
			
			var body = $(document.createElement("div"));
			this.container.append(body);
			body.addClass("body");
			body.html("<form target='thread_upload_iframe' method='post' action='"+this.ajaxURL+"' enctype='multipart/form-data'>" +
				"<input type='hidden' name='thread_sid' value='"+sid+"' />" +
				"<input type='hidden' name='type' value='upload' />" +
				"<div style='margin-bottom: 3px;'>Title: <input type='text' name='title' style='width: 250px;' /></div>" +
				"<input type='file' name='file' class='button' /> " +
				"<input type='submit' value='Upload' class='button' />" +
				"</form>");
			
			$("body").append(this.container);
			this.container.show(400);
			this.container.draggable({ containment: "body", handle: ".title" });
		}
	};
	
	this.close = function()
	{
		if (this.isOpen)
		{
			this.container.hide(400);
			
			setTimeout(function()
			{
				UploadAdd.container.remove();
				UploadAdd.isOpen = false;
			}, 401);
		}
	};
	
	this.result = function(data)
	{
		if (data.error == 0)
		{
			this.close();
			$("#thread_upload_files").parent().show();
			$("#thread_upload_files").find("tbody").append("<tr><td valign='top' class='color_column'>"+data.html+"</td></tr>");
		} else {
			alert(data.reasons);
		}
	};
	
	this.remove = function(sid, row)
	{
		$.post(this.ajaxURL,
			{ type: "delete", upload_sid: sid },
			function(data)
			{
				row.parent().parent().remove();
			});
	};
};
var UploadAdd = new UploadAdd();