(function() {
  var FeatureError, ROOT, VERSION, geolocation;

  VERSION = '1';

  ROOT = typeof root !== "undefined" && root !== null ? root : window;

  window.visited = false;

  FeatureError = function(message) {
    this.name = "FeatureError";
    return this.message = "Your browser does not support the requirements for this application";
  };

  FeatureError.prototype = Error.prototype;

  window.ENV = {
    fullscreen: navigator.standalone,
    online: navigator.onLine,
    IE: /MSIE (\d+\.\d+);/.test(navigator.userAgent) && !document.getElementsByClassName
  };

  window.AppUtils = {
    /* 
     @params {id} The pub ID as per data {attr} The required attribute such as name, lat etc...
     @returns {String} The attribute requested
    */
    getPubAttr: function(id, attr) {
      var found, pub, _i, _len;
      found = null;
      for (_i = 0, _len = PUBS.length; _i < _len; _i++) {
        pub = PUBS[_i];
        if (pub['id'] === id) {
          found = pub[attr];
          break;
        }
      }
      return found;
    },
    isAndroid: function() {
      return navigator.userAgent.toLowerCase().indexOf("android") > -1;
    },
    isiOS: function() {
      return navigator.userAgent.indexOf('like Mac OS X') > -1;
    },
    isRetina: function() {
      return window.devicePixelRatio >= 2;
    },
    isBlackberry: function() {
      return navigator.userAgent.toLowerCase().indexOf("blackberry") > -1;
    },
    isDesktop: function() {
      var desktop;
      desktop = true;
      if (this.isAndroid() || this.isiOS() || this.isBlackberry()) desktop = false;
      return desktop;
    }
  };

  window.Fullers = {
    Utils: AppUtils,
    Models: {},
    Collections: {},
    Views: {},
    Routers: {},
    Modules: {},
    init: function() {
      var _this = this;
      new Fullers.Routers.App();
      Backbone.history.start();
      window.location = "#/index";
      $('.container').addClass('visible');
      $('#site_loading').addClass('hidden');
      setTimeout(function() {
        return $('#site_loading').addClass('gone');
      }, 1000);
      Fullers.bindEvents();
      Fullers.buildCollections();
      if (!window.ENV.online) return alert("No internet connection");
    },
    showLoad: function() {
      var height, loader, width;
      loader = document.getElementById('loading');
      width = $(".container").width();
      height = window.innerHeight;
      loader.style.left = (width / 2) - 32 + "px";
      return loader.style.display = "block";
    },
    hideLoad: function() {
      var loader;
      loader = document.getElementById('loading');
      return loader.style.display = "none";
    },
    closeHome: function() {
      var _this = this;
      Fullers.addToHomeScreen.setAttribute("class", Fullers.addToHomeScreen.getAttribute('class') + " closed");
      setTimeout(function() {
        return Fullers.addToHomeScreen.style.display = "none";
      }, 1000);
      return window.visited = true;
    },
    setCenter: function() {
      var container, div;
      container = $(".container").width() / 2;
      div = $("#addToHomeScreen").width() / 2;
      return container - div + "px";
    },
    addToHome: function() {
      var _this = this;
      Fullers.addToHomeScreen = document.querySelector('#addToHomeScreen');
      if (!window.ENV.fullscreen && !/MSIE (\d+\.\d+);/.test(navigator.userAgent) && !window.visited) {
        Fullers.addToHomeScreen.style.left = this.setCenter();
        $('#addToHomeScreen .close').click(function() {
          return _this.closeHome();
        });
        setTimeout(function() {
          return Fullers.addToHomeScreen.setAttribute("class", Fullers.addToHomeScreen.getAttribute('class') + " opened");
        }, 1000);
        return setTimeout(function() {
          return _this.closeHome();
        }, 5000);
      } else {
        return this.closeHome();
      }
    },
    hideAll: function() {
      var item, sections, _i, _len, _results;
      sections = ["#home", "#attractions", "#pubs", "#map", "#map_list", "#pub_detail", "#logo"];
      _results = [];
      for (_i = 0, _len = sections.length; _i < _len; _i++) {
        item = sections[_i];
        _results.push($(item).hide());
      }
      return _results;
    },
    renderBaseURL: function() {
      return window.location = "index.html";
    },
    back: function() {
      window.history.back();
      return false;
    },
    postcodeSearch: function() {
      Fullers.Modules.PostcodeSearch.render(document.getElementById('p_code').value);
      return false;
    },
    bindEvents: function() {
      $(".backButton").click(function(e) {
        return Fullers.back();
      });
      return $("#postcode_search").submit(function(val) {
        $('#p_code_go').focus();
        return Fullers.postcodeSearch();
      });
    },
    buildCollections: function() {
      return Fullers.Collections.Pubs = new Backbone.Collection(window.PUBS);
    },
    showPostcodeModal: function(callback) {
      var $modal;
      var _this = this;
      $modal = $("#postcodeModal");
      $modal.removeClass("hidden");
      return $("#modal_postcode").submit(function(e) {
        Fullers.postcode = $("#modal_p_code").val();
        if (_this.validatePostcode(Fullers.postcode)) {
          $modal.addClass("hidden");
          postcode.usePointFromPostcode(Fullers.postcode, function(p) {
            Fullers.clientLocation = {
              lat: p.lat(),
              lng: p.lng()
            };
            return callback.call(this, Fullers.clientLocation.lat, Fullers.clientLocation.lng);
          });
        } else {
          $("#validation").removeClass("hidden");
        }
        return false;
      });
    },
    validatePostcode: function(postcode) {
      var postcodeRegEx;
      postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
      return postcodeRegEx.test(postcode);
    }
  };

  /*
  JSON Parser
  */

  Fullers.Modules.Parser = parser;

  /*
  Postcode Validator and Search
  */

  Fullers.Modules.PostcodeSearch = postcode;

  /*
   Geolocation Module
  */

  geolocation = {};

  Fullers.Modules.Geolocation = geolocation;

  /* 
   @Public
   @Description Returns the current user location. Takes a callback function
   @returns {Function} {lat, long}
  */

  /*
  Gets the current location and runs a callback function with the result
  */

  geolocation.getLocation = function(callback) {
    var _this = this;
    if (navigator.geolocation && !AppUtils.isDesktop()) {
      return navigator.geolocation.getCurrentPosition(function(position) {
        var lat, long, _ref;
        _ref = [position.coords.latitude, position.coords.longitude], lat = _ref[0], long = _ref[1];
        return callback.call(_this, lat, long);
      });
    } else if (Fullers.clientLocation != null) {
      return callback.call(this, Fullers.clientLocation.lat, Fullers.clientLocation.lng);
    } else {
      return Fullers.showPostcodeModal(callback);
    }
  };

  /*
   Sugar Method. Takes a Lambda and returns GMap Obj
   Just return the current location as a GMap Object
   @returns {Object} Google maps LagLng object
  */

  geolocation.current = function() {
    return geolocation.getLocation(function(lat, long) {
      return new google.maps.LatLng(lat, long);
    });
  };

  Fullers.Modules.Map = window.Map;

  /* 
   @Public
   @Description JSON File Parser
  */

  Fullers.Modules.FB = {};

  Fullers.Modules.FB.showStream = function() {
    return alert("run");
  };

  window.onload = function() {
    return Fullers.init();
  };

}).call(this);

