/**
 *	cookieを利用して検索に必要な情報をブラウザに記憶させるモデル
 *	Ex. var Cookie = new MemoryModel;
 */
function MemoryModel()
{
	//	クッキー保存位置の確定
	var pos = document.location.pathname.indexOf('/', 1);
	if (pos == -1)
	{
		pos = 1;
	}
	this.hostname = document.location.hostname;
	this.pathname = document.location.pathname.substring(0, pos);


/*	--------------------------------------------------------------------------
	エンコード関数
*/
	if (!!window.encodeURIComponent)
	{
		this.enc = function(str)
		{
			return window.encodeURIComponent(str).replace(/%2F/g, '/');
		};

		this.dec = function(str)
		{
			return window.decodeURIComponent(str);
		};
	}
	else
	{
		this.enc = function(str)
		{
			if ('h'.toString(16).toUpperCase() == null) { return escape(str); }

			var ltr = cod = uni = '';
			var len = (!!str) ? str.length : 0;
			for (var ii=-1; ++ii < len;)
			{
				ltr = str.charAt(ii);
				if (ltr == '/' || ltr == '.' || ltr == '-' || ltr == '@' || ltr == '+' || ltr == '*' || ltr == '_')
				{
					uni += ltr;
				}
				else
				{
					cod = str.charCodeAt(ii);
					if (cod <= 255)
					{
						if (cod <= 15) { uni += "%0" + cod.toString(16).toUpperCase(); }
						else if (cod >= 48 && cod <= 57)  { uni += ltr; }
						else if (cod >= 65 && cod <= 90)  { uni += ltr; }
						else if (cod >= 97 && cod <= 122) { uni += ltr; }
						else { uni += "%" + cod.toString(16).toUpperCase(); }
					}
					else
					{
						uni += "%u" + cod.toString(16).toUpperCase();
					}
				}
			}

			return uni;
		};

		this.dec = function(str)
		{
			return unescape(str);
		};
	}


/*	--------------------------------------------------------------------------
	クッキー情報を取得
*/
	this.Cookie = new Object();
	if (document.cookie)
	{
		var AA = document.cookie.replace(/ /g,'').split(';');

		for (var ii = -1; ++ii < AA.length;)
		{
			var pos = AA[ii].indexOf('=');
			if (pos == -1)
			{
				continue;
			}

			var cookieKey = AA[ii].substring(0, pos);
			var cValues = this.dec(AA[ii].substring(pos + 1)).split('&');
			var rtn = new Object();

			switch (cookieKey)
			{
			case 'RECENTLY':
			case 'FAVORITE':
				for (var jj = 0; jj < cValues.length; ++jj)
				{
					var Values = cValues[jj];
					var pos = Values.indexOf('=');
					if (pos == -1)
					{
						continue;
					}

					var vName = Values.substring(0, pos);
					rtn[vName] = Values.substring(pos + 1).split(',');
				}

				this.Cookie[cookieKey] = rtn;
				break;

			//	Simple
			case 'CONDITION':
			case 'LEARN':
				for (var jj = 0; jj < cValues.length; ++jj)
				{
					var Values = cValues[jj];
					var pos = Values.indexOf('=');
					if (pos == -1)
					{
						continue;
					}

					rtn[Values.substring(0, pos)] = Values.substring(pos + 1);
				}
				this.Cookie[cookieKey] = rtn;
				break;
			}
		}
	};


/*	--------------------------------------------------------------------------
	処理関数
*/
	//	検索設定
	this.getCondition = function()
	{
		return this._getSimpleCookie('CONDITION');
	};

	this.setCondition = function(conds)
	{
		return this._setSimpleCookie('CONDITION', conds);
	};

	this.addCondition = function(name, value)
	{
		return this._addSimpleCookie('CONDITION', name, value);
	};


	//	最近訪れた項目
	this.getRecently = function(bName)
	{
		return this._getCookie('RECENTLY', bName);
	};

	this.setRecently = function(bName, bDatas)
	{
		return this._setCookie('RECENTLY', bName, bDatas);
	};

	this.addRecently = function(bName, bData)
	{
		return this._addCookie('RECENTLY', bName, bData);
	};

	this.removeRecently = function(bName, bData)
	{
		return this._removeCookie('RECENTLY', bName, bData);
	};


	//	お気に入り項目
	this.getFavorite = function(bName)
	{
		return this._getCookie('FAVORITE', bName);
	};

	this.setFavorite = function(bName, bDatas)
	{
		return this._setCookie('FAVORITE', bName, bDatas);
	};

	this.addFavorite = function(bName, bData)
	{
		return this._addCookie('FAVORITE', bName, bData);
	};

	this.removeFavorite = function(bName, bData)
	{
		return this._removeCookie('FAVORITE', bName, bData);
	};


	//	その他いろいろ設定
	this.getLearn = function()
	{
		return this._getSimpleCookie('LEARN');
	};

	this.setLearn = function(conds)
	{
		return this._setSimpleCookie('LEARN', conds);
	};

	this.addLearn = function(lName, lValue)
	{
		return this._addSimpleCookie('LEARN', lName, lValue);
	};


/*	--------------------------------------------------------------------------
	クッキー取得関数 Simple版
*/
	//	Cookieの取得
	this._getSimpleCookie = function(cookieKey)
	{
		if (!!this.Cookie[cookieKey])
		{
			return this.Cookie[cookieKey];
		}
		else
		{
			return new Object();
		}
	};

	//	Cookieの設定
	this._setSimpleCookie = function(cookieKey, conds)
	{
		this.Cookie[cookieKey] = conds;

		var cValue = '';

		if (typeof(conds) == 'object')
		{
			var cArray = '';
			for (var ii in conds)
			{
				cArray += '&' + ii + '=' + conds[ii];
			}
			cValue = cArray.substring(1);
		}

		return this.__setCookie(cookieKey, cValue);
	};

	//	Cookieの追加設定
	this._addSimpleCookie = function(cookieKey, lName, lValue)
	{
		var conds = this._getSimpleCookie(cookieKey);
		conds[lName] = lValue;

		return this._setSimpleCookie(cookieKey, conds);
	};


/*	--------------------------------------------------------------------------
	クッキー取得関数
*/
	//	Cookieの取得
	this._getCookie = function(cookieKey, bName)
	{
		if (!!this.Cookie[cookieKey])
		{
			return (bName)
			?	((!!this.Cookie[cookieKey][bName]) ? this.Cookie[cookieKey][bName] : new Array())	//	Array
			:	this.Cookie[cookieKey]	//	Object
			;
		}
		else
		{
			return (bName) ? new Array() : new Object();
		}
	};

	//	Cookieの設定
	this._setCookie = function(cookieKey, bName, bDatas)
	{
		//	現在のクッキー情報を取得
		var oldValue = this._getCookie(cookieKey);

		//	データの代入
		oldValue[bName] = bDatas;
		this.Cookie[cookieKey] = oldValue;

		//	文字化
		var rtnstr = '';
		for (var bName in oldValue)
		{
			rtnstr += '&' + bName + '=' + oldValue[bName].join(',');
		}

		return this.__setCookie(cookieKey, rtnstr.substring(1));
	};

	//	Cookieの追加設定
	this._addCookie = function(cookieKey, bName, bData)
	{
		if (!bData)
		{
			return true;
		}

		//	現在のクッキー情報を取得
		var oldDatas = this._getCookie(cookieKey, bName);

		//	新しいデータリスト
		var newDatas = new Array();
		newDatas[newDatas.length] = bData;

		for (var ii = 0; ii < oldDatas.length; ++ii)
		{
			if (oldDatas[ii] == null)
			{
				break;
			}
			else if (oldDatas[ii] == bData)
			{
				continue;
			}
			newDatas[newDatas.length] = oldDatas[ii];
		}

		return this._setCookie(cookieKey, bName, newDatas);
	};

	//	Cookie情報の削除
	this._removeCookie = function(cookieKey, bName, bData)
	{
		if (!bData)
		{
			return true;
		}

		//	現在のクッキー情報を取得
		var oldDatas = this._getCookie(cookieKey, bName);

		//	新しいデータリスト
		var newDatas = new Array();

		var cnt = 0;
		for (var ii = 0; ii < oldDatas.length; ++ii)
		{
			if (oldDatas[ii] == bData)
			{
				newDatas = newDatas.concat(oldDatas.slice(ii +1));
				break;
			}

			newDatas[newDatas.length] = oldDatas[ii];

			if (++cnt >= 50)
			{
				break;
			}
		}

		return this._setCookie(cookieKey, bName, newDatas);
	};


	//	Cookieの設定本体
	this.__setCookie = function(cookieKey, cValue)
	{
		var dttm = new Date();
		dttm.setTime(dttm.getTime() + 31536000000);

		var str = cookieKey + "=" + this.enc(cValue) + "; "
		+	"domain=" + this.hostname + "; path=" + this.pathname + "; "
		+	"expires=" + dttm.toGMTString() + "; "
		;

		return (document.cookie = str) ? true : false;
	};

}
var memorymodel = new MemoryModel();
