﻿$(function(){
	var regex = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/ig;
	$("#right div.post div div").each(function(){
		if ($("a", this).length == 0) {
			var html = $(this).html();
			var l = html.length;
			var sp = html.replace(/&nbsp;/gi, " ").replace(/<br\s*\/?>/gi, "\n").match(/\S{10,}/g);
			if (sp) {
				for (var s, i = 0; i < sp.length; i++) {
					s = sp[i];
					if (!s.match(/^(http:\/\/|www\.)/i) && (s.match(/\.(com|org|net)$/i) || s.match(/\.(com|org|net)\//i)))
						s = "http://" + s;
					s = s.replace(/^www\./i, "http://www.");
					if (s.match(regex))
						html = html.replace(sp[i], '<a href="' + s + '" target="_blank">' + sp[i] + '</a>');
				}
				if (html.length != l)
					$(this).html(html);
			}
		}
	});
	$("#right h2 a").click(function(){
		var action = "close";
		if ($(this).html().indexOf("Open") == 0)
			action = "open";
		if (confirm("Are you sure you want to " + action + " this thread?")) {
			$.getJSON("discussion.aspx", {id: dID, a: action}, function(data){
				if (data.success)
					location.href = location.href;
				else
					alert(data.error);
			});
		}
		return false;
	});
	var added = false;
	$("#addfav").click(function(){
		this.blur();
		if (!added) {
			var me = $(this).fadeOut();
			$.getJSON("discussion.aspx", {id: dID, a: "favorite"}, function(data){
				if (data.success) {
					added = true;
					me.addClass("added").html("Added successfully!").fadeIn();
				} else {
					alert(data.error);
					me.fadeIn();
				}
			});
		}
		return false;
	});
	$("#right a.edit").click(function(){
		var cancel = $(this).parent().parent().next().find("img");
		if (cancel.length > 0) {
			cancel.click();
		} else {
			var pid = $(this).parents(".post").attr("id").substring(1);
			$("<div><form method='post' action=''><textarea name='edit' class='textinput'>" + $(this).parent().siblings("div").html().replace(/<br\s*\/?>/gi, "\n") + "</textarea><br /><img src='images/buttons/new/cancel.png' /> <input type='image' src='images/buttons/new/post.png' /></form></div>").hide().insertAfter($(this).parent().parent()).slideDown(function() {
				var me = this;
				setTimeout(function(){
					$("textarea", me)[0].focus();
				}, 100);
			}).children().submit(function(){
				var me = $(this).parent().slideUp();
				var text = $("textarea", me).val();
				if ($.trim(text).length > 0 ) {
					$.ajax({type: "POST", url: "discussion.aspx?id=" + pid + "&a=edit", data: {edit: text}, dataType: "json", success: function(data){
						if (data.success) {
							me.prev().find("div").html(data.edit);
							var dates = me.prev().find("span");
							var i = dates.html().indexOf("Updated");
							if (i > -1)
								dates.html(dates.html().substring(0, i + 8) + data.updated);
							else
								dates.append(" - Updated " + data.updated);
							me.remove();
						} else {
							alert(data.error);
							me.slideDown();
						}
					}});
				} else
					alert("The update may not be empty.");
				return false;
			}).find("img").click(function(){
				$(this).parent().parent().slideUp(function(){
					$(this).remove();
				});
			});
		}
		return false;
	});
	$("#right a.delete").click(function(){
		if (confirm("Are you sure you want to delete this post?")) {
			var me = $(this).parent().slideUp();
			var pid = me.attr("id").substring(1);
			$.getJSON("discussion.aspx", {id: pid, a: "delete"}, function(data){
				if (data.success) {
					if (me.siblings(".post").length == 0)
						location.href = location.href;
					me.remove();
				} else {
					alert(data.error);
					me.slideDown();
				}
			});
		}
		return false;
	});
	$("#htmlok a").click(function(){
		openWindow(this.href, {width: 410, height: 385});
		return false;
	});
	$("form").submit(function(){
		if ($.trim($("form textarea").val()).length == 0) {
			alert("The reply may not be empty.");
			return false;
		}
	});
});
