function StateCookie(name) {
	this.name = name ? name : '';

	this.load();
}

StateCookie.prototype.hash = null;
StateCookie.daysExp = 555;
StateCookie.debug = 0;

StateCookie.trace = function(str) {
	if (StateCookie.debug) {
		alert(str);
	}
}	

StateCookie.prototype.getExpires = function(days) {
	oneday = 1000*3600*24;
	s = 'expires=';
	d = new Date();
	expires = new Date(d.valueOf() + oneday*days);
	s += expires.toUTCString();
	return s;
}

StateCookie.prototype.setCookie = function(value) {
	document.cookie = 'psp-state-' + this.name + "=" + value + ";" + this.getExpires(StateCookie.daysExp) + ";path=/;";
}

StateCookie.prototype.save = function() {
	this.setCookie(this.serialize());
}

StateCookie.prototype.load = function() {
	var str = Cookies('psp-state-' + this.name);
	this.hash = {};

	if(str && str.length>0) {
		var kv = str.split(':');
		for(var i=0;i<kv.length;i++) {
			var kvitem = unescape(kv[i]).split('|');
			this.hash[kvitem[0]] = kvitem[1];
		}
	}
}

StateCookie.prototype.get = function(k) {
	return this.hash[k];
}

StateCookie.prototype.set = function(k, v) {
	this.hash[k] = v;
	this.save();
}

StateCookie.prototype.remove = function(k) {
	this.hash[k] = null;
    this.save();
}

StateCookie.prototype.serialize = function() {
	var kv = new Array();
	for (var k in this.hash) {
		if (this.hash[k]!=null) {
			kv.push( escape(k + '|' + this.hash[k]) );
		}
	}

	var s = kv.join(':');
	return s;
}

StateCookie.prototype.clear = function() {
	this.hash = {};
	this.save();
}
