window.onload = function() {
	key_up=false
	key_right=false
	key_down=false
	key_left=false
	key_shift=false
	key_control=false
	
	key_order=new Array()
	for (var i=0;i<4;i++) {
		key_order[i]=0
	}
	
	document.onkeydown=keydown
	document.onkeyup=keyup
}

function getkeycode(e) {
  var keycode
  if (window.event) keycode=window.event.keyCode
  else if (e) keycode=e.which
  else keycode=0
  return keycode
}

function keydown(e) {
	var keycode=getkeycode(e)
		
	if (keycode==38 && !key_up) {
		key_up=true
		key_order.splice(0,0,1)
		key_order.splice(3,1)
	}
	else if (keycode==39 && !key_right) {
		key_right=true
		key_order.splice(0,0,2)
		key_order.splice(3,1)
	}
	else if (keycode==40 && !key_down) {
		key_down=true
		key_order.splice(0,0,3)
		key_order.splice(3,1)
	}
	else if (keycode==37 && !key_left) {
		key_left=true
		key_order.splice(0,0,4)
		key_order.splice(3,1)
	}
	else if (keycode==16) key_shift=true
	else if (keycode==17) key_control=true
	
	if (keycode==38 || keycode==39 || keycode==40 || keycode==37) find_direction()
	if (keycode==38 || keycode==39 || keycode==40 || keycode==37 || keycode==16) get_col_length()
}

function keyup(e) {
	var keycode=getkeycode(e)
	
	if (keycode==38 && key_up) {
		key_up=false
		for (var i=0;i<4;i++) {
			if (key_order[i]==1) key_order.splice(i,1)
		}
		key_order.splice(3,0,0)
	}
	else if (keycode==39 && key_right) {
		key_right=false
		for (var i=0;i<4;i++) {
			if (key_order[i]==2) key_order.splice(i,1)
		}
		key_order.splice(3,0,0)
	}
	else if (keycode==40 && key_down) {
		key_down=false
		for (var i=0;i<4;i++) {
			if (key_order[i]==3) key_order.splice(i,1)
		}
		key_order.splice(3,0,0)
	}
	else if (keycode==37 && key_left) {
		key_left=false
		for (var i=0;i<4;i++) {
			if (key_order[i]==4) key_order.splice(i,1)
		}
		key_order.splice(3,0,0)
	}
	else if (keycode==16) key_shift=false
	else if (keycode==17) key_control=false
	
	if (key_order[0]>0 && (keycode==38 || keycode==39 || keycode==40 || keycode==37)) find_direction()	
	if (keycode==38 || keycode==39 || keycode==40 || keycode==37 || keycode==16) get_col_length()
}
