//<script type="text/javascript"> (This is so TextMate gives me nice JS highlighting.)
$(function(){
	// Scan AJAX responses for errors.
	$(document).ajaxComplete(function(imconfused, request){
		var response = request.responseText
		if (isError(response))
			alert(response.replace(/(HEY_JAVASCRIPT_THIS_IS_AN_ERROR_JUST_SO_YOU_KNOW|<([^>]+)>\n?)/gm, ""))
	})

	$(".toggle_admin").click(function(){
		if (!$("#admin_bar:visible").size()) {
			$("#admin_bar").slideDown()
			Cookie.destroy("chyrp_hide_admin")
		} else {
			$("#admin_bar").slideUp()
			Cookie.set("chyrp_hide_admin", "true", 30)
		}
		return false
	})

	$(".post_edit_link").click(function(){
		var id = $(this).attr("id").replace(/post_edit_/, "")
		Post.edit(id)
		return false
	})

	$(".post_delete_link").click(function(){
		if (!confirm("Are you sure you want to delete this post?\n\nIt cannot be restored if you do this. If you wish to hide it, save it as a draft.")) return false
		var id = $(this).attr("id").replace(/post_delete_/, "")
		Post.destroy(id)
		return false
	})
//<script>
	if ($(".comments").size()) {
		var updater = setInterval("Comment.reload()", 30000);
		$("#add_comment").append($(document.createElement("input")).attr({ type: "hidden", name: "ajax", value: "true", id: "ajax" }));
		$("#add_comment").ajaxForm({ dataType: "json", resetForm: true, beforeSubmit: function(){
			$("#add_comment").loader();
		}, success: function(json){
			$.post("http://unstyled.co.uk/includes/ajax.php", { action: "show_comment", comment_id: json.comment_id, reason: "added" }, function(data) {
				if ($(".comment_count").size() && $(".comment_plural").size()) {
					var count = parseInt($(".comment_count:first").text())
					count++
					$(".comment_count").text(count)
					var plural = (count == 1) ? "" : "s"
					$(".comment_plural").text(plural)
				}
				$("#last_comment").val(json.comment_id)
				$(data).appendTo(".comments").hide().fadeIn("slow")
				$("#comment_delete_"+json.comment_id).click(function(){
					if (!confirm("Are you sure you want to delete this comment?\n\nIt cannot be restored if you do this.")) return false
					Comment.destroy(json.comment_id)
					return false
				})
			})
		}, complete: function(){
			$("#add_comment").loader(true)
		} })
	}
	$(".comment_edit_link").click(function(){
		var id = $(this).attr("id").replace(/comment_edit_/, "")
		Comment.edit(id)
		return false
	})
	$(".comment_delete_link").click(function(){
		if (!confirm("Are you sure you want to delete this comment?\n\nIt cannot be restored if you do this.")) return false
		var id = $(this).attr("id").replace(/comment_delete_/, "")
		Comment.destroy(id)
		return false
	})
//</script>
})

