var DELAY_AFTER_KEYDOWN = 50;  // in ms (1000 = 1s)
var ALT_KEY = 18;

var SHIFT_X_KEY     = 88;
var PAGE_UP_KEY     = 33;
var PAGE_DOWN_KEY   = 34;
var ARROW_UP_KEY    = 38;
var ARROW_DOWN_KEY  = 40;
var ARROW_LEFT_KEY  = 37;
var ARROW_RIGHT_KEY = 39;

var ns4 = (document.layers)? true:false;
var ie4 = (document.all)? true:false;

var current_x = 0; // Know where we are, scrolling around...
var current_y = 0; // Know where we are, scrolling around...

var last_x = 0; // So we remember where we were the last time we moved...
var last_y = 0; // So we remember where we were the last time we moved...


document.onkeydown = handleKeyDown;
window.onscroll    = handleScroll;

if (ns4) {
  document.captureEvents(Event.KEYDOWN);
}


function handleScroll () {
    current_x = document.body.scrollLeft || window.pageXOffset;
    current_y = document.body.scrollTop || window.pageYOffset;
}

/* **************************************************
   handleKeyDown()
   Responds to key press events.

 ************************************************** */

function handleKeyDown(e) {
	var pushed_key = 'default';
	if (ie4) {
		pushed_key = event.keyCode;
	} else {
		pushed_key = e.which;
	}
	setTimeout("getScrollPos("+pushed_key+")", DELAY_AFTER_KEYDOWN);
}


function getScrollPos(pushed) {
	if (!ie4) { return; }
	
	// Note: all of these document.body.scrollTop and
	// document.body.scrollLeft entries are IE-specific, and will need a
	// netscape check using self.pageYOffset (and X)... I think.
		
	if(current_y == last_y) { // Then we're at the end, move on:
		if (pushed == PAGE_DOWN_KEY  || pushed == ARROW_DOWN_KEY) {
			parent.parent.navbar.handleNextPageClick();
		}
		if (pushed == PAGE_UP_KEY || pushed == ARROW_UP_KEY){
			parent.parent.navbar.handlePreviousPageClick();
		}
	}
	
	if(current_x == last_x) { // Then we're at the end, move on:
		if (pushed == ARROW_LEFT_KEY) {
			parent.parent.navbar.handlePreviousPageClick();
		}
		if (pushed == ARROW_RIGHT_KEY){
			parent.parent.navbar.handleNextPageClick();
		}
	}
	
	last_x = current_x;
	last_y = current_y;
}

function KeyCheck(e) {
	
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	switch(KeyID) {
      
		case 16:
			$('debugger_input_2').value = "Shift";
		break; 
		
		case 17:
			$('debugger_input_2').value = "Ctrl";
		break;
		
		case 18:
			$('debugger_input_2').value = "Alt";
		break;
		
		case 19:
			$('debugger_input_2').value = "Pause";
		break;
		
		case 37:
			$('debugger_input_2').value = "Arrow Left";
		break;
		
		case 38:
			$('debugger_input_2').value = "Arrow Up";
		break;
		
		case 39:
			$('debugger_input_2').value = "Arrow Right";
		break;
		
		case 40:
			$('debugger_input_2').value = "Arrow Down";
		break;
	}
}

function KeyCodeCheck(e) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if(e) {
		keycode = e.which;
	}
	
	$('debugger_input_1').value = keycode;
}


document.onkeyup	= KeyCheck;
document.onkeydown	= KeyCodeCheck;
