<!-- --><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, #controls:visible").size()) {
			$("#admin_bar, #controls").slideDown()
			Cookie.destroy("hide_admin")
		} else {
			$("#admin_bar, #controls").slideUp()
			Cookie.set("hide_admin", "true", 30)
		}
		return false
	})

	prepare_ajax_links();


})

var Post = {
	delete_animations: { height: "hide", opacity: "hide" },
	delete_wrap: "<div></div>",
	edit: function(id) {
		$("#post_"+id).loader()
		$.post("http://bitbang.geradorzero.com/includes/ajax.php", { action: "edit_post", id: id }, function(data) {
			$("#post_"+id).loader(true).fadeOut("fast", function(){
				$(this).replaceWith(data)
				$("#post_edit_form_"+id).css("opacity", 0).animate({ opacity: 1 }, 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_form_"+id).ajaxForm({ beforeSubmit: function(){
						$("#post_edit_form_"+id).loader()
					}, success: function(response){
						if (isError(response))
							return $("#post_edit_form_"+id).loader(true)

						if ($("#post_edit_form_"+id+" select#status").val() == "draft") {
							$("#post_edit_form_"+id).loader(true).fadeOut("fast")
							alert("Post has been saved as a draft.")
						} else {
							$.post("http://bitbang.geradorzero.com/includes/ajax.php", { action: "view_post", context: "all", id: id, reason: "edited" }, function(data) {
								$("#post_edit_form_"+id).loader(true).fadeOut("fast", function(){
									$(this).replaceWith(data)
									$("#post_"+id).hide().fadeIn("fast", function(){
										prepare_ajax_links(id)
									})
								})
							})
						}
					}
				})
				$("#post_cancel_edit_"+id).click(function(){
					$("#post_edit_form_"+id).loader()
					$.post("http://bitbang.geradorzero.com/includes/ajax.php", { action: "view_post", context: "all", id: id, reason: "cancelled" }, function(data) {
						$("#post_edit_form_"+id).loader(true).fadeOut("fast", function(){
							$(this).replaceWith(data)
							$(this).hide().fadeIn("fast", function(){
								prepare_ajax_links(id)
							})
						})
					})
					return false
				})
			}) })
		})
	},
	destroy: function(id) {
		$("#post_"+id).loader()
		$.post("http://bitbang.geradorzero.com/includes/ajax.php", { action: "delete_post", id: id }, function(response){
			$("#post_"+id).loader(true)
			if (isError(response)) return

			if (Post.delete_wrap != "")
				$("#post_"+id).wrap(Post.delete_wrap).parent().animate(Post.delete_animations, function(){
					$(this).remove()
				})
			else
				$("#post_"+id).animate(Post.delete_animations, function(){
					$(this).remove()
				})
		})
	}
}

function prepare_ajax_links(id) {
	if (id != null) {
		$("#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
		})
	} else {
		$(".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
		})
	}
}

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

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

	$(this).after("<div class=\"load_overlay\"><img src=\"http://bitbang.geradorzero.com/includes/close.png\" style=\"display: none\" class=\"close\" /><img src=\"http://bitbang.geradorzero.com/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).outerWidth(),
		height: $(this).outerHeight(),
		background: ($.browser.msie) ? "transparent" : "transparent url('http://bitbang.geradorzero.com/includes/trans.png')",
		textAlign: "center",
		filter: ($.browser.msie) ? "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='http://bitbang.geradorzero.com/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 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>
