// Generated by CoffeeScript 1.6.3
(function() {
  this.TheBigDB = (function() {
    function TheBigDB(options) {
      var defaultConfiguration, version;
      if (options == null) {
        options = {};
      }
      version = "1.1.0";
      defaultConfiguration = {
        apiKey: null,
        useSSL: false,
        verifySSLCertificates: false,
        beforeRequestExecution: null,
        afterRequestExecution: null,
        ajaxSuccessCallback: null,
        ajaxErrorCallback: null,
        apiHost: "api.thebigdb.com",
        apiPort: 80,
        apiVersion: "1"
      };
      this.configuration = this.mergeOptions(defaultConfiguration, options);
      if (this.configuration.useSSL && !options.apiPort) {
        this.configuration.apiPort = 443;
      }
      this.clientUserAgent = {
        publisher: "thebigdb",
        version: version,
        language: "javascript"
      };
    }

    TheBigDB.prototype.Statement = function(action, params, successCallback, errorCallback) {
      var method, path;
      if (params.constructor === Array) {
        params = {
          nodes: params
        };
      }
      if (params.constructor === String) {
        params = {
          id: params
        };
      }
      method = action === "get" || action === "show" || action === "search" ? "GET" : "POST";
      path = "/statements/" + action;
      return this.executeRequest(method, path, params, successCallback, errorCallback);
    };

    TheBigDB.prototype.User = function(action, params, successCallback, errorCallback) {
      var method, path;
      method = "GET";
      path = "/users/" + action;
      return this.executeRequest(method, path, params, successCallback, errorCallback);
    };

    TheBigDB.prototype.executeRequest = function(method, path, params, successCallback, errorCallback) {
      var scheme, url, xhr, _base,
        _this = this;
      params.api_key = this.configuration.apiKey;
      scheme = this.configuration.useSSL ? "https" : "http";
      url = "" + scheme + "://" + this.configuration.apiHost + ":" + this.configuration.apiPort + "/v" + this.configuration.apiVersion + path;
      if (method === "GET") {
        url += "?" + this.serializeQueryParams(params);
      }
      xhr = window.ActiveXObject ? new window.ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
      if (typeof (_base = this.configuration).beforeRequestExecution === "function") {
        _base.beforeRequestExecution();
      }
      xhr.open(method, url, true);
      xhr.setRequestHeader("X-TheBigDB-Client-User-Agent", JSON.stringify(this.clientUserAgent));
      if (method === "POST") {
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
      xhr.onreadystatechange = function() {
        var e, response, _base1, _base2, _base3;
        if (xhr.readyState === 4) {
          if (typeof (_base1 = _this.configuration).afterRequestExecution === "function") {
            _base1.afterRequestExecution();
          }
          response = (function() {
            try {
              return JSON.parse(xhr.responseText);
            } catch (_error) {
              e = _error;
              return {
                status: "error",
                error: {
                  code: "0000",
                  description: "The server gave an invalid JSON text: " + xhr.responseText
                }
              };
            }
          })();
          if (response.status === "success") {
            if (typeof (_base2 = _this.configuration).ajaxSuccessCallback === "function") {
              _base2.ajaxSuccessCallback(response);
            }
            if (typeof successCallback === "function") {
              successCallback(response);
            }
          } else {
            if (typeof (_base3 = _this.configuration).ajaxErrorCallback === "function") {
              _base3.ajaxErrorCallback(response);
            }
            if (typeof errorCallback === "function") {
              errorCallback(response);
            }
          }
          return _this.last_response = response;
        }
      };
      if (method === "GET") {
        return xhr.send(null);
      } else {
        return xhr.send(this.serializeQueryParams(params));
      }
    };

    TheBigDB.prototype.serializeQueryParams = function(obj, prefix) {
      var key, param_key, str, value;
      str = (function() {
        var _results;
        _results = [];
        for (key in obj) {
          value = obj[key];
          param_key = prefix ? "" + prefix + "[" + key + "]" : key;
          if (typeof value === "object") {
            _results.push(this.serializeQueryParams(value, param_key));
          } else {
            _results.push(encodeURIComponent(param_key) + "=" + encodeURIComponent(value));
          }
        }
        return _results;
      }).call(this);
      return str.join("&");
    };

    TheBigDB.prototype.mergeOptions = function(obj1, obj2) {
      var key, ret, value;
      ret = obj1;
      for (key in obj2) {
        value = obj2[key];
        ret[key] = value;
      }
      return ret;
    };

    return TheBigDB;

  })();

}).call(this);
