// AtCookie v1.0 - Copyright (c) 2006 Kwangho Seo.
// This copyright notice MUST stay intact for use.
// Author's blog: http://sweetier.com
// Last Updated on 2006. 6. 6.

var _COOKIE = {};

var AtCookie = {
	initialize: function() {
		_COOKIE = {};
		var cookies = document.cookie.split(';');
		for (var i=0; i<cookies.length; i++) {
			var e = cookies[i].indexOf('=');
			var o = decodeURIComponent(cookies[i].substring(0, e)).replace(/^\s*(\S*(\s+\S+)*)\s*$/,'$1');
			_COOKIE[o] =  decodeURIComponent(cookies[i].substring(e+1));
			//log('cookie list: '+o+'='+_COOKIE[o] );
		}
	},

	getCookie: function(name) {
		this.initialize();
		return _COOKIE[name];
	},

	//setcookie ( string name, string value [, int day [, string path [, string domain]]] )
	setCookie: function(name, value, day, path, domain) {
		var cookie = urlEncode(name)+'='+urlEncode(value);

		if (typeof(day)!="undefined" && day!=null) {
			day = Number(day);
			if (day>0) {
				var date = new Date();
				date.setTime(date.getTime() + (day * 24*60*60*1000));
				cookie += '; expires='+date.toGMTString();
			}
		}

		if (typeof(path)!="undefined") {
			if (path!=null && path.length>0) {
				cookie += '; path='+path;
			}
		}
		else {
			cookie += '; path=/';
		}

		if (typeof(domain)!="undefined") {
			if (domain!=null && domain.length>0) {
				cookie += '; domain='+domain;
			}
		}

		document.cookie=cookie;
		_COOKIE[name]=value;
	},

	// removeCookie (string name[, string path])
	removeCookie: function(name, path)
	{
		var date = new Date();
		date.setTime(date.getTime() - (1000*60*60*24));
		var expires = date.toGMTString();
		var cookie = urlEncode(name) + '=; expires='+expires;

		if (typeof(path)!="undefined") {
			cookie += "; path="+path; 
		}
		else {
			cookie += "; path=/"; 
		}
		document.cookie = cookie;
		//log('remove '+cookie);
		if (name in _COOKIE) {
			delete _COOKIE[name];
		}
	},

	removeAllCookies: function(path)
	{
		document.cookie = '';
		this.initialize();
		var c = _COOKIE;
		for (name in c) {
			this.removeCookie(name,path);
		}
	}
};



AtCookie.initialize();

