var DocumentCookie = (function() {
	var Cookie = function(doc, cookieName, val, expiration, path, domain, secure) {
		this._doc = doc;
		this._cookieName = cookieName;
		this._val = unescape(val ? val : '');
		this._expiration = expiration ? new Date((new Date()).getTime()	+ expiration * 1000) : null;
		this._path = path;
		this._domain = domain;
		this._secure = secure ? true : false;
		
	};
	Cookie.AL_PATTERN = /\w+:[^&]*/g;
	F.extend(Cookie.prototype, {
		getVal : function() {
			return this._val;
		},
		
		setVal : function(value) {
			this._val = value;
		},
		
		setExpiration : function(expiration) {
			this._expiration = expiration ? new Date((new Date()).getTime()	+ expiration * 1000) : null;
		},
		
		setPath : function(path) {
			this._path = path;
		},
		
		setDomain : function(domain) {
			this._domain = domain;
		},
		
		setSecure : function(secure) {
			this._secure = secure ? true : false;
		},

		write : function() {
			var val = escape(this._val);
			var cookie = this._cookieName + '=' + val;
			cookie += (this._expiration ? ';expires=' + this._expiration.toGMTString() : '');
			cookie += (this._path ? ';path=' + this._path : '');
			cookie += (this._domain ? ';domain=' + this._domain : '');
			cookie += (this._secure ? ';secure' : '');
			this._doc.cookie = cookie;
			cookie = null;
		},

		remove : function() {
			this._val = '';
			this._expiration = new Date(1970, 1, 2, 0, 0, 0);
		}
	});

	var DocumentCookie = {
		COOKIE_PATTERN : /[^=]+=[^;]*/g,
		doc : null,
		cookieList : {},

		load : function(doc) {
			var _this = this;
			this.doc = doc || document;
			var cookies = this.doc.cookie.match(this.COOKIE_PATTERN);

			F.each(cookies, null, function(cookie) {
				var arr = cookie.split('=');
				arr[0] = arr[0].replace(/;[ ]?/, '');
				_this.cookieList[arr[0]] = new Cookie(_this.doc, arr[0],
						arr[1]);
			});
		},

		save : function() {
			F.each(this.cookieList, null, function(cookie) {
				cookie.write();
			});
		},

		clear : function() {
			F.each(this.cookieList, null, function(cookie) {
				cookie.remove();
			});
		},

		get : function(name) {
			if (this.cookieList[name] == null) {
				this.cookieList[name] = new Cookie(this.doc, name);
			};
			return this.cookieList[name];
		}
	};
	
	return DocumentCookie;
})();

