/*
  copyright (c) 2003 Jan-Klaas Kollhof
 
  This is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
 
  This software is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with this software; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
*/

svgui.inputManager = function(){
    const KeyTab=9;
    const KeyDelete=127;
    const KeyBackSpace=8;
    const KeyUp = 38;
    const KeyDown = 40;
    const KeyRight = 39;
    const KeyLeft = 37;
    
    
    var NavigateEvent=function(direction){
        this.type="navigate";
        this.direction=direction;
    }
        
    var TextInput=function(data){
        this.type="textInput";
        this.data=data;
    }
    
    var CycleChildEvent=function(){
        this.type  = "cycleChild";
    
    }
    
    var DeleteLeftEvent=function(){
        this.type="deleteL";
    }
    var DeleteRightEvent=function(){
        this.type="deleteR";
    }
    
    var InputManager=function(){
        this.navigateN=0;
        this.navigateNE=45;
        this.navigateE=90;
        this.navigateSE=135;
        this.navigateS=180;
        this.navigateSW=225;
        this.navigateW=270;
        this.navigateNW=325;
             
        
        this.listeners = new Array();
        document.documentElement.addEventListener("keypress",this, false);
        document.documentElement.addEventListener("keydown",this, false);
        document.documentElement.addEventListener("keyup",this, false);
        document.documentElement.addEventListener('SVGScroll', this, false);
    }
    
    InputManager.prototype.handleEvent=function(evt){
        if(this[evt.type]){
            this[evt.type](evt);
        }
        //dispatch all events to all listners
        this.dispatchEvent(evt);
        //debug.debugMsg(evt.type);
        //debug.printObj(evt);
    }
    
    InputManager.prototype.SVGScroll =  function(evt){
        var currTrans =  document.documentElement.currentTranslate;
        if(currTrans.x<0){
            this.dispatchEvent(new NavigateEvent(this.navigateE));
        }else if(currTrans.x>0){
            this.dispatchEvent(new NavigateEvent(this.navigateW));
        }
        if(currTrans.y<0){
            this.dispatchEvent(new NavigateEvent(this.navigateS));
        }else if(currTrans.y>0){
            this.dispatchEvent(new NavigateEvent(this.navigateN));
        }
        currTrans.x=0;
        currTrans.y=0;
    }
    //----------------------------------------keyboard------------------------------------------
    InputManager.prototype.keypress =  function(evt){
        switch(evt.charCode){
            case KeyBackSpace: 
                //this is needed for backspace to work
                break;
            default:
                this.dispatchEvent(new TextInput(String.fromCharCode(evt.charCode)));
        }
    }
    
    InputManager.prototype.keydown =  function(evt){
        switch(evt.keyCode){
            case KeyBackSpace:
                this.dispatchEvent(new DeleteLeftEvent());
                break;
            case KeyDelete:
                this.dispatchEvent(new DeleteRightEvent());
                break;
            default:
                break;
        }
    }
    
    InputManager.prototype.keyup =  function(evt){
        switch(evt.keyCode){
            case KeyUp: 
                this.dispatchEvent(new NavigateEvent(this.navigateN));
                break;
            case KeyDown:
                this.dispatchEvent(new NavigateEvent(this.navigateS));
                break;
            case KeyRight:
                this.dispatchEvent(new NavigateEvent(this.navigateE));
                break;
            case KeyLeft:
                this.dispatchEvent(new NavigateEvent(this.navigateW));
                break;
            case KeyTab:
                this.dispatchEvent(new CycleChildEvent());
                break;
            default:
                
        }
    }
    
    //----------------------------------Event Listeners----------------------------------------------------------------
        
    InputManager.prototype.registerListener = function(listener){
        this.listeners.push(listener);
    }
    
    InputManager.prototype.unregisterListener = function(listener){
        for(var i =0; i < this.listeners.length; i++){
            if (this.listeners[i]==listener){
                this.listeners.splice(i,1);
                i=this.listeners.length;
            }
        }
    }
    
    InputManager.prototype.dispatchEvent = function(evt){
        for(var i=0; i < this.listeners.length; i++){
            svgui.dispatchEvent(this.listeners[i],evt);
        }
    }
    
    return new InputManager();
}();