function getPost(next) {
	//clearTimeout(window.nextHash);
	$("#content").scrollTo("#post-"+next, 1000, {axis:'xy'});
	$("#content").scrollTo("-=70px", 1000, {axis:'xy'});
	//var window.nextHash = setTimeout(function(){doHash(next)}, 1010);
	setTimeout(function(){doHash(next)}, 1010);
	//updateNav(next);
	return false;
}

function doHash(next) {
	window.location.hash = "post-"+next;
}

function checkHash() {
	if (window.location.hash) {
		if ($(window.location.hash).length < 1) {
			calcArchive(window.location.hash.slice(-1));
		}
		$("#content").stop().scrollTo(window.location.hash, 1000, {axis:'xy'});
		$("#content").stop().scrollTo("-=70px", 1000, {axis:'xy'});
		var post = window.location.hash.slice(window.location.hash.indexOf("-")+1);
		updateNav(post);
		return false;
	}
}

$(function() {
	checkHash();
	newPage();
});


function updateNav(currentPost) {
	if (window.location.pathname.substr(0, 9) == "/archive/") {
		var curURL = "&archive="+window.location.pathname.substr(9, 2);
	}
	$.get("/?total=1&ajax=1"+curURL, function(total){
		if (currentPost >= total -1) {
			$("#scrollme-left > *").fadeOut("slow");
		}
		else {
			if (currentPost < total -1) {
				$("#scrollme-left").html("<a style=\"display:none;\" href=\"index.php?post="+(parseInt(currentPost) +1)+"\" onclick=\"return getPost("+(parseInt(currentPost) +1)+");\">&lt;&nbsp;previous</a>");
				$("#scrollme-left > *").fadeIn("slow");
			}
		}
		var unit = String(currentPost).slice(-1);
		if (unit <= 0) {
			$("#scrollme-right > *").fadeOut("slow");
			//$("#content").scrollTo("+=70px", 1000);
		}
		if (unit > 0) {
			$("#scrollme-right").html("<a style=\"display:none;\" href=\"index.php?post="+(parseInt(currentPost) -1)+"\" onclick=\"return getPost("+(parseInt(currentPost) -1)+");\">next&nbsp;&gt;</a>");
			$("#scrollme-right > *").fadeIn("slow");
		}
		updateTitle(currentPost);
	});
}

function updateTitle(currentPost) {
	document.title = "Skywave Systems - "+$("#post-"+currentPost+" h1").text();
}

// borrowed this piece of loveliness from http://www.bennadel.com/index.cfm?dax=blog:1520.view

	// Our plugin will be defined within an immediately
	// executed method.
	(
		function( $ ){
			// Default to the current location.
			var strLocation = window.location.href;
			var strHash = window.location.hash;
			var strPrevLocation = "";
			var strPrevHash = "";
 
			// This is how often we will be checkint for
			// changes on the location.
			var intIntervalTime = 100;
 
			// This method removes the pound from the hash.
			var fnCleanHash = function( strHash ){
				return(
					strHash.substring( 1, strHash.length )
					);
			}
 
			// This will be the method that we use to check
			// changes in the window location.
			var fnCheckLocation = function(){
				// Check to see if the location has changed.
				if (strLocation != window.location.href){
 
					// Store the new and previous locations.
					strPrevLocation = strLocation;
					strPrevHash = strHash;
					strLocation = window.location.href;
					strHash = window.location.hash;
 
					// The location has changed. Trigger a
					// change event on the location object,
					// passing in the current and previous
					// location values.
					$( window.location ).trigger(
						"change",
						{
							currentHref: strLocation,
							currentHash: fnCleanHash( strHash ),
							previousHref: strPrevLocation,
							previousHash: fnCleanHash( strPrevHash )
						}
						);
 
				}
			}
 
			// Set an interval to check the location changes.
			setInterval( fnCheckLocation, intIntervalTime );
		}
	)( jQuery );

	// Bind a change handler to the window location.
	$( window.location ).bind(
		"change",
		function( objEvent, objData ){
			checkHash();
		}
		);

// end of borrowed js

function newPage() {
	$(".post > a").attr("target", "_blank");
}

function calcArchive(post) {
	var archive = Math.ceil((parseInt(post)+1)/10)*10;
	window.location = "/archive/"+archive+"/#post-"+post;
}