(function() {
  var global, parseParams;
  global = typeof exports !== "undefined" && exports !== null ? exports : this;
  parseParams = function(params) {
    _.each(params, function(v, k) {
      var isRegex;
      if (_.isString(v)) {
        isRegex = v.match(/^\/(.*)\/([imxs]*)$/);
        if (isRegex) {
          return params[k] = new RegExp(isRegex[1], isRegex[2]);
        } else if (v === 'true') {
          return params[k] = true;
        } else if (v === 'false') {
          return params[k] = false;
        } else if (v === 'null') {
          return params[k] = null;
        } else if (v.match(/^[\{\[].*[\}\]]$/)) {
          try {
            return params[k] = JSON.parse(v);
          } catch (err) {
            log.error('c/api - JSON error parsing');
            return console.error(err);
          }
        }
      }
    });
    return params;
  };
  _.templateSettings = {
    interpolate: /\{\{(.+?)\}\}/g,
    evaluate: /<%([\s\S]+?)%>/g
  };
  global.API = {
    "delete": function(url, find, next) {
      var ajaxOpts;
      if (next == null) {
        next = function() {};
      }
      ajaxOpts = {
        url: url,
        type: 'DELETE'
      };
      if (typeof find === 'string') {
        ajaxOpts.url += '/' + find;
      }
      $.ajax(ajaxOpts).success(function(r) {
        next(null, r);
      }).error(function(r) {
        console.error('API failed to delete', r);
        next(r);
      });
    },
    get: function(url, params, next) {
      if (next == null) {
        next = function() {};
      }
      $.get(url, params).success(function(r) {
        r = parseParams(r);
        next(null, r);
      }).error(function(r) {
        console.error('API failed to get', r);
        next(r);
      });
    },
    put: function(url, find, params, next) {
      var ajaxOpts;
      if (next == null) {
        next = function() {};
      }
      ajaxOpts = {
        url: url,
        type: 'PUT',
        data: params
      };
      if (typeof find === 'string') {
        ajaxOpts.url += '/' + find;
      }
      $.ajax(ajaxOpts).success(function(r) {
        next(null, r);
      }).error(function(r) {
        console.error('API failed to update', r);
        next(r);
      });
    },
    post: function(url, params, next) {
      if (next == null) {
        next = function() {};
      }
      $.post(url, params).success(function(r) {
        next(null, r);
      }).error(function(r) {
        console.error('API failed to create', r);
        next(r);
      });
    }
  };
  global.LM = {
    ver: 1.0,
    api: {
      "delete": function(url, find, next) {
        var ajaxOpts;
        if (next == null) {
          next = function() {};
        }
        ajaxOpts = {
          url: url,
          type: 'DELETE'
        };
        if (typeof find === 'string') {
          ajaxOpts.url += '/' + find;
        }
        return $.ajax(ajaxOpts).success(function(r) {
          return next(null, r);
        }).error(function(r) {
          console.error('API failed to delete', r);
          return next(r);
        });
      },
      get: function(url, params, next) {
        if (next == null) {
          next = function() {};
        }
        return $.get(url, params).success(function(r) {
          r = parseParams(r);
          return next(null, r);
        }).error(function(r) {
          console.error('API failed to get', r);
          return next(r);
        });
      },
      put: function(url, find, params, next) {
        var ajaxOpts;
        if (next == null) {
          next = function() {};
        }
        ajaxOpts = {
          url: url,
          type: 'PUT',
          data: params
        };
        if (typeof find === 'string') {
          ajaxOpts.url += '/' + find;
        }
        return $.ajax(ajaxOpts).success(function(r) {
          return next(null, r);
        }).error(function(r) {
          console.error('API failed to update', r);
          return next(r);
        });
      },
      post: function(url, params, next) {
        if (next == null) {
          next = function() {};
        }
        return $.post(url, params).success(function(r) {
          return next(null, r);
        }).error(function(r) {
          console.error('API failed to create', r);
          return next(r);
        });
      }
    },
    utils: {
      loadScript: function(url) {
        if (!$('script[src="' + url + '"]').length) {
          return $('<script src="' + url + '"></script>').appendTo('body');
        }
      },
      loadStyle: function(url) {
        if (!$('link[href="' + url + '"]').length) {
          return $('<link rel="stylesheet" type="text/css" href="' + url + '"/>').appendTo('head');
        }
      }
    }
  };
  if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(obj) {
      var i;
      i = 0;
      while (i < this.length) {
        if (this[i] === obj) {
          return i;
        }
        i++;
      }
      return -1;
    };
  }
  if (!String.prototype.trim) {
    String.prototype.trim = function() {
      return this.replace(/^\s+|\s+$/g, '');
    };
  }
}).call(this);