var Post = {
  edit: function(id) {
		$("#post_"+id+" .target, #post_"+id+".target").loader()
		$.post("http://unstyled.co.uk/includes/ajax.php", { action: "edit_post", id: id }, function(data) {
			$("#post_"+id+" .target, #post_"+id+".target").loader(true).fadeOut("fast", function(){ $(this).html(data).fadeIn("fast", function(){
				$("#more_options_link_"+id).click(function(){
					if ($("#more_options_"+id).css("display") == "none") {
						$(this).html("&laquo; Fewer Options")
						$("#more_options_"+id).slideDown("slow")
					} else {
						$(this).html("More Options &raquo;")
						$("#more_options_"+id).slideUp("slow")
					}
					return false
				})
				$("#post_edit_"+id).ajaxForm({ beforeSubmit: function(){
					$("#post_"+id+" .target, #post_"+id+".target").loader()
				}, success: function(response){
					if (isError(response)) return $("#post_"+id+" .target, #post_"+id+".target").loader(true)
						$.post("http://unstyled.co.uk/includes/ajax.php", { action: "view_post", context: "all", id: id, reason: "edited" }, function(data) {
							$("#post_"+id+" .target, #post_"+id+".target").loader(true)
							$("#post_"+id+" .target, #post_"+id+".target").fadeOut("fast", function(){ $(this).html(data).fadeIn("fast", function(){
								$("#post_edit_"+id).click(function(){
									Post.edit(id)
									return false
								})
								$("#post_delete_"+id).click(function(){
									if (!confirm("Are you sure you want to delete this post?\n\nIt cannot be restored if you do this. If you wish to hide it, save it as a draft.")) return false
									Post.destroy(id)
									return false
								})
							}) })
						})
				} })
				$("#post_cancel_edit_"+id).click(function(){
					$("#post_"+id+" .target, #post_"+id+".target").loader()
					$.post("http://unstyled.co.uk/includes/ajax.php", { action: "view_post", context: "all", id: id, reason: "cancelled" }, function(data) {
						$("#post_"+id+" .target, #post_"+id+".target").loader(true)
						$("#post_"+id+" .target, #post_"+id+".target").fadeOut("fast", function(){ $(this).html(data).fadeIn("fast", function(){
							$("#post_edit_"+id).click(function(){
								Post.edit(id)
								return false
							})
							$("#post_delete_"+id).click(function(){
								if (!confirm("Are you sure you want to delete this post?\n\nIt cannot be restored if you do this. If you wish to hide it, save it as a draft.")) return false
								Post.destroy(id)
								return false
							})
						}) })
					})
					return false
				})
			}) })
		})
	}
	,
	destroy: function(id) {
		$("#post_"+id+" .target, #post_"+id+".target").loader()
		$.post("http://unstyled.co.uk/includes/ajax.php", { action: "delete_post", id: id }, function(response){
			$("#post_"+id+" .target, #post_"+id+".target").loader(true)
			if (isError(response)) return
			$("#post_"+id).animate({ height: "hide", opacity: "hide" }, function(){
				$(this).remove()
			})
			appendNextPost()
		})
		window.location = "http://unstyled.co.uk"
	}
}

$.fn.loader = function(remove) {
	if (remove) {
		$(this).next().remove()
		return this
	}

	var offset = $(this).offset()
	var width = $(this).width()
	var loading_top = ($(this).height() / 2) - 11
	var loading_left = ($(this).width() / 2) - 63

	$(this).after("<div class=\"load_overlay\"><img src=\"http://unstyled.co.uk/includes/close.png\" style=\"display: none\" class=\"close\" /><img src=\"http://unstyled.co.uk/includes/loading.gif\" style=\"display: none\" class=\"loading\" /></div>")

	$(".load_overlay .loading").css({
		position: "absolute",
		top: loading_top+"px",
		left: loading_left+"px",
		display: "inline"
	})

	$(".load_overlay .close").css({
		position: "absolute",
		top: "3px",
		right: "3px",
		color: "#fff",
		cursor: "pointer",
		display: "inline"
	}).click(function(){ $(this).parent().remove() })

	$(".load_overlay").css({
		position: "absolute",
		top: offset.top,
		left: offset.left,
		zIndex: 100,
		width: $(this).width(),
		height: $(this).height(),
		background: ($.browser.msie) ? "transparent" : "transparent url('http://unstyled.co.uk/includes/trans.png')",
		textAlign: "center",
		filter: ($.browser.msie) ? "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='http://unstyled.co.uk/includes/trans.png');" : ""
	})

	return this
}

var Cookie = {
	set: function(name, value, expires) {
		var today = new Date()
		today.setTime( today.getTime() )

		if (expires)
			expires = expires * 1000 * 60 * 60 * 24

		var expires_date = new Date(today.getTime() + (expires))

		document.cookie = name+"="+escape(value)+
		                  ((expires) ? ";expires="+expires_date.toGMTString() : "" )+";path=/"
	},
	destroy: function(name) {
		document.cookie = name+"=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT"
	}
}

function appendNextPost() {
}

function isError(text) {
	return /HEY_JAVASCRIPT_THIS_IS_AN_ERROR_JUST_SO_YOU_KNOW/m.test(text);
}

//<script>
var ap_instances = new Array();

function ap_stopAll(playerID) {
	for(var i = 0;i<ap_instances.length;i++) {
		try {
			if(ap_instances[i] != playerID) document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 1);
			else document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 0);
		} catch( errorObject ) {
			// stop any errors
		}
	}
}

