//<script> (This is so TextMate gives me nice JS highlighting.)
$(function(){
	$(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/, ""))
	})
	
	$(".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
	})
	
	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://www.whathappenedinpiedmont.com/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").text())
					count++
					$(".comment_count").text(count)
					var plural = (count == 1) ? "" : "s"
					$(".comment_plural").text(plural)
				}
				$("#last_comment").val(json.comment_id)
				$("#add_comment").loader(true)
				$(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
				})
			})
		} })
	}
	$(".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
	})
})

var Post = {
}

$.fn.loader = function(remove) {
	if (remove) {
		$(this).next().remove()
	} else {
		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://www.whathappenedinpiedmont.com/includes/close.png\" style=\"display: none\" class=\"close\" /><img src=\"http://www.whathappenedinpiedmont.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).width(), 
			height: $(this).height(), 
			background: ($.browser.msie) ? "transparent" : "transparent url('http://www.whathappenedinpiedmont.com/includes/trans.png')", 
			textAlign: "center", 
			filter: ($.browser.msie) ? "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='http://www.whathappenedinpiedmont.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 appendNextPost(minus) {
	var minus = (minus == "") ? 1 : minus ;
	if ($("#posts").length == 0) return;
	$.ajax({ type: "post", url: "http://www.whathappenedinpiedmont.com/includes/ajax.php", data: { action: "view_post", offset: 5 - minus }, success: function(data) {
		$("#posts").append(data)
		$("#posts .post:last").hide().fadeIn("slow")
		var id = $("#posts .post:last").attr("id").replace(/post_/, "")
		
	}, error: function(request){
		if (request.status == 404) {
			$("#next_page_page").fadeOut("fast");
		}
	} });
}

function isError(text) {
	return /HEY_JAVASCRIPT_THIS_IS_AN_ERROR_JUST_SO_YOU_KNOW/.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() {
		var id = $(".comments").attr("id").replace(/comments_/, "")
		if (editing == 0 && notice == 0 && $(".comments").children().size() < 25) {
			$.ajax({ type: "post", dataType: "json", url: "http://www.whathappenedinpiedmont.com/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://www.whathappenedinpiedmont.com/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://www.whathappenedinpiedmont.com/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; Less Options")
						$("#more_options_"+id).slideDown("slow");
					} else {
						$(this).html("More Options &raquo;")
						$("#more_options_"+id).slideUp("slow");
					}
					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://www.whathappenedinpiedmont.com/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://www.whathappenedinpiedmont.com/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").text())
				count--
				$(".comment_count").text(count)
				var plural = (count == 1) ? "" : "s"
				$(".comment_plural").text(plural)
			}
		})
	}
}
//</script>
//</script>