Cookie = Class.create({
	initialize: function(options) {
		this.options = Object.extend({
			path: '/',
			domain: '',
			localPath: false,
			secure: false
		}, options || {});
		if (!this.options.expires) {
			var d = new Date();
			d.setTime(d.getTime() + 365 * 24 * 60 * 60 * 1000);
			this.options.expires = d;
		}
		if (this.options.localPath)
			this.options.path = '';
		this.cache = null;
		this.cookie = {};
	},
	set: function(name, value, remove) {
		var o = this.options, remove = remove || false;
		document.cookie = name + '=' + escape(value) +
			(remove ? ';expires=-1' : (o.expires ? ';expires=' + o.expires.toGMTString() : '')) + 
			(o.path ? ';path=' + o.path : '') + 
			(o.domain ? ';domain=' + o.domain : '') +
			(o.secure ? ';secure' : '');
	},
	_parse: function() {
		if (this.cache == document.cookie) return;
		this.cache = document.cookie;
		this.cookie = this.cache.split(';').inject({}, function(h, v) {
			var pair = v.strip().split('=');
			h[unescape(pair[0])] = pair.length < 2 ? true : unescape(pair[1]);
			return h;
		});
	},
	get: function(name) {
		this._parse();
		var value = this.cookie[name];
		return value && !value.blank() ? value : null;
	},
	remove: function(name) {
		this.set(name, '', true);
	},
	removeAll: function() {
		this._parse();
		for (var name in cookie)
			if (typeof name != 'function')
				this.set(name, '', true);
	}
});
