var UI = require('ui');
var ajax = require('ajax');

var activeThing;
var activeTimeout;
var thingList;

var main = new UI.Card({
  title: 'Home Pebble',
  subtitle: 'Control your home from your wrist!'
});

var activeCard = new UI.Card();
activeCard.on('click', function(e) {
  activeCard.body('Status: ...');
  takeAction(activeThing);
});


main.show();

// Retrieve things to control
ajax(
  {url: 'http://10.0.0.16:3000/things', type: 'json', cache: false},
  function(data){
    thingList = new UI.Menu({
      sections: [{
        items: data.things.map(function(thing) {
          return {title: thing.name, subtitle: thing.action};
        })
      }]
    });
    thingList.on('select', function(e) {
      activeThing = {
        index: e.itemIndex,
        title: e.item.title,
        subtitle: e.item.subtitle
      };
      takeAction(activeThing);
    });

    // Now that thingList is ready, add main click handler
    main.on('click', function(e) {
      thingList.show();
    });

  }, function(error) {
    console.log('error: ', error);
  }
);

function takeAction(activeThing) {
  clearTimeout(activeTimeout);

  ajax(
    {url: 'http://10.0.0.16:3000/action/'+activeThing.index, type: 'json', cache: false },
    function(data){
      activeCard.title(activeThing.title);
      activeCard.subtitle(activeThing.subtitle);
      activeCard.body('Status: ' + data.status);
      activeCard.show();

      // close active thing window
      activeTimeout = setTimeout(function() {
        activeCard.hide();
      }, 3000);

    }, function(error) {
      console.log('error: ', error);
      activeCard.body('Communication Error');
    }
  );  
}

@@ -63,16 +63,32 @@
 eThing;%0A
+var thingList;%0A%0A
 var acti
@@ -98,32 +98,34 @@
 imeout;%0Avar 
-thingLis
+mainTimeou
 t;%0A%0Avar main
@@ -354,17 +354,16 @@
 );%0A%7D);%0A%0A
-%0A
 main.sho
@@ -1140,16 +1140,45 @@
 imeout);
+%0A  clearTimeout(mainTimeout);
 %0A%0A  ajax
@@ -1574,17 +1574,164 @@
      %7D, 
-3
+5000);%0A%0A      // close whole app if no action%0A      mainTimeout = setTimeout(function() %7B%0A        thingList.hide();%0A        main.hide();%0A      %7D, 15
 000);%0A%0A 

Add timeout for whole application