var KeyboardController = new Class({
	Extends: BaseController, 
	
	keyCodes:    {left: 37, up: 38, right: 39, down: 40, 'space': 32, 'escape': 27, 'a':65, 'w':87, 'd':68, 's':83, 'r':82, 'f':70, 'k':75, 'enter':13, 'backspace':8},
	commandKeys: {'37': 'dx', '38': 'dy', '39': 'dx', '40': 'dy', '70': 'f'},

	initialize: function(url, options){
		this.url = url;
		this.parent(options);
		
		this.isFireEnabled = true;
		this.isStarted = false;
		this.isMoveEnabled = true;
		//used to allow typing in textboxes
		this.isEnabled = true;
		
		this.keyDownHandler = jQuery.proxy( this.onKeyDown, this );
        this.keyUpHandler = jQuery.proxy( this.onKeyUp, this );
        this.tickHandler = jQuery.proxy( this.onTick, this );
        
        this.pressedKeys = [];
        this.resetPressedKeys();
	},
	
	resetPressedKeys: function() {
        this.pressedKeys[this.keyCodes.left] = false;
        this.pressedKeys[this.keyCodes.right] = false;
        this.pressedKeys[this.keyCodes.up] = false;
        this.pressedKeys[this.keyCodes.down] = false;
        this.pressedKeys[this.keyCodes.f] = false;
	},

	onKeyDown: function(eventObj) {
		// IE doesn't pass event on keydown
        var eventObj = eventObj || window.event;
        
        if (this.isStarted && this.isEnabled 
            && (this.isMoveEnabled || (eventObj.keyCode == this.keyCodes.f && !this.isMoveEnabled)) ) {
            var keyCode = ''+eventObj.keyCode;
            if (this.pressedKeys[keyCode] === false) {
                this.pressedKeys[keyCode] = true;
            }

    		// to prevent crash in IE
            if(this.pressedKeys[keyCode] && eventObj.preventDefault) {
                eventObj.preventDefault();
                eventObj.stopPropagation();
                this.sendCommands();
                return false;
            }
        }
        return true;
    },
	
	onKeyUp: function(eventObj) {
		// IE doesn't pass event on keydown
        var eventObj = eventObj || window.event;
        
        if (this.isStarted && this.isEnabled 
            && (this.isMoveEnabled || (eventObj.keyCode == this.keyCodes.f && !this.isMoveEnabled))) {
	        var keyCode = ''+eventObj.keyCode;
            if (this.pressedKeys[keyCode]) {
                this.pressedKeys[keyCode] = false;
                
                // get command by key code
                var command = this.commandKeys[keyCode];
                if (this.stack[command]) {
                    delete this.stack[command];
                }
                
                if(eventObj.preventDefault) {
                    eventObj.preventDefault();
                    eventObj.stopPropagation();
                }
                return false;
            }
        }
        return true;
	},
	
	sendCommands: function() {
	    for(var pressedKey in this.pressedKeys) {
	        var value = this.pressedKeys[pressedKey];
	        if (typeof(value) == "boolean" && value === true) {
    	        pressedKey = parseInt(pressedKey);
    	        switch(pressedKey) {
    		        case this.keyCodes.left:
    		        	this.addGunCommand(this.commands.moveLeft, 1);
    		            break;
    		        case this.keyCodes.up:
    		            this.addGunCommand(this.commands.moveUp, 1);
    		            break;
    		        case this.keyCodes.right:
    		            this.addGunCommand(this.commands.moveRight, -1);
    		            break;
    		        case this.keyCodes.down:
    		            this.addGunCommand(this.commands.moveDown, -1);
    		            break;
    		        case this.keyCodes.f:
    		        	if (this.isFireEnabled) {
    		            	this.addGunCommand(this.commands.fire, 1);
    		            }
    		            break;
    	        }
            }
	    }
	    
		if (this.haveCommands) {
			this.sendRequest('gun');
		}  
	},
	
	start: function() {
		if (!this.isStarted) {
			this.isStarted = true;
            //this.timer = window.setInterval(this.tickHandler, 200);
            
            $(document).keydown(this.keyDownHandler);
            $(document).keyup(this.keyUpHandler);
		}
		
		this.isMoveEnabled = true;
	},
	
	stop: function() {
	    if (this.isStarted) {
    		this.isStarted = false;
            $(document).unbind('keydown', this.keyDownHandler);
            $(document).unbind('keyup', this.keyDownHandler);
        
            //window.clearInterval(this.timer);
            this.clearGunCommands();
            this.resetPressedKeys();
        }
	},
	
	onTick: function() {
	    // moved to gun controller
	    this.sendCommands();
	},
	
	enable: function(isEnabled) {
	    this.isEnabled = isEnabled;
	},
	
	enableReset: function(isEnabled) {
		this.isResetEnabled = isEnabled;
	},
	
	enableFire: function(isEnabled) {
		this.isFireEnabled = isEnabled;
		if (!this.isStarted) {
			this.isMoveEnabled = false;
			this.isResetEnabled = false;
			//this.start();
		} else {
			//this.stop();
			this.isMoveEnabled = true;
			this.isResetEnabled = true;
		}
	}/*,
	
	addMoveCommand: function(command, value) {
		if (this.isMoveEnabled) {
			this.addGunCommand(command, value);
		}
	},

    catchStopEvent: function(evt) {
		// IE doesn't pass event on keyup
        var event = evt || window.event;

        this.clearGunCommands();
    },

	catchEvent: function(evt) {
		// IE doesn't pass event on keydown
        var event = evt || window.event;
        
        if (!this.isStarted || !this.isEnabled) return true;
        
		this.command = null;
		var action = 'move';

		switch (event.keyCode) {
		    case this.keyCodes.left:
		    case this.keyCodes.a:
		    	this.addMoveCommand(this.commands.moveLeft, 1);
		        break;
		    case this.keyCodes.up:
		    case this.keyCodes.w:
		        this.addMoveCommand(this.commands.moveUp, 1);
		        break;
		    case this.keyCodes.right:
		    case this.keyCodes.d:
		        this.addMoveCommand(this.commands.moveRight, -1);
		        break;
		    case this.keyCodes.down:
		    case this.keyCodes.s:
		        this.addMoveCommand(this.commands.moveDown, -1);
		        break;
		    //case this.keyCodes.fire:
		    //case this.keyCodes.enter:
		    case this.keyCodes.f:
            //case this.keyCodes.k:
		    	if (this.isFireEnabled) {
		        	this.addGunCommand(this.commands.fire, 1);
		        	void(0);
		        }
		        action = 'shot';
		        break;
		    //case this.keyCodes.reset:
		    case this.keyCodes.r:
		    //case this.keyCodes.backspace:
		    	if (this.isResetEnabled) {
		        	this.addGunCommand(this.commands.reset, 1);
		        }
		        action = 'reset';
		        break;
		}
		
		if (this.haveCommands) {
			// to prevent crash in IE
            if(typeof event.preventDefault == 'function') {
                event.preventDefault();
            }
			// to prevent crash in IE
            if(typeof event.stopPropagation == 'function') {
                event.stopPropagation();
            }
			this.sendRequest(action);

			return false;
		}
		
		return true;
	}
*/
});
