/*関数*/
function getRequest(){
  if(location.search.length > 1) {
    var get = new Object();
    var ret = location.search.substr(1).split("&");
    for(var i = 0; i < ret.length; i++) {
      var r = ret[i].split("=");
      get[r[0]] = r[1];
    }
    return get;
  } else {
    return false;
  }
}


function is_smartphone(){
  var useragents = [
    'iPhone',         // Apple iPhone
    'iPod',           // Apple iPod touch
    'Android'        // 1.5+ Android
  ];
  var pattern = new RegExp(useragents.join('|'), 'i');
  return pattern.test(navigator.userAgent);
}

CookieStorage = function() {
    var _instance = null;
    return function() {
        if (typeof this._initialize == 'undefined') {
            throw new Error("error");
        }
        if (_instance != null) {
            return _instance;
        }
        _instance = this;
        this._initialize.apply(this, arguments);
    };
}();

CookieStorage.getInstance = function() {
    return new CookieStorage();
};

CookieStorage.prototype = {
    /** @type String */
    classname : 'CookieStorage',
    /** @type String */
    _sepalator : ';',
    /** @type String */
    _path : '',
    /** @type Object */
    _cookies : null,
    /** @type Number */
    _lifetime : null,
    /** @type String */
    _lifetimeValue : '',
    /**
     * 初期化
     */
    _initialize : function() {

        //var res = 
        this._cookies = this._parseCookie(window.document.cookie);

        // 期限をとりあえず1年に
        this.setLifeTime(3600 * 24 * 365);
        this.setPath('/');
    },
    /**
     * Cookieの保持期間を設定
     */
    setLifeTime : function(lifeTime) {
        this._lifetime = lifeTime;
        var now = new Date;
        now.setSeconds(now.getSeconds() + this._lifetime);
        this._lifetimeValue = ' ; expires=' + now.toGMTString();
    },
    setPath : function(path) {
        this._path = '; path=' + path;
    },
    /**
     * Cookie文字列をパースして内容とデータ個数を返す
     * 
     * @param {String}
     *            cookiestr
     * @return {Object}
     */
    _parseCookie : function(cookiestr) {
        var result = {};
        if (cookiestr) {
            var cookies = cookiestr.split(this._sepalator);
            for ( var i = 0; i < cookies.length; i++) {
                var tmp = cookies[i].split('=');
                if (tmp.length > 0) {
                    var key = unescape(tmp[0]).replace(/^\s+|\s+$/g, '');
                    var value = unescape(tmp[1]).replace(/^\s+|\s+$/g, '');
                    result[key] = value;
                }
            }
        }
        return result;
    },
    /**
     * 値の削除
     * 
     * @param {String}
     *            key
     */
    removeItem : function(key) {
        if (typeof this._cookies[key] == "undefined") {
            return;
        }
        delete this._cookies[key];
        var date = new Date();
        date.setYear(date.getYear() - 1);
        // 保持期限を過去にして値をセット
        window.document.cookie = key + '= ;expires=' + date.toGMTString() + this._path;
    },
    /**
     * 値の設定
     * 
     * @param {String}
     *            key
     * @param {String}
     *            value
     */
    setItem : function(key, value) {
        if (value !== null) {
            window.document.cookie = key + '=' + value + this._lifetimeValue
                    + this._path;
            this._cookies[key] = value;
        }
    },
    /**
     * 値の取得
     * 
     * @param {String}
     *            key
     * @return {String} value
     */
    getItem : function(key) {
        return this._cookies[key];
    },
    /**
     * 値のクリア
     */
    clear : function() {
        var data = this._cookies;
        for ( var key in data) {
            this.removeItem(key);
        }
    }
};
/*関数*/


