function showbox(elmnt) {
	document.getElementById('hiddencats').style.display = 'block';
}

function hidebox() {
	document.getElementById('hiddencats').style.display = 'none';
}

function fixURL() {
	parts = window.location.href.split("#/");
	if(parts.length > 1) {
		window.location.href = parts[parts.length - 1];
	}
}

function changeURL(href) {
	url = window.location.href.split("#/");
	for(var i in url) {
		if(url[i] == href) {
			url.splice(i, 1);
		}
	}
	window.location.href = url.join("#/") + "#/" + href;
}

if(typeof jQuery == "function") {
	jQuery(document).ready(function($) {
		fixURL();
		$("#post-container").css("display", "block");
		init(jQuery, "body");
	});
	
	function init($, id) {
		id = (typeof(id) == 'undefined') ? "body" : id;
		
		$(".morecomments a").click(function() {
			href = $(this).attr('href');
			url = href.split("#")[0];
			
			stylesheet_uri = $("link[rel='stylesheet']:eq(0)").attr("href").split("/");
			stylesheet_uri.length--;
			img = "<img src='"+stylesheet_uri.join("/") + "/images/indicator_small.gif"+"' alt='Loading' />";
			$(this).parent().css("text-align", "center").html(img).parent().load(url + " div#comments #commentlist");
			return false;
		});
		
		$("a.respondlink").click(function() {
			post_id = $(this).attr('id').split("-")[1];
			$(this).parent().next().next().css("display", "none");
			$("#commentform-" + post_id).css("display", "block");
			$("#commentform-" + post_id + " .focus:first").focus();
			return false;
		});
		
		$(".respondtext").click(function() {
			$(this).parent().css("display", "none");
			$(this).parent().prev().prev().children("a.respondlink").click();
		});
		
		$("a.nextpost").click(function() {
			stylesheet_uri = $("link[rel='stylesheet']:eq(0)").attr("href").split("/");
			stylesheet_uri.length--;
			img = "&nbsp;<img src='"+stylesheet_uri.join("/") + "/images/indicator_small.gif"+"' alt='Loading' />";
			$(this).after(img);
			href = $(this).attr("href");
			link = $(this);
			$(".post-list").removeClass("post-list");
			$(this).parent().parent().before("<div class='older'></div>");
			$(this).parent().parent().prev().load(href + " .post-list", {}, function() {
				init(jQuery, ".post-list");
				link.next().remove();
			});
			return false;
		});
		
		$(id + " a").not(".nextpost").not(".notajax").each(function() {
			site = $("meta[name='home']").attr("content");
			dashboard = $("meta[name='url']").attr("content") + "/wp-admin";
			wplogin = $("meta[name='url']").attr("content") + "/wp-login.php";
			if (
				$(this).attr('href') != '#' && //it's not a '#' only link
				$(this).attr('href').indexOf(site) == 0 && //it's an internal link
				$(this).attr('href').indexOf(dashboard) == -1 && //it's not a link to dashboard
				$(this).attr('href').indexOf(wplogin) == -1 //it's not a link to wp-login.php
			) {
				$(this).click(function() {
					hidebox();
					href = $(this).attr("href");
					if($(this).parent().hasClass("cat-item")) {
						$(".cat-item").removeClass("current-cat");
						$(this).parent().addClass("current-cat");
					}
					
					stylesheet_uri = $("link[rel='stylesheet']:eq(0)").attr("href").split("/");
					stylesheet_uri.length--;
					img = "<img src='"+stylesheet_uri.join("/") + "/images/indicator_large.gif"+"' alt='Loading' />";
					$("#post-container").html(img).load(href + " #posts", {}, function() {
						document.title = $("input[type='hidden'][name='title']").attr("value");
						
						site = (site.charAt(site.length - 1) == "/") ? site : site + "/";
						href = href.replace(site, "");
						changeURL(href);
						init($, "#posts"); //re-init the database
					});
					return false;
				});
			}
		});
	}
}