function ap_registerPlayers() {
	var objectID;
	var objectTags = document.getElementsByTagName("object");
	for(var i=0;i<objectTags.length;i++) {
		objectID = objectTags[i].id;
		if(objectID.indexOf("audioplayer") == 0) {
			ap_instances[i] = objectID.substring(11, objectID.length);
		}
	}
}

var ap_clearID = setInterval( ap_registerPlayers, 100 );
//</script>
//<script>
var editing = 0
var notice = 0
var Comment = {
	reload: function() {
		if ($(".comments").attr("id") == undefined) return;
		var id = $(".comments").attr("id").replace(/comments_/, "")
		if (editing == 0 && notice == 0 && $(".comments").children().size() < 25) {
			$.ajax({ type: "post", dataType: "json", url: "http://unstyled.co.uk/includes/ajax.php", data: "action=reload_comments&post_id="+id+"&last_comment="+$("#last_comment").val(), success: function(json) {
				$.each(json.comment_ids, function(i, id) {
					$("#last_comment").val(id)
					$.post("http://unstyled.co.uk/includes/ajax.php", { action: "show_comment", comment_id: id }, function(data){
						$(data).appendTo(".comments").hide().fadeIn("slow")
					})
				})
			} })
		}
	},
	edit: function(id) {
		editing++
		$("#comment_"+id).loader()
		$.post("http://unstyled.co.uk/includes/ajax.php", { action: "edit_comment", comment_id: id }, function(data) {
			if (isError(data)) return $("#comment_"+id).loader(true)
			$("#comment_"+id).loader(true).fadeOut("fast", function(){ $(this).html(data).fadeIn("fast", function(){
				$("#more_options_link_"+id).click(function(){
					if ($("#more_options_"+id).css("display") == "none") {
						$(this).html("&laquo; Fewer Options")
						$("#more_options_"+id).slideDown("slow");
					} else {
						$(this).html("More Options &raquo;")
						$("#more_options_"+id).slideUp("slow");
					}
					return false;
				})
				$("#comment_cancel_edit_"+id).click(function(){
					$("#comment_"+id).loader()
					$.post("http://unstyled.co.uk/includes/ajax.php", { action: "show_comment", comment_id: id }, function(data){
						$("#comment_"+id).replaceWith(data)
						$("#comment_"+id).loader(true)
						$("#comment_edit_"+id).click(function(){
							Comment.edit(id)
							return false
						})
						$("#comment_delete_"+id).click(function(){
							notice++
							if (!confirm("Are you sure you want to delete this comment?\n\nIt cannot be restored if you do this.")) return notice--
							Comment.destroy(id)
							return false
						})
					})
				})
				$("#comment_edit_"+id).ajaxForm({ beforeSubmit: function(){
					$("#comment_"+id).loader()
				}, success: function(response){
					editing--
					if (isError(response)) return $("#comment_"+id).loader(true)
					$.post("http://unstyled.co.uk/includes/ajax.php", { action: "show_comment", comment_id: id, reason: "edited" }, function(data) {
						if (isError(data)) return $("#comment_"+id).loader(true)
						$("#comment_"+id).loader(true)
						$("#comment_"+id).fadeOut("fast", function(){ $(this).replaceWith(data).fadeIn("fast", function(){
							$("#comment_edit_"+id).click(function(){
								Comment.edit(id)
								return false
							})
							$("#comment_delete_"+id).click(function(){
								notice++
								if (!confirm("Are you sure you want to delete this comment?\n\nIt cannot be restored if you do this.")) return notice--
								Comment.destroy(id)
								return false
							})
						}) })
					})
				} })
			}) })
		})
	},
	destroy: function(id) {
		notice--
		$("#comment_"+id).loader()
		$.post("http://unstyled.co.uk/includes/ajax.php", { action: "delete_comment", id: id }, function(response){
			$("#comment_"+id).loader(true)
			if (isError(response)) return
			$("#comment_"+id).animate({ height: "hide", opacity: "hide" })

			if ($(".comment_count").size() && $(".comment_plural").size()) {
				var count = parseInt($(".comment_count:first").text())
				count--
				$(".comment_count").text(count)
				var plural = (count == 1) ? "" : "s"
				$(".comment_plural").text(plural)
			}
		})
	}
}
//</script>
//</script>
