//= require jsuri

window.Spree = {};

Spree.ready = function(callback) {
  if (typeof Turbolinks !== 'undefined' && Turbolinks.supported) {
    jQuery(document).on('turbolinks:load', function() {
      callback(jQuery);
    });
  } else {
    jQuery(document).ready(callback);
  }
};

Spree.mountedAt = function() {
  return "<%= Rails.application.routes.url_helpers.spree_path(trailing_slash: true) %>";
};

Spree.pathFor = function(path) {
  var locationOrigin;
  locationOrigin = (window.location.protocol + "//" + window.location.hostname) + (window.location.port ? ":" + window.location.port : "");
  return locationOrigin + Spree.mountedAt() + path;
};

Spree.ajax = function(url, options) {
  if (typeof url === "object") {
    options = url;
    url = undefined;
  }
  options = options || {};
  options = $.extend(options, {
    headers: {
      'Authorization': 'Bearer ' + Spree.api_key
    }
  });
  return $.ajax(url, options);
};

Spree.getJSON = function(url, data, success) {
  if (typeof data === 'function') {
    success = data;
    data = undefined;
  }
  return Spree.ajax({
    dataType: "json",
    url: url,
    data: data,
    success: success
  });
};
