var keypost=1;
var postsOff = new Array();
var postDatesOff = new Array();
postsOff[0] = 0;
postDatesOff[0] = 0;

// EVENT TRIGGERS

document.onkeydown = function(e) {
	var e=window.event || e;
	return keyNavigationIndicator(e);
}

document.onkeyup = function(e) {
	var e=window.event || e;
	keyNavigation(e);
}

// MAIN FUNCTIONS

function keyNavigation(e) {
	
	el = document.getElementById('keynav_indicator');
	el.style.display = 'none';
	
	postsOff = getPostsOff();
	postDatesOff = getPostDatesOff();
	scrolly = getScrollY();
	var makemove = 0;
	
	if ((e.keyCode == 39) || (e.keyCode == 75)) { // Right Arrow or K
		var i = 0;
		while(postsOff[i] < (scrolly + 10)) {
			i = i + 1;
		}
		if (i>0 && i<postsOff.length) {
			makemove = 1;
		}
		if (makemove == 1) {
			window.scrollTo(0,(postsOff[i] + 120));
		}
	}
	
	if ((e.keyCode == 37) || (e.keyCode == 74)) { // Left Arrow or J
		var i = postsOff.length-1;
		while(scrolly < (postsOff[i] + 130)) {
			i = i - 1;
		}
		if (i>0 && i<postsOff.length) {
			makemove = 1;
		}
		if (makemove == 1) {
			window.scrollTo(0,(postsOff[i] + 120));
		}
	}
	
	if (e.keyCode == 34) { // PgDown
		var i = 0;
		while(postDatesOff[i] < (scrolly + 10)) {
			i = i + 1;
		}
		if (i>0 && i<postDatesOff.length) {
			makemove = 1;
		}
		if (makemove == 1) {
			window.scrollTo(0,(postDatesOff[i] + 150));
		}
		if (i==postDatesOff.length) {
			window.scrollTo(0,60000);
		}
	}
	
	if (e.keyCode == 33) { // PgUp
		var i = postDatesOff.length-1;
		while(scrolly < (postDatesOff[i] + 160)) {
			i = i - 1;
		}
		if (i>0 && i<postDatesOff.length) {
			makemove = 1;
		}
		if (makemove == 1) {
			window.scrollTo(0,(postDatesOff[i] + 150));
		}
		if (i == 0) {
			window.scrollTo(0,0);
		}
	}
	
}

function keyNavigationIndicator(e) {
	el = document.getElementById('keynav_indicator');
	ind = getWindowSize();
	bodyHeight = getBodyHeight();
	indY = ind[1] / 2 - 70;
	indX = ind[0] / 2 - 150;
	el.style.cssText = 'top: '+ indY + 'px; left: '+ indX + 'px;';
		
	postsOff = getPostsOff();
	postsNum = postsOff.length - 1;
	postDatesOff = getPostDatesOff();
	postDatesNum = postDatesOff.length - 1;
	scrolly = getScrollY();
	
	if (postsNum > 1) {
		if ((e.keyCode == 37) || (e.keyCode == 74)) { // Left Arrow or J
			if (scrolly > (postsOff[1] + 160)) {
				el.innerHTML = '<b>&larr; Newer post</b>';
				el.style.display = 'block';
			}
			
		}
		if ((e.keyCode == 39) || (e.keyCode == 75)) { // Right Arrow or K
			if (scrolly < (postsOff[postsNum] + 10)) {
				el.innerHTML = '<b>Older post &rarr;</b>';
				el.style.display = 'block';
			}
			if (scrolly >= (postsOff[postsNum] + 10)  || scrolly + ind[1] >= bodyHeight) {
				el.style.display = 'none';
			}
		}
	}

	if (postDatesNum > 1) {	
		if (e.keyCode == 33) { // PgUp
			if (scrolly >= 0) {
				el.innerHTML = '<b>Page top</b>';
				el.style.display = 'block';
			}
			if (scrolly > (postDatesOff[1] + 160)) {
				el.innerHTML = '<b>&larr; Newer date</b>';
				el.style.display = 'block';
			}
			return false;
		}
		if (e.keyCode == 34) { // PgDown
			if (scrolly < (postDatesOff[postDatesNum] + 10)) {
				el.innerHTML = '<b>Previous date &rarr;</b>';
				el.style.display = 'block';
			}
			if (scrolly >= (postDatesOff[postDatesNum] + 10) || scrolly + ind[1] >= bodyHeight) {
				el.innerHTML = '<b>Page bottom</b>';
				el.style.display = 'block';
			}
			return false;
		}
	}
}


// AUX FUNCTIONS

function getPostsOff() {
	var i = 1;
	while(document.getElementById('post'+i)) {
		postsOff[i] = document.getElementById('post'+i).offsetTop;
		i = i + 1;
	}
	
	return postsOff;
}

function getPostDatesOff() {
	var i = 1;
	while(document.getElementById('postdate'+i)) {
		postDatesOff[i] = document.getElementById('postdate'+i).offsetTop;
		i = i + 1;
	}
	
	return postDatesOff;
}

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    }
	return scrOfY;
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

function getBodyHeight() {
	if (document.documentElement.scrollHeight) {
		var theheight = document.documentElement.scrollHeight;
	} else if (document.documentElement.offsetHeight) {
		var theheight = document.documentElement.offsetHeight;
	}
	return theheight;
}