
var ie = ( document.all ) ? true : false;

var posts = null;
var cur = 0;

function newPost( id, url )
{
	if ( posts == null )
		posts = new Array( );

	errMsg( id + " " + url + " " + posts.length, true );

	$( id ).attr( "_index", posts.length );
	$( id ).attr( "_url", url );
	posts[ posts.length ] = id;
	
	
	/*$( id + "handle" ).click( function( ) {
		var content = "#" + $( this ).attr( "id" ).replace( "handle", "" );
		errMsg( content, true );
		hideAllPosts( );
		showPost( content );
	} );*/
}

function hideAllPosts( )
{
	for ( i = 0; i < posts.length; i++ )
	{
		//$( posts[ i ] ).hide( );
		$( posts[ i ] + "main" ).removeClass( "messageSelected" );
		$( posts[ i ] + "main" ).addClass( "message" );
	}
}

function showPost( id )
{
	errMsg( "id= " + id, true );
	$( id ).show( );
	if ( posts.length > 1 )
	{
		$( id + "main" ).removeClass( "message" );
		$( id + "main" ).addClass( "messageSelected" );
	}
	var offset = $( id + "main" ).offset( );
	if ( offset )
	{
		var i = offset.top;
		errMsg( "offset top = " + i, true );
	}
	else
	{
		errMsg( "no offset top", true );
		var i = $( id + "main" ).height( );
	}
	var j = $( id + "main" ).height( ) + 2;
	var k = window.screen.availHeight;
	var l = window.pageYOffset;
	var m = window.innerHeight;
	if ( window.pageYOffset != 0 )
	{
		if ( l > i )
			window.scroll(0,i);
		else if ( ( i + j ) > ( m + l ) )
		{
			window.scroll(0,(j+i)-(m));
		}
	}
	else if ( ( i + j ) > m )
	{
		window.scroll(0,(j+i)-(m));
	}
	
	cur = $( id ).attr( "_index" );
	errMsg( cur, true ); 
}

var nk={
	Rc:function(a){return a["document"].body.clientHeight},
	Sc:function(a){return a["document"]["documentElement"].clientHeight},
	Fc:function(a){return a.innerHeight}
};

function firstPost( )
{
	if ( posts != null )
	{
		cur = 0;
		hideAllPosts( );
		showPost( posts[ cur ] );
	}
}

function lastPost( )
{
	if ( posts != null )
	{
		cur = posts.length-1;
		hideAllPosts( );
		showPost( posts[ cur ] );
	}
}

function nextPost( )
{
	if ( posts != null && posts.length > 1 )
	{
		if ( cur < posts.length-1 )
		{
			cur++;
			hideAllPosts( );
			errMsg( posts[ cur ], true );
			showPost( posts[ cur ] );
		}
		else
		{
			firstPost( );
		}
	}
}

function prevPost( )
{
	if ( posts != null && posts.length > 1 )
	{
		if ( cur > 0 )
		{
			cur--;
			hideAllPosts( );
			showPost( posts[ cur ] );
		}
		else
		{
			lastPost( );
		}
	}
}

var gstate = false;
var nonav = false;

function navoff( )
{
	nonav = true;
}

function navon( )
{
	nonav = false;
}

$( document ).keydown( function( event )
{
	var CC = event.keyCode
	errMsg( "nonav: " + nonav, true );
	if ( nonav ) return;
	
	if ( gstate )
	{
		switch( CC )
		{
			case 72: //h
				document.location.href = "/";
				break;
			default:
				gstate = false;
		}
	}
	else
	{
		switch( CC )
		{
			case 71: //g
				gstate = true;
				break;
			case 74: //j
				nextPost( );
				break;
			case 75: //k
				prevPost( );
				break;
			case 82: //r
				document.location.href = document.location.href;
				break;
			case 86: //v
				document.location.href = $( posts[ cur ] ).attr( "_url" );
				break;
			case 191: /* / */
				$( "#search" ).focus( );
				return false;
			default:
		}
	}
} );

$( document ).ready( function( )
{
	//debugToggle( );
	$( "#pi" ).html( "<img src='/images/pi.png' alt='pi' onclick='location.href=\"/admin/\"'/>" );
} );

