/**
 * @namespace com.thesis.util.Cookies
 * @imports com.thesis.base.String
 * @imports com.thesis.util.StringBuffer
 * @author 钟军锐 August.R@263.net
 */

/** @id Cookies */
var Cookies = {};

/** @id set */
Cookies.set = function(sName, sValue, iLifecycle, sPath, sDomain, bSecure){
	
	if(!sName || !sValue || sName.trim()=="" || sValue.trim()=="") return;
	
	var sf = new StringBuffer();
	var dExpire = new Date();
	var value = sValue.escape();
	var cv = "";
	
	sf.append(sName);
	sf.append("=");
	sf.append(value);
	
	if(typeof iLifecycle == "number"){
		dExpire.setTime( dExpire.getTime() + (iLifecycle * 1000) );
	}
	sf.append("; expires=");
	sf.append(dExpire.toGMTString());
	
	if(typeof sPath == "string"){
		sf.append("; path=");
		sf.append(sPath);
	}
	
	if(typeof sDomain == "string"){
		sf.append("; domain=");
		sf.append(sDomain);
	}
	
	if(bSecure == true)
		sf.append("; secure");
	
	cv = sf.toString();
	if(cv.length < 4096){
		if(sName.indexOf("_divide_")==-1) this.del(sName, sPath, sDomain);
		document.cookie = cv;
	}
	else{
		var max = Math.ceil(value.length / 3800);
		for(var i=0; i<max; i++){
			if(i==0)
				this.set(sName,"^divide|" + max + "$" + value.substr(0,3800), iLifecycle, sPath, sDomain, bSecure);
			else
				this.set(sName + "_divide_" + i, value.substr(i*3800,3800), iLifecycle, sPath, sDomain, bSecure);
		}
	}
};

/** @id del */
Cookies.del = function(sName, sPath, sDomain){
	if(!sName || sName.trim()=="") return;
	var value = this.get(sName);
	if(!value) return;
	value = escape(value);
	var dExpire = new Date();
	dExpire.setTime(1);
	var sPart =	( (sPath && sPath.trim()!="")? "; path=" + sPath : "" ) + 
				( (sDomain && sDomain.trim()!="")? "; domain=" + sDomain : "" ) + 
				( "; expires=" + dExpire.toGMTString() );
	if(value.length > 3800){
		var max = Math.ceil(value.length / 3800);
		for(var i=1; i<max; i++){
			document.cookie = sName + "_divide_" + i + "=" + sPart;
		}
	}
	document.cookie = sName + "=" + sPart;
};

/** @id get */
Cookies.get = function(sName){
	var result = undefined;
	if(typeof sName != "string" || sName.trim() == ""){
		return result;
	}
	
	var cke = document.cookie;
	var cv = cke.split("; ");
	var tmp;
	var name;
	var value;
	var cvam = {};
	var cvat = [];
	var i = "";
	var j=0;
	var k=0;
	for(i in cv){
		j = cv[i].indexOf(sName);
		k = cv[i].indexOf("=");
		if(k==-1 || j==-1 || j>=k) continue;
		
		tmp = cv[i].split("=");
		if(tmp.length != 2) continue;
		
		name = tmp[0].trim();
		value = tmp[1].trim();
		if(name.indexOf("_divide_")>0) cvam[name] = value;
		else if(name != "") cvat[cvat.length] = [name, value];
	}
	var sDivide = escape("^divide|");
	var Separator = escape("$");
	for(tmp in cvat){
		i = cvat[tmp][1];
		j = i.indexOf(sDivide);
		if(j!=0){
			result = i;
		}
		else{
			var s = i.indexOf(Separator);
			k = i.substring(sDivide.length, s);
			result = i.substr(s + Separator.length);
			for(var l=0; l<k; l++){
				result += cvam[sName + "_divide_" + l];
			}
		}
	}
	if(result)	return unescape(result);
	else return result;
};

