addLoadEvent( function( )
{
	var pixelsPerSecond = 400;

	if ( withID( "entries" ) && withID( "text_area" ) && parseFloat( withID( "entries" ).offsetHeight ) > parseFloat( withID( "text_area" ).offsetHeight ) )
	{
		if ( withID( "up_arrow" ) )
		{
			withID( "up_arrow" ).onmousedown = function( )
			{
				startScroll( pixelsPerSecond );
			};
		}
		
		if ( withID( "down_arrow" ) )
		{
			withID( "down_arrow" ).onmousedown = function( )
			{
				startScroll( -pixelsPerSecond );
			};
		}
		
		if ( withID( "scroll_bar" ) )
		{
			updateScrollBarPosition( );
	
			withID( "scroll_bar" ).onmousedown = function( event )
			{
				event = getEvent( event );
				
				startScrollBar( event.clientY );
				
				// START http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx
				
				// prevent text selection (except IE)
				return false; 
			
				// END   http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx
			};
		}
	}
	else
	{
		if ( withID( "up_arrow" ) )
		{
			withID( "up_arrow" ).style.display = "none";
		}
		
		if ( withID( "down_arrow" ) )
		{
			withID( "down_arrow" ).style.display = "none";
		}
		
		if ( withID( "scroll_bar" ) )
		{
			withID( "scroll_bar" ).style.display = "none";
		}
	}
} );

var draggingScrollBar = false;
var intervalID = null;
var desiredInterval = 10; // milliseconds
var timeOfLastCall = null; // milliseconds

function startScroll( pixelsPerSecond )
{
	withID( "up_arrow" ).onmouseup = withID( "up_arrow" ).onmouseout = withID( "down_arrow" ).onmouseup = withID( "down_arrow" ).onmouseout = stopScroll;

	scrollText( pixelsPerSecond );
	
	intervalID = setInterval( "scrollText( " + pixelsPerSecond + " )", desiredInterval );
}

function stopScroll( )
{
	withID( "up_arrow" ).onmouseup = withID( "up_arrow" ).onmouseout = withID( "down_arrow" ).onmouseup = withID( "down_arrow" ).onmouseout = null;

	if ( intervalID != null )
	{
		clearInterval( intervalID );
	}
	
	intervalID = null;
	timeOfLastCall = null;
	draggingScrollBar = false;
}

function scrollText( pixelsPerSecond )
{
	var timeOfThisCall = (new Date( )).valueOf( );
	
	if ( withID( "entries" ).style.top == 0 )
	{
		withID( "entries" ).style.top = "0px";
	}
	
	var interval = desiredInterval;
	
	if ( timeOfLastCall != null )
	{
		interval = timeOfThisCall - timeOfLastCall;
	}
		
	var newTop = ( parseFloat( withID( "entries" ).style.top ) + ( pixelsPerSecond * interval / 1000 ) ) + "px";

	setPagePosition( newTop );
	
	updateScrollBarPosition( );
	
	timeOfLastCall = timeOfThisCall;
}

var mouseDragYStart = 0;
var mouseDragPageYStart = 0;
var informationPanel = new InformationPanel( );

function startScrollBar( mouseY )
{
	mouseDragYStart = mouseY;
	mouseDragPageYStart = parseInt( withID( "entries" ).style.top );
	mouseDragPageYStart = isNaN( mouseDragPageYStart ) || mouseDragPageYStart == "" ? 0 : mouseDragPageYStart;
	
	draggingScrollBar = true;
	
	document.body.onmouseup = function( )
	{
		draggingScrollBar = false;
		
		document.body.onmouseup = null;
		document.body.onmousemove = null;
		document.onselectstart = null;
	};

	document.body.onmousemove = function( event )
	{
		informationPanel.addText( "onmousemove", true );
		
		event = getEvent( event );
		
		var mouseYPosition = event.clientY;

		var mouseYOffset = mouseYPosition - mouseDragYStart;
		
		hP = parseFloat( withID( "entries" ).offsetHeight ); // height of page
		hT = parseFloat( withID( "scroll_bar_track" ).offsetHeight ); // height of scroll bar track

		var newTop = ( parseFloat( mouseDragPageYStart ) - parseFloat( mouseYOffset ) * hP / hT );
		
		setPagePosition( newTop );
		
		updateScrollBarPosition( );
	};

	// START http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx
	
	// cancel out any text selections 
	document.body.focus( );

	// prevent text selection in IE
	document.onselectstart = function ( )
	{
		return false;
	};

	// END   http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx
}

function setPagePosition( newTop )
{
	if ( parseFloat( newTop ) >= 0 )
	{
		withID( "entries" ).style.top = "0px";
	}
	else if ( parseFloat( newTop ) <= -( parseFloat( withID( "entries" ).offsetHeight ) - parseFloat( withID( "text_area" ).offsetHeight ) ) )
	{
		withID( "entries" ).style.top = ( -( parseFloat( withID( "entries" ).offsetHeight ) - parseFloat( withID( "text_area" ).offsetHeight ) ) ) + "px";
	}
	else
	{
		withID( "entries" ).style.top = newTop;
	}
}

function updateScrollBarPosition( )
{
	hP = parseFloat( withID( "entries" ).offsetHeight ); // height of page
	hV = parseFloat( withID( "text_area" ).offsetHeight ); // height of view
	yP = parseFloat( withID( "entries" ).style.top ); // y position of top of page
	yP = isNaN( yP ) ? 0 : yP;
	
	yT = parseFloat( getStyle( withID( "scroll_bar_track" ), "top" ) ); // y position of top of scroll bar track
	hT = parseFloat( withID( "scroll_bar_track" ).offsetHeight ); // height of scroll bar track
	yS = yT - yP * hT / hP; // y position of top of scroll bar
	hS = hT * hV / hP; // height of scroll bar
	
	withID( "scroll_bar" ).style.height = hS;
	withID( "scroll_bar" ).style.top = yS;
}

function scrollToEvent( id )
{
	withID( "entries" ).style.top = -withID( "post-" + id ).offsetTop;
	
	updateScrollBarPosition( );
}
