define(["./../common/inputmanager.js"],function(_inputmanager){var repeatingKey,isLooping,ButtonInfos={0:{key:"GamepadA",keyCode:0,enableRepeatKeyDown:!1,clickOnKeyUp:!0},1:{key:"GamepadB",keyCode:27},12:{key:"GamepadDPadUp",keyCode:38},13:{key:"GamepadDPadDown",keyCode:40},14:{key:"GamepadDPadLeft",keyCode:37},15:{key:"GamepadDPadRight",keyCode:39},GamepadLeftThumbStickUp:{key:"GamepadLeftThumbStickUp",keyCode:38},GamepadLeftThumbStickDown:{key:"GamepadLeftThumbStickDown",keyCode:40},GamepadLeftThumbStickLeft:{key:"GamepadLeftThumbStickLeft",keyCode:37},GamepadLeftThumbStickRight:{key:"GamepadLeftThumbStickRight",keyCode:39}},lastRepeatingKeyTime=0;function setPressed(gamepad,buttonInfo,newPressedState){var fire,key=buttonInfo.key,keyCode=buttonInfo.keyCode;!0===newPressedState?(fire=!1!==buttonInfo.pressed&&(!1===buttonInfo.enableRepeatKeyDown||function(key,repeat,timestamp){if(repeat&&key===repeatingKey){if(timestamp-lastRepeatingKeyTime<100)return 1}else repeatingKey=key;lastRepeatingKeyTime=timestamp}(key,!0,Date.now()))?fire:!0)&&keyCode&&raiseEvent("keydown",key,keyCode,!0===buttonInfo.pressed):!1===newPressedState&&!0===buttonInfo.pressed&&(keyCode&&raiseEvent("keyup",key,keyCode),buttonInfo.clickOnKeyUp)&&allowInput()&&_inputmanager.default.trigger("select"),buttonInfo.pressed=newPressedState}function allowInput(){var doc=document;return"hidden"!==doc.visibilityState&&!!doc.hasFocus()}function raiseEvent(name,key,keyCode,repeat){var event;allowInput()&&((event=document.createEvent("Event")).initEvent(name,!0,!0),event.key=key,event.keyCode=keyCode,null!=repeat&&(event.repeat=repeat),(document.activeElement||document.body).dispatchEvent(event))}function setButtonsPressed(gamepad,buttons,pressed){for(var i=0,length=buttons.length;i<length;i++)setPressed(0,buttons[i],pressed)}function runInputLoop(){for(var hasGamepad=!1,gamepads=function(){try{return navigator.getGamepads()||[]}catch(err){return console.log("Error getting gamepads: "+err),[]}}(),i=0,length=gamepads.length;i<length;i++){var gamepad=gamepads[i];if(gamepad)if(hasGamepad=!0,gamepad._lastTimestamp===gamepad.timestamp){var btns=gamepad._buttonsPressed;btns&&setButtonsPressed(0,btns,!0)}else{gamepad._lastTimestamp=gamepad.timestamp;for(var btns=gamepad.axes,leftStickX=btns[0],leftStickY=btns[1],pressedButtons=[],unpressedButtons=[],buttons=(.75<leftStickX?pressedButtons.push(ButtonInfos.GamepadLeftThumbStickRight):leftStickX<-.75?pressedButtons.push(ButtonInfos.GamepadLeftThumbStickLeft):leftStickY<-.75?pressedButtons.push(ButtonInfos.GamepadLeftThumbStickUp):(.75<leftStickY?pressedButtons:(unpressedButtons.push(ButtonInfos.GamepadLeftThumbStickLeft),unpressedButtons.push(ButtonInfos.GamepadLeftThumbStickRight),unpressedButtons.push(ButtonInfos.GamepadLeftThumbStickUp),unpressedButtons)).push(ButtonInfos.GamepadLeftThumbStickDown),gamepad.buttons),j=0,len=buttons.length;j<len;j++)if(buttons[j].pressed)switch(j){case 12:case 13:case 14:case 15:case 0:case 1:pressedButtons.push(ButtonInfos[j])}else switch(j){case 12:case 13:case 14:case 15:case 0:case 1:unpressedButtons.push(ButtonInfos[j])}setButtonsPressed(0,pressedButtons,!0),setButtonsPressed(0,unpressedButtons,!1),gamepad._buttonsPressed=pressedButtons}}hasGamepad?requestAnimationFrame(runInputLoop):(console.log("exiting gamepad input loop"),isLooping=!1)}navigator.getGamepads||(navigator.webkitGetGamepads?navigator.getGamepads=navigator.webkitGetGamepads:navigator.getGamepads=function(){return null}),window.addEventListener("gamepadconnected",function(e){var e=e.gamepad;e&&(e=e.index,console.log("gamepadconnected: "+e),isLooping||(isLooping=!0,runInputLoop()))}),window.addEventListener("gamepaddisconnected",function(e){var e=e.gamepad;e&&(e=e.index,console.log("gamepaddisconnected: "+e))}),window.navigator&&"string"==typeof window.navigator.gamepadInputEmulation&&(window.navigator.gamepadInputEmulation="gamepad")});