var ownersCookie;
var myPageValues;

var myPageSettingsArray;
function CookieFunctions(funcType,mySettings){
	//var myCookie = document.getElementById(cookieID);
	switch(funcType){
		case "loadPortlet":
			//myPortletArray.push({"id":myCookie.id,"settings": ["open"]});
		break;
		case "checkCookie":
			ownersCookie = unescape(document.cookie);
			alert(ownersCookie);
		break;
		case "read":
			ownersCookie = unescape(document.cookie);
		break;
		
		case "initPageCookie":
//				alert("nocookie found - make it ");
		break;
		
		case "initPortlet":
			var myCurrentSettings = mySettings;
			//this is where you would read the cookie for an existing entry or write out the passed defaults
			var myTempCookieArray = serialjs_GetCookie("owners.portlets");
//			alert("initPortlet "+myTempCookieArray);
			if(myTempCookieArray==null){
				myTempCookieArray = new Array(mySettings);
				serialjs_SetCookie("owners.portlets",myTempCookieArray);
				//CookieFunctions("initPageCookie", myTempCookieArray);
			}
			myPageSettingsArray = myTempCookieArray;
//			alert("myPageSettingsArray "+myPageSettingsArray[myPageSettingsArray.length-1][0]);
			for (c=0; c<myTempCookieArray.length;c++){
				if(myTempCookieArray[c][0]==mySettings[0]){
					//myCurrentSettings = check for cookie with id cookieID;
					myCurrentSettings = myTempCookieArray[c];
					return myCurrentSettings;
				}
			}
			myTempCookieArray.push(mySettings);
			myPageSettingsArray = myTempCookieArray;
			serialjs_SetCookie("owners.portlets",myPageSettingsArray);
			return myCurrentSettings;
		break;
	}
}
function upDateCookieArray(myLocalArray){
	//alert("Portlet "+myLocalArray[0]+" has changed its settings to "+myLocalArray);
	for (c=0; c<myPageSettingsArray.length;c++){
		if(myPageSettingsArray[c][0]==myLocalArray[0]){
			//myCurrentSettings = check for cookie with id cookieID;
			myPageSettingsArray[c] = myLocalArray;
			serialjs_SetCookie("owners.portlets",myPageSettingsArray);
		}
	}
}
/**
 * serial.js
 *
 *
 * functions to store and retrieve complex variables (nulls, strings, booleans,
 * numbers, and n-dimensional arrays containing any combination of those
 * datatypes either as strings or as cookies.
 *
 *
 * public functions:
 *     variant serialjs_Deserialize(String p_cin)
 *     variant serialjs_GetCookie(String p_cname)
 *     String serialjs_Serialize(variant p_xin)
 *     void serialjs_SetCookie(String p_cname, variant p_xvalue)
 */



/** values returned by typeof() for various datatypes */
var /*String*/ m_serialjs_cTYPE_ARR = typeof([ 123 ]); //actually returns "object"
var /*String*/ m_serialjs_cTYPE_CHR = typeof("123");
var /*String*/ m_serialjs_cTYPE_LOG= typeof(true);
var /*String*/ m_serialjs_cTYPE_NUM = typeof(123);

/** values which can be stored as a single character */
var /*String*/ m_serialjs_cCODE_LOG_N = "l";
var /*String*/ m_serialjs_cCODE_LOG_Y = "L";
var /*String*/ m_serialjs_cCODE_NUL = "0";

/** start of multi-character data */
var /*String*/ m_serialjs_cCODE_ARR = "[";
var /*String*/ m_serialjs_cCODE_CHR = "c";
var /*String*/ m_serialjs_cCODE_NUM = "n";

/** end of multi-character data */
var /*String*/ m_serialjs_cCODE_END = ";";





/**
 * decode a value/array encoded with serialjs_Serialize back to its original form.
 * serialjs_Deserialize(serialjs_Serialize(x)) == x for all x.
 *
 * @param p_cin a variable encoded by serialjs_Serialize
 * @return the value originally decoded
 */
function /*variant*/ serialjs_Deserialize(/*String*/ p_cin) {
	var /*int*/ nstart = 0;

	function /*variant*/ Recur() {
		var /*char*/ ccode;
		var /*char*/ cchar;
		var /*String*/ cret;
		var /*variant[]*/ aret;
		var /*variant*/ xelement;

		if (nstart >= p_cin.length)
			return null;
		switch(ccode = p_cin.charAt(nstart++)) {
			case m_serialjs_cCODE_END: return null;
			case m_serialjs_cCODE_NUL: return null;
			case m_serialjs_cCODE_LOG_N: return false;
			case m_serialjs_cCODE_LOG_Y: return true;
			case m_serialjs_cCODE_ARR:
				aret = [];
				do
					if ((xelement = Recur()) == null)
						break;
					else
						aret[aret.length] = xelement;
				while(true);
				return aret;
			default:
				cret = "";
				do
					if (nstart >= p_cin.length)
						break;
					else if ((cchar = p_cin.charAt(nstart++)) == m_serialjs_cCODE_END)
						break;
					else 
						cret += cchar;
				while(true);
				switch(ccode) {
					case m_serialjs_cCODE_NUM: return Number(cret);
					case m_serialjs_cCODE_CHR: default: return unescape(cret);
				} //end switch
		} //end switch
	} //end function

	return Recur();
} //end function


/**
 * retrieve an encoded cookie stored with serialjs_Serialize
 *
 * @param p_cname the name of the cookie to retrieve
 */
function /*variant*/ serialjs_GetCookie(/*String*/ p_cname) {
	var /*String*/ clead = " " + escape(p_cname) + "=";
	var /*int*/ nlead = clead.length;
	var /*String*/ ccookie = " " + document.cookie;
	var /*int*/ nstart = ccookie.indexOf(clead);
	var /*int*/ nend;

	if (nstart < 0)
		return null;
	if ((nend = ccookie.indexOf(";", nstart + nlead)) < 0)
		nend = ccookie.length;
	return serialjs_Deserialize(unescape(ccookie.substring(nstart + nlead, nend)));
} //end function




/**
 * encode a complex variable into a string for storage (e.g. as a cookie).
 * Can handle nulls, strings, booleans, numbers, and arrays (n-dimensional)
 * containing any combination of those datatypes.  Object type variables
 * (incl. Date) are not supported.
 * serialjs_Serialize(serialjs_Deserialize(x)) == x for all x.
 *
 * @param p_xin a variable to be encoded
 * @return a String equivalent of p_xin which can be stored and later decoded
 *         using serialjs_Deserialize
 */
function /*String*/ serialjs_Serialize(/*variant*/ p_xin) {
	var /*String*/ cret;

	if (p_xin == null)
		return m_serialjs_cCODE_NUL;
	switch(typeof(p_xin)) {
		case m_serialjs_cTYPE_CHR: return m_serialjs_cCODE_CHR + escape(p_xin) + m_serialjs_cCODE_END;
		case m_serialjs_cTYPE_LOG: return p_xin ? m_serialjs_cCODE_LOG_Y : m_serialjs_cCODE_LOG_N;
		case m_serialjs_cTYPE_NUM: return m_serialjs_cCODE_NUM + p_xin + m_serialjs_cCODE_END;
	} //end switch
	cret = m_serialjs_cCODE_ARR;
	for (var /*int*/ i = 0; i < p_xin.length; i++)
		cret += serialjs_Serialize(p_xin[i]);
	return cret + m_serialjs_cCODE_END;
} //end function



/**
 * store a non-string variable (nulls, strings, booleans, numbers, and arrays
 * (n-dimensional) containing any combination of those datatypes into a specially
 * encoded cookie readable by serialjs_GetCookie()
 * this cookie will expire in one year.
 *
 * @param p_cname the name of the cookie to store the variable as
 * @param p_xvalue the data to store
 */
function /*void*/ serialjs_SetCookie(/*String*/ p_cname, /*variant*/ p_xvalue) {
	document.cookie = p_cname + "=" + escape(serialjs_Serialize(p_xvalue)) + ";expires=" + new Date(new Date().getTime() + 365 * 1000 * 60 * 60 * 24).toGMTString() + ";path=/";
} //end function
