//<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
	})
	
})

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://labsix.net/beeb/includes/close.png\" style=\"display: none\" class=\"close\" /><img src=\"http://labsix.net/beeb/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://labsix.net/beeb/includes/trans.png')", 
			textAlign: "center", 
			filter: ($.browser.msie) ? "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='http://labsix.net/beeb/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://labsix.net/beeb/includes/ajax.php", data: { action: "view_post", context: "archive", year: "2008", month: "12", offset: 10 - 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>