// hitolog Api

function HitologApi(options) {
  var defaults = {
  };
  var options = $.extend(defaults, options);
  this.options = options;
  this.initialize();
}

HitologApi.prototype = {
  initialize: function() {
    if (!this.options.serviceUrl) {
      alert("serviceUrl is required.");
      return;
    }
  }
  ,
  callApi: function(apiPath, inData, success, error, options) {
    var self = this;
    var url = this.options.serviceUrl + apiPath;
    this._onApiCalling();
    var params = {
      async: true,
      type: "POST",
      contentType: "application/x-www-form-urlencoded",
      url: url,
      data: $.extend(inData, { _key: this.options.apiKey || "" }),
      dataType: "json",
      timeout: 60000,
      retryLimit: 3,
      success: function(outData, dataType) {
        self._onApiCalled();
        if (success) {
          success(url, inData, outData);
        }
      },
      error: function(xhr, textStatus, errorThrown) {
        self._onApiCalled();
        var outData;
        try {
          outData = window["eval"]("(" + xhr.responseText + ")");
        } catch (e) {
          outData = {
            status_code: xhr.statusCode
          };
        }
        if (error) {
          error(url, inData, outData, xhr);
        }
      }
    };
    if (options) {
      params = $.extend(params, options);
    }
    $.ajax(params);
  }
  ,
  _onApiCalling: function() {
    if (this.options.onApiCalling) {
      this.options.onApiCalling();
    }
  }
  ,
  _onApiCalled: function() {
    if (this.options.onApiCalled) {
      this.options.onApiCalled();
    }
  }
  ,
  _checkAuth: function(url, inData, outData) {
    var sc = outData.status_code;
    return sc == 200 ? true : false;
  }
}
