//  ========================================================
//  tlab-histrecotd.js
//  Copyright 2011 TEAM-LAB
//  ========================================================

if ( typeof(TLAB) == 'undefined' ) TLAB = function() {};

TLAB.historySetup = {
	cookie: "history_cl002",
	maxage: 31536000,
	path: "/",
	maxnum: 10,
	delimitor: ","
};

// TLAB.Cookie
TLAB.Cookie = function (param) {
	param = param || {};
	this.path = param.path;
	this.domain = param.domain;
	this.secure = param.secure;
	this.maxage = param.maxage;
};

TLAB.Cookie._expired = (new Date(1970, 0, 2)).toGMTString();

TLAB.Cookie.prototype.get = function (name) {
	var si, ck = document.cookie + ";";
	if ((si = ck.indexOf(name + "=")) < 0)
		return false;
	return unescape(ck.substring(
			si + name.length + 1, ck.indexOf(";", si)));
};

TLAB.Cookie.prototype.set = function (name, value, maxage) {
	var ck = name + "=" + escape(value);
	maxage = Number(maxage || this.maxage);
	if (isFinite(maxage)) {
		ck += ";expires=" + (new Date((new Date()).getTime() +
				maxage * 1000)).toGMTString();
	}
	document.cookie = ck + ((this.path) ? ";path=" + this.path : "") +
			((this.domain) ? ";domain=" + this.domain : "") +
			((this.secure) ? ";secure" : "");
};

TLAB.Cookie.prototype.remove = function (name) {
	document.cookie = name + "=;expires=" + TLAB.Cookie._expired;
};

// TLAB.HistRecord
TLAB.HistRecord = {};

TLAB.HistRecord.addItem = function(itemCd) {
	function contains(list, v) {
		for (var i = 0; i < list.length; i++) {
			if (v == list[i]) { return true; }
		}
		return false;
	}
	if (!itemCd) return;
	var self = TLAB.HistRecord;
	var setup = TLAB.historySetup;
	var value = self.cookie.get(setup.cookie) || "";
	var list = value.split(new RegExp(setup.delimitor, "g"));
	var newList = [ itemCd ];
	for (var i = 0; i < list.length; i++) {
		if (newList.length >= setup.maxnum) { break; }
		if (list[i] && !contains(newList, list[i])) {
			newList.push(list[i]);
		}
	}
	var newValue = newList.join(setup.delimitor);
	self.cookie.set(setup.cookie, newValue);
};

TLAB.HistRecord.listItem = function() {
	var self = TLAB.HistRecord;
	var setup = TLAB.historySetup;
	return (self.cookie.get(setup.cookie) || "").split(
			new RegExp(setup.delimitor, "g"));
};

TLAB.HistRecord.cookie = new TLAB.Cookie(TLAB.historySetup);

// EOF

