var App = {
  
GPS : {
  settings : null,
  position : null,
  locationFound : function() {
    return this.position !== null;
  },
  checkLocation : function(settings) {
    var that = this;
    that.settings = settings || {};
    that.position = null;
    var options = {};
    options.timeout = settings.timeout || 20000; // default to 20 seconds for trying to obtain the location.
    navigator.geolocation.getCurrentPosition(
      function (position) {
    	that.position = position;
        if (typeof(that.settings.success) === "function") {
          that.settings.success(position);
        }
      },
      function (positionError) {
        if (typeof(that.settings.error) === "function") {
          that.settings.error(positionError);
        }
      },
      options);
  }
},
  XHR : {
  defaultOptions : {
	method : "GET",
	url: "",
	success : function() {},
	error: function() {}
  },
  stateChange : function(xhr, options) {
	return function() {
      if (!xhr || xhr.readyState !== 4) {
        return;
      }
      
      if (xhr.status === 200) {
        if (typeof(options.success) === "function") {
          options.success(xhr);
        }
      } else {
        if (typeof(options.success) === "function") {
          options.error(xhr);
        }
      }
	};
  },
  send : function(options) {
	function createXmlHttpRequest() {
	  return new XMLHttpRequest();
	}
	options = options || {};
    
	try {
	  var xhr = createXmlHttpRequest();
      
      for (var i in this.defaultOptions) {
        options[i] = options[i] || this.defaultOptions[i];
      }
    
      xhr.open(options.method, options.url);
      xhr.onreadystatechange = this.stateChange(xhr, options);
    
      xhr.send();
	} catch (e) {
	  console.log(e);
	}
  }
},
Tools : {
  capitals : function(str) {
    var words = str.split(/\s/),
        result = [];
    for (var i = 0; i < words.length; ++i) {
      var word = words[i];
      result.push(word.substr(0,1).toUpperCase() + word.substr(1).toLowerCase());
    }
    return result.join(' ');
  },
  
  isOpen: function(item) {
    var dayNames = [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ],
        d = new Date(),
        today = dayNames[ d.getDay() ];

    var opening = item[today + "_open"].split(":"); //splits on hours, minutes, seconds
    var openingOffset = ( d.getHours() < 4 ? -1 : 0 ); //if it is < 4am, assume we're talking about the previous day
    var openingTime = new Date( d.getFullYear(), d.getMonth(), 
                           d.getDate() + openingOffset, opening[0], opening[1], 0, 0); //exact opening time
		
		
    var closing = item[today + "_close"].split(":");
    var closingTime = new Date( d.getFullYear(), d.getMonth(), 
                           d.getDate(), closing[0], closing[1], 0, 0); //exact closing time
    
    if (closingTime <= openingTime) {
      if (d.getHours() < 5) {
        openingTime.setDate( openingTime.getDate() - 1 );
      } else {
        closingTime.setDate( closingTime.getDate() + 1 );
      }
    }
		
    return (d > openingTime && d < closingTime);
  }
}};
