/**
 * CookieManager v1.4
 *
 * Can store any complex JavaScript datatype as a cookie, by
 * serialising data before storing it.
 * Example: <code>cookie = new CookieManager('MyProject');</code>
 *
 * Dependencies class.Utilities.js, JSON.js
 *
 * @author Andrew Ramsden <http://irama.org/web/dhtml/cookie-manager/>
 * @license GNU GENERAL PUBLIC LICENSE (GPL) <http://www.gnu.org/licenses/gpl.html>
 */
function CookieManager (uniqueCookieName) {
	this.init = function (uniqueCookieName)
	{
		if (typeof(Utilities) == 'undefined') return false;
		this.u = new Utilities();
		
		this.cookieName = uniqueCookieName;
		this.cookieData = null;
		
		this.daysTillExpiration = 365;
		
		this.retrieve();
	}
	
	/**
	 * Again, any number of reference levels can be specified, but they
	 * will relate to the baseReferences set when this object was initialised
	 */
	this.get = function ()
	{
		if (this.cookieData == {}) this.retrieve();
		return this.cookieData;
	}
	
	/**
	 * Again, any number of reference levels can be specified, but they
	 * will relate to the baseReferences set when this object was initialised
	 */
	this.set = function (cookieData)
	{
		this.cookieData = cookieData;
		this.save();
	}
	
	/**
	 * Retrieves all data from cookie and populates
	 * this.cookieData
	 */
	this.retrieve = function ()
	{
		if ((data = this.getCookie(this.cookieName))!=null) {
			data = this.u.decode64(data)+'';
			this.cookieData = this.u.JSON.parseString(data);
		} else {
			this.cookieData = null;
		}
	}
	
	/**
	 * Encodes this.cookieData and saves it to document.cookie
	 */
	this.save = function ()
	{
		expires = new Date();
		expires.setDate(expires.getDate()+this.daysTillExpiration);
		
		data = this.u.JSON.objectToString(this.cookieData);
		this.setCookie(this.cookieName, this.u.encode64(data), expires, path='/', domain=window.location.host);
	}
	
	
	this.setCookie = function (name,value,expires,path,domain,secure) {
	  document.cookie = name + "=" + escape (value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "; domain="+window.location.hostname) +
		((secure) ? "; secure" : "");
	}
	
	this.getCookie = function (name) {
	  var arg = name + "=";
	  var alen = arg.length;
	  var clen = document.cookie.length;
	  var i = 0;
	  while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		  return this.getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	  }
	  return null;
	}
	
	this.deleteCookie = function (name,path,domain) {
	  if (this.getCookie(name)) {
		document.cookie = name + "=" +
		  ((path) ? "; path=" + path : "") +
		  ((domain) ? "; domain=" + domain : "; domain="+window.location.hostname) +
		  "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	  }
	}
	
	this.getCookieVal = function (offset) {
	  var endstr = document.cookie.indexOf (";", offset);
	  if (endstr == -1)
		endstr = document.cookie.length;
	  return unescape(document.cookie.substring(offset, endstr));
	}
	
	
	this.init(uniqueCookieName);
}