/*
|| Cookie Management Class (cookie_manager.js)
|| Script Version: 0.1.0
|| Copyright (C) 2004 Hawk <w3l_admin@hawk.34sp.com>
|| WebSite URL: http://www.hawk.34sp.com/
*/

/**
 * @static
 * @access public
 * @param int
 * @param string 
 * @return Date
 * 
 */
Date.fromNow = function(num, u) {
	var units = {
		'Y':'FullYear',
		'M':'Month',
		'D':'Date',
		'h':'Hours',
		'm':'Minutes',
		's':'Seconds'
	};

	if(units[u]==null) return null;

	var d = new Date();
	    d['set'+ units[u]](d['get'+ units[u]]() + num);
	    return d;
}

/**
 * class CookieManager
 * 
 * 
 */
function CookieManager () {
	/**
	 * @static
	 * @access private
	 * 
	 */
	var id = new Object();
	var cookies = {};
	var past = Date.fromNow(-1, 'Y');

	/**
	 * [Constructor]
	 * 
	 * @param Date
	 * @param string
	 * @param string
	 * @param bool
	 * 
	 */
	CookieManager = function (_expires, _path, _domain, _secure) {
		this.expires= _expires!=null ? _expires: CookieManager.expires;
		this.path   = _path   !=null ? _path   : CookieManager.path;
		this.domain = _domain !=null ? _domain : CookieManager.domain;
		this.secure = _secure !=null ? _secure : CookieManager.secure;
		
		var privates = {};
		this._getPrivate = function(o, name) {
			if(o==id) return privates[name];
			else undefined;
		}
		this._setPrivate = function(o, name, value) {
			if(o==id) privates[name] = value;
		}
	}

	//initializing static members ...

	/**
	 * @static
	 * @access public
	 * 
	 */
	CookieManager.expires= null;	//:Date
	CookieManager.path   = null;	//:string
	CookieManager.domain = null;	//:string
	CookieManager.secure = false;	//:bool

	/**
	 * @static 
	 * @access private
	 * 
	 */
	function readCookies() {
		cookies = {};
		var temp = [];
		if(document.cookie != '') {
			var datas= document.cookie.split("; ");
			for(var i=0; i<datas.length; i++){
				temp=datas[i].split("=");
				cookies[temp[0]]=unescape(temp[1]);
			}
		}
	}
	readCookies();

	/**
	 * @static
	 * @access public
	 * @param string key
	 * @param string value
	 *
	 */
	CookieManager.setCookie = function(key, value) {
		var temp = key +"="+ escape(value);
		if(this.expires != null) temp+="; expires="+ this.expires.toGMTString();
		if(this.path    != null) temp+="; path="   + this.path;
		if(this.domain  != null) temp+="; domain=" + this.domain;
		if(this.secure)  temp += "; secure";

		document.cookie=temp;
		readCookies();
	}


	/**
	 * @static
	 * @access public
	 * @param string key
	 * @param string defValue
	 * @return string
	 * 
	 */
	CookieManager.getCookie = function (key, defValue) {
		return cookies[key]!=null ? cookies[key] : defValue;
	}
	
	/**
	 * @static
	 * @access public
	 * @param key
	 * 
	 */
	CookieManager.deleteCookie = function (key) {
		var tmp = this.expires;
		this.expires = past;
		this.setCookie(key, 'dummy');
		this.expires = tmp;
		readCookies();
	}

	/**
	 * @access public
	 * @param string key
	 * @param string value
	 * 
	 */
	CookieManager.prototype.setCookie = CookieManager.setCookie;

	/**
	 * @access public
	 * @param string key
	 * @param string defValue
	 * @return string
	 * 
	 */
	CookieManager.prototype.getCookie = CookieManager.getCookie;
	
	/**
	 * @access public
	 * @param key
	 * 
	 */
	CookieManager.prototype.deleteCookie = CookieManager.deleteCookie;

}; CookieManager();

