/*jslint bitwise: true, browser: true, eqeqeq: true, immed: true, newcap: true, regexp: true, white: true, nomen: false, onevar: false, undef: true, plusplus: false, white: true, indent: 2 */
/*global $ alert window document google geo_position_js */

var threshold = 0.5; // Threshold before we get a change event

var last_lat = null; // The last lat position before the poll

var have_shown_wait = false; // Have we shown the waiting message?
var current_havent_moved = 0;

function update_position(p) {
  $('#current').html(p.coords.latitude + ' ' + p.coords.longitude);
}

function initialize()  {
  if (geo_position_js.init()) {
    geo_position_js.getCurrentPosition(function (p) {
      $('.needs-position').addClass('have-position');
      update_position(p);
      last_lat = p.coords.latitude;
      $.ajax({
        url: 'http://ws.geonames.org/findNearbyPlaceNameJSON?lat=' + p.coords.latitude + '&lng=' + p.coords.longitude,
        dataType: 'json',
        success: function (data) {
          $('.locality').text(data.geonames[0].name);
          $('.region').text(data.geonames[0].adminName1);
        },
        error: function (xhr, status, error) {
          alert(error);
        }
      })

    },
    // Error-handler
    function () {
      alert("Couldn't get location");
    },
    // Options                                   
    { enableHighAccuracy: true});
  }
  else
  {
//    alert("Geo functionality not available");
  }
}

function onpagechange() {

  // Poll for our new location
  setInterval(function () {
    geo_position_js.getCurrentPosition(function (p) {
      update_position(p);
      if (last_lat === p.coords.latitude) {
        $('.havent-moved .choice:eq(' + current_havent_moved + ')').addClass('have-position');
        current_havent_moved ++;
        if (current_havent_moved === 4) {
          $('.needs-change').addClass('have-position');          
          setTimeout(function () {           $('#moved').addClass('have-position'); }, 500);
        }
      }
      else {
        $('#moved').addClass('have-position');
        $('.needs-change').addClass('have-position');
      }
      last_lat = p.coords.latitude;
    })
  },
  3000);

}
              
var bookData = {
  getComponents: function () {
    return [
      'page1.html',
      'page2.html'
    ];
  },
  getContents: function () {
    return [
      {
        title: "Prologue",
        src: "page1.html"
      },
      {
        title: "Chapter 1",
        src: "page2.html"
      }
    ]
  },
  getComponent: function (componentId) {
    return {
      'page1.html':
        '<p class="intro">She stood at the threshold of her home, wondering, “Would this work?”</p><p>There was only one way to find out...</p><h2 class="needs-position">Down and Out in <span class="locality">Your City</span> <span class="region">Your State</span></h2>',
      'page2.html':
        '<div class="havent-moved"><p class="choice">She sighed impatiently. <span class="choice">Checked her watch.</span><span class="choice"> In the distance a dog barked urgently.</span></p></div><p class="moved needs-change">She started off on her adventure. Where would it take her next?</p>'
    }[componentId];
  },
  getMetaData: function (key) {
    return {
      title: "A Geographic Adventure",
      creator: "Threpress Consulting Inc."
    }[key];
  }
}





