var gamepads = {};
var haveEvents = 'GamepadEvent' in window;
var haveWebkitEvents = 'WebKitGamepadEvent' in window;
var socket = require('socket.io-client')('http://196.168.15.149:3000');

//handles connection and disconnection of gamepads
function gamepadHandler(event, connecting) {
  var gamepad = event.gamepad;
  // Note:
  // gamepad === navigator.getGamepads()[gamepad.index]

  if (connecting) {
    gamepads[gamepad.index] = gamepad;
    console.log("Gamepad connected at index %d: %s. %d buttons, %d axes.",
      e.gamepad.index, e.gamepad.id,
      e.gamepad.buttons.length, e.gamepad.axes.length);
    socket.emit("gamepad", connected)
    commandLoop();
  } else {
    delete gamepads[gamepad.index];
    console.log("Gamepad disconnected from index %d: %s",
      e.gamepad.index, e.gamepad.id);
    socket.emit("gamepad",disconnected)
  }
}

//function keeps getting values from first gamepad checking axis 0 and 1 for x y values respectively.
//maps values to left right motors and again maps that to axis 2, throttle.
function commandLoop () {
  var gamepadconnected = true;
  while (gamepadconnected)
  gamepadconnected = false;
  for (gamepad in gamepads) {
    if (gamepadconnected) {
    }
    else if (gamepad) {
      if (gamepad.connected) {
        gamepadconnected = true;
        var axis = gamepad.axes;
        var data = {valueL: 0, valueR: 0}
        //when switched to mode Orange with button 24, tank drive active
        // axis 2 (throttle) is left and joystick is right.
        if (gamepad.button[24]) {
          var right = -axis[1];
          var left = axis[2];
          data.valueL = left;
          data.valueR = right;
        }
        else {
          var drivePower = -axis[1];
          var turnPower = axis[0];
          var throttle = axis[2];
          data.valueL = (drivePower + turnPower)/2*throttle;
          data.valueR = (drivePower - turnPower)/2*throttle;
        }
        socket.emit("serialOut", data);
      }
    }
  }
}

//scans for gamepads in chrome based browsers
function scangamepads() {
  var gamepad = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
  for (var i = 0; i < gamepad.length; i++) {
    if (gamepad[i]) {
      if (!(gamepad[i].index in gamepads)) {
        gamepadHandler(gamepad[i], true);
      } else {
        gamepads[gamepad[i].index] = gamepad[i];
      }
      clearInterval(interval);      //stop polling for new gamepads once one is found. this may be problamatic right now considering above code structure is for multiple pads.
    }
  }
}

//determine which event listener is best
if (haveEvents) {
  window.addEventListener("gamepadconnected", function(e) { gamepadHandler(e, true); }, false);
  window.addEventListener("gamepaddisconnected", function(e) { gamepadHandler(e, false); }, false);
} else if (haveWebkitEvents) {
  window.addEventListener("webkitgamepadconnected", function(e) { gamepadHandler(e, true); }, false);
  window.addEventListener("webkitgamepaddisconnected", function(e) { gamepadHandler(e, false); }, false);
} else {
  setInterval(scangamepads, 500);
}

@@ -182,16 +182,38 @@
 3000');%0A
+var command;%0Avar scan;
 %0A//handl
@@ -669,16 +669,28 @@
 ed)%0A    
+setInterval(
 commandL
@@ -692,17 +692,20 @@
 mandLoop
-(
+, 20
 );%0A  %7D e
@@ -1121,70 +1121,16 @@
 ed =
- true;%0A  while (gamepadconnected)%0A  gamepadconnected =
  false;%0A
   fo
@@ -1125,16 +1125,18 @@
  false;%0A
+  
   for (g
@@ -1153,32 +1153,34 @@
 gamepads) %7B%0A    
+  
 if (gamepadconne
@@ -1187,26 +1187,30 @@
 cted) %7B%0A    
+ 
+ 
 %7D%0A
+  
     else if 
@@ -1217,24 +1217,26 @@
 (gamepad) %7B%0A
+  
       if (ga
@@ -1258,24 +1258,26 @@
 ) %7B%0A        
+  
 gamepadconne
@@ -1289,16 +1289,18 @@
 = true;%0A
+  
         
@@ -1324,16 +1324,18 @@
 d.axes;%0A
+  
         
@@ -1376,16 +1376,18 @@
         
+  
 //when s
@@ -1451,16 +1451,18 @@
         
+  
 // axis 
@@ -1505,32 +1505,34 @@
  right.%0A        
+ 
+ 
 if (gamepad.butt
@@ -1547,24 +1547,26 @@
 %7B%0A          
+  
 var right = 
@@ -1567,32 +1567,34 @@
 ght = -axis%5B1%5D;%0A
+  
           var le
@@ -1609,32 +1609,34 @@
 s%5B2%5D;%0A          
+  
 data.valueL = le
@@ -1639,16 +1639,18 @@
 = left;%0A
+  
         
@@ -1684,25 +1684,19 @@
     
-%7D%0A     
   
+%7D
  else %7B%0A
     
@@ -1683,32 +1683,34 @@
        %7D else %7B%0A
+  
           var dr
@@ -1722,32 +1722,34 @@
 wer = -axis%5B1%5D;%0A
+  
           var tu
@@ -1773,24 +1773,26 @@
 ;%0A          
+  
 var throttle
@@ -1795,32 +1795,34 @@
 ttle = axis%5B2%5D;%0A
+  
           data.v
@@ -1868,32 +1868,34 @@
 ttle;%0A          
+ 
+ 
 data.valueR = (d
@@ -1933,26 +1933,30 @@
 le;%0A        
-%7D%0A
+  %7D%0A  
         sock
@@ -1993,15 +1993,78 @@
     
-%7D%0A    %7D
+  %7D%0A      %7D%0A    %7D%0A  if(!gamepadconnected) %7B%0A    clearInterval(command)
 %0A  %7D
@@ -2523,16 +2523,12 @@
 val(
-interval
+scan
 );  
@@ -3188,16 +3188,23 @@
 else %7B%0A 
+ scan =
  setInte

set polling on interval of 20 ms