/*
   jscript include providing 
   keyboard inputs functionalities
   through automatically refreshed global vars
*/


// keyboard state array: 0 if key up, 1 if key down
var key=new Array(256);

/*
// total key action array: 
// +1 for each keypress and autorepeat
var keybuf=new Array(256);
*/

// latest key event
var key_last={code:0,way:0};

// various keycodes,roughly equals ascii (incomplete)
// TODO check key-mouse.htm to complete
var KEY_ESC=27;
var KEY_SPACE=32;
var KEY_LEFT=37;
var KEY_UP=38;
var KEY_RIGHT=39;
var KEY_DOWN=40;
var KEY_A=65;
var KEY_ENTER=13;

/* exemple usage
while (!key[KEY_ESC]){
  if (key[KEY_LEFT]) x--;
  if (key[KEY_RIGHT]) x++;
  ...
*/



/***************************************************************************
			     ARROW KEYS
Trouvé ici: http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=17

modifié pour le tilegame

***************************************************************************/

// init key array to 0 (all key up)
for (k=0;k<256;k++){
  key[k]=0;
  //keybuf[k]=0;
}

//FIRST, TELL THE BROWSERS TO REACT TO THE EVENT
if( document.captureEvents ) {
    //non IE
    if( Event.KEYUP ) {
        //NS 4, NS 6+, Mozilla 0.9+
        document.captureEvents( Event.KEYDOWN | Event.KEYUP | Event.KEYPRESS );
    }
}
/* this next line tells the browser to detect a keyup
event over the whole document and when it detects it,
it should run the event handler function 'key_down_handler' 
and 'key_up_handler' below */

document.onkeyup = key_up_handler;
document.onkeydown = key_down_handler;
document.onkeypress = key_press_handler;

function key_up_handler(e) {
   key_change_handler(e,0);
   if (typeof(key_up_raw)=="function")
     key_up_raw(e);
}

function key_down_handler(e) {
   key_change_handler(e,1);
   if (typeof(key_down_raw)=="function")
     key_down_raw(e);   
}

function key_press_handler(e) {
  if (typeof(key_press_raw)=="function")
     key_press_raw(e);
}


function key_change_handler(e,way) {
    if( !e ) {
        //if the browser did not pass the event information to the
        //function, we will have to obtain it from the event register
        if( window.event ) {
            //DOM
            e = window.event;
        } else {
            //TOTAL FAILURE, WE HAVE NO WAY OF REFERENCING THE EVENT
            return;
        }
    }
    if( typeof( e.which ) == 'number' ) {
        //NS 4, NS 6+, Mozilla 0.9+, Opera
        e = e.which;
    } else if( typeof( e.keyCode ) == 'number'  ) {
        //IE, NS 6+, Mozilla 0.9+
        e = e.keyCode;
    } else if( typeof( e.charCode ) == 'number'  ) {
        //also NS 6+, Mozilla 0.9+
        e = e.charCode;
    } else {
        //TOTAL FAILURE, WE HAVE NO WAY OF OBTAINING THE KEY CODE
        return;
    }

  // Skip system autorepeat that generates multiple keydown events:
  // if the key was allready down then abort 

  if (key[e]==way)
    return;

  // UPDATE TO NEW KEY STATE

  key[e]= way;
  key_last.code=e;
  key_last.way=way;

  key_event_manager(e,way);

}


