/**
 * @overview Apeman client side javascript library.
 * @author Taka Okunishi
 * @version 0.0.7
 *
 */
apeman = (function(ap) {
  /**
   * Disable a function for a while
   * @name apeman.disableWhile
   */

  ap.disableWhile = function(namespace, key, interval) {
    var _fn;
    _fn = namespace[key];
    namespace[key] = ap.doNothing.bind(ap);
    return setTimeout(function() {
      return namespace[key] = _fn;
    }, interval || 1000);
  };

  /**
   * Function to do nothing
   * @name apeman.string
   */

  ap.doNothing = function() {};

  /**
   * Apeman query module
   * @name apeman.query
   */

  ap.query = (function(query) {
    query.toQueryString = function(data) {
      var params;
      if (!data) {
        return;
      }
      params = [];
      Object.keys(data).forEach(function(key) {
        var val;
        val = data[key];
        return params.push([key, val].join('='));
      });
      return params.join('&');
    };
    return query;
  })({});

  /**
   * Apeman string module
   * @name apeman.string
   */

  ap.string = (function(string) {
    var PATTERNS;
    PATTERNS = {
      ZENKAKU: /[！-～]/g,
      HANKAKU: /[\!-\~]/g,
      HIRAGANA: /[ぁ-ん]/g,
      KATAKANA: /[ァ-ン]/g
    };
    string.toHiragana = function(str) {
      return str && str.replace(PATTERNS.KATAKANA, function($0) {
        return String.fromCharCode($0.charCodeAt(0) - 0x60);
      });
    };
    string.toKatakana = function(str) {
      return str && str.replace(PATTERNS.HIRAGANA, function($0) {
        return String.fromCharCode($0.charCodeAt(0) + 0x60);
      });
    };
    string.toHankaku = function(str) {
      return str && str.replace(PATTERNS.ZENKAKU, function($0) {
        return String.fromCharCode($0.charCodeAt(0) - 0xFEE0);
      });
    };
    string.toZenkaku = function(str) {
      return str && str.replace(PATTERNS.HANKAKU, function($0) {
        return String.fromCharCode($0.charCodeAt(0) + 0xFEE0);
      });
    };
    string.ambiguousMatch = function(searchWord, pattern) {
      var convert, matchable, matched, toHankaku, toHiragana, toKatakana, toZenkaku, _i, _len, _ref;
      if (!searchWord) {
        return null;
      }
      if (!pattern) {
        return null;
      }
      toHiragana = string.toHiragana;
      toKatakana = string.toKatakana;
      toZenkaku = string.toZenkaku;
      toHankaku = string.toHankaku;
      matchable = toZenkaku(toHiragana(searchWord.trim()));
      _ref = [toHiragana, toKatakana, toZenkaku, toHankaku];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        convert = _ref[_i];
        matched = convert(pattern).match(matchable);
        if (matched) {
          return matched;
        }
      }
      return null;
    };
    return string;
  })({});

  /**
   * Function to throw a NotImplementedError.
   * @name apeman.throwNotImplementedError
   */

  ap.throwNotImplementedError = function() {
    throw new Error('Not implemented.');
  };

})(this.apeman || {});