var UI = require('ui');
var Vibe = require('ui/vibe');
var ajax = require('ajax');
var host;

Pebble.addEventListener('showConfiguration', function(e) {
  Pebble.openURL('https://pebble-config.herokuapp.com/config?title=Pebble%20Presenter%20Config&fields=host');
});

Pebble.addEventListener('webviewclosed', function(e) {
  host = JSON.parse(decodeURIComponent(e.response)).host;
});

function navigate(by) {
  if (!host) {
    return;
  }
  
  ajax(
    { url: host, method: 'post', type: 'json', data: { by: by } },
    function success() {
      Vibe.vibrate('short');
    },
    function failure() {
      Vibe.vibrate('long');
    }
  );
}

var main = new UI.Card({
  title: 'Simple Presenter',
  body: 'Press up for forward, down for backward.'
});

main.on('click', 'up', function forward() {
  navigate(1);
});

main.on('click', 'down', function backward() {
  navigate(-1);
});

main.show();

var UI = require('ui');
var Vibe = require('ui/vibe');
var ajax = require('ajax');
var host;

Pebble.addEventListener('showConfiguration', function(e) {
  // TODO - This only works over HTTP. A much simpler config site can be made and hosted on github pages.
  Pebble.openURL('https://qubyte.github.io/pebble-presenter/');
});

Pebble.addEventListener('webviewclosed', function(e) {
  host = JSON.parse(decodeURIComponent(e.response)).host;
});

function navigate(by) {
  if (!host) {
    return;
  }
  
  ajax(
    { url: host, method: 'post', type: 'json', data: { by: by } },
    function success() {
      Vibe.vibrate('short');
    },
    function failure() {
      Vibe.vibrate('long');
    }
  );
}

var main = new UI.Card({
  title: 'Simple Presenter',
  body: 'Press up for forward, down for backward.'
});

main.on('click', 'up', function forward() {
  navigate(1);
});

main.on('click', 'down', function backward() {
  navigate(-1);
});

main.show();

Use custom config page hosted on gh-pages.
Use custom config page hosted on gh-pages.

The config page is hosted on the gh-pages branch of this repo. This allows it to be HTTPS, and much lighter than the predecessor.