/**************************************\
 ccl.State
	+ccl.State.Cookie
\**************************************/
/**
 * Cookie状态器
 * @param {object} cookie对象
		path		(optional)
		expires		(optional)
		domain		(optional)
		secure		(optional)
 */
ccl.State.Cookie=function(o){
	ccl.State.Cookie.superclass.constructor.call(this);
	this.path="/";
	this.expires=null;//new Date(new Date().getTime()+(1000*60*60*24*7));
	this.domain=null;
	this.secure=false;
	this.enable=(navigator.cookieEnabled)?true:false;
	if(o)ccl.extend(this,o,true);
	this.state=function(){
		if(!this.enable)return{};
		var cookies={};
		var c=document.cookie+";";
		var re=/\s?(.*?)=(.*?);/g;
		var matches,name,value;
		while((matches=re.exec(c))!=null){
			name=matches[1];
			value=matches[2];
			if(name&&name.substring(0,3)=="ys-")cookies[name.substr(3)]=this.decode(value);
			else cookies[name]=value;
		}
		return cookies
	}.call(this);
}
ccl.inherit(ccl.State.Cookie,ccl.State.Base,{
	/**
	 * 设置Cookie的值
	 * @param {string} name
	 * @param {string} value
	 * @param {int} expires (optional) 保留毫秒数
	 * @param {boolean} b (optional) 默认为true即采用 数值类型编码,自动添加ys-前缀 否则不添加
	 */
	set:function(n,v,e,b){
		if(typeof v=="undefined"||v===null){
			this.remove(n);
			return
		}
		if(e){
			this.expires=new Date(new Date().getTime()+parseInt(e));
		}
		var t="";
		if(b===false)t=n+"="+v;
		else t="ys-"+n+"="+this.encode(v);
		document.cookie=t+
			((this.expires==null)?"":("; expires="+this.expires.toGMTString()))+
			((this.path==null)?"":("; path="+this.path))+
			((this.domain==null)?"":("; domain="+this.domain))+
			((this.secure==true)?"; secure":"");
		ccl.State.Cookie.superclass.set.call(this,n,v)
	},
	/**
	 * 删除指定Cookie项值
	 * @param {string} name
	 */
	remove:function(n){
		document.cookie="ys-"+n+"=null; expires=Thu, 01-Jan-70 00:00:01 GMT"+
			((this.path==null)?"":("; path="+this.path))+
			((this.domain==null)?"":("; domain="+this.domain))+
			((this.secure==true)?"; secure":"");
		ccl.State.Cookie.superclass.remove.call(this,n)
	}
});
ccl.BOM.Cookie=ccl.State.Cookie;