/**
 * Provides an object for reading and writing cookies with JavaScript.  JavaScript accesses cookies
 * for a particular page with the document.cookies property.  The cookies property uses the following
 * format: "cookiename1=value1; cookiename2=value2".
 *
 * @return object
 * @author Henry Rivera
 *
 * @ctor
 * Parses values in the cookie property of the document object that was passed as a reference.  Individual
 * cookies are given default values, and stored in an array property.
 *
 * @param  object docObject Reference to a document object to manage cookies for.
 *
 * @return object
 */
function Cookie(docObject) {
	return this._constructor(docObject);
}


/**************************************
 * Private properties.                *
 **************************************/

	/**
	 * Reference to a document object.  The cookies that this object will
	 * manage come from the cookies property of that reference.  This is needed
	 * when managing cookies for pages with multiple frames.
	 *
	 * @private
	 */
	Cookie.prototype._document = new Object();


/**************************************
 * Private methods.                   *
 **************************************/

	/**
	 * Object constructor.  This method exists in an attempt to clarify where initialization
	 * occurs.  See the documentation for Cookie::Cookie.
	 *
	 * @return object
	 * @private
	 */
	Cookie.prototype._constructor = function (docObject) {
		this._document = docObject;
		this.__cookies = new Array();

		if (typeof(debugFrame) != 'undefined') {
			this.__output = window.parent.frames[debugFrame].document.body;
			if (!this.__output) { //-> Frame does not exist.
				this.__output = null;
			}
		} else {
			this.__output = null;
		}

		tempCookies = this._document.cookie.split('; ');

		for (var i = 0; i < tempCookies.length; i++) {
			/* Split the name / value pair. */
			tempValues = tempCookies[i].split('=');

			/* Note, the name property is the same as the variable name of each
			 * cookie object stored in the __cookies array. */
			var cookie = new Object();

			/* Set default properties for the cookie object being stored. */
			cookie.name = tempValues[0];
			cookie.value = tempValues[1];
			cookie.store = false;
			cookie.expires = null;
			cookie.path = null;
			cookie.domain = null;
			cookie.secure = false;
			this[cookie.name] = cookie;
		}

		// Debugging.
		if (this.__output) {
			for (property in this) {
				if (property.substr(0,2) == '__' || typeof(this[property]) == 'function') {
					continue;
				}

				var cookie = this[property];
				//this.__output.innerHTML += property + ' = ' + cookie.value + '<br />';
			}
		}

		return this;
	}

	/**
	 * Stores a cookie value that has been defined by this class.  This method should never be called
	 * explicity.  This method has been written in an effort to split some of the logic between
	 * defining a cookie and actually storing a cookie.  When saving a cookie, always call the public
	 * method saveCookie.
	 *
	 * @param string name Name of the cookie to store.
	 *
	 * @return boolean
	 * @private
	 */
	Cookie.prototype._storeCookie = function(name) {
		var cookieString = '';
		var cookie = this[name];

		if (!cookie) {
			return false;
		}

		cookieString += cookie.name + '=' + escape(cookie.value);
		if (cookie.expires) {
			cookieString += '; expires=' + cookie.expires;
		}
		if (cookie.path) {
			cookieString += '; path=' + cookie.path;
		}
		if (cookie.domain) {
			cookieString += '; domain=' + cookie.domain;
		}
		if (cookie.secure) {
			cookieString += '; secure';
		}
		if (this.__output) {
			this.__output.innerHTML += cookieString + '<br />';
		}

		this._document.cookie = cookieString;

		return true;
	}

/**************************************
 * Public methods.                    *
 **************************************/


	/**
	 * Stores a cookie value.  Returns true on success or false if a name and value were not passed.
	 *
	 * @param string  name    Name of the cookie to set.
	 * @param string  value   Value of the cookie to set.
	 * @param date    expires Expiration date to set.
	 * @param string  path    Path that this cookie is to be stored for.  Defaults to root (/).
	 * @param string  domain  Domain cookie is to be associated with.
	 * @param boolean secure  Whether or not this cookie will be secure.
	 *
	 * @return boolean
	 * @todo Enhance expiration date handling to accept dates in multiple formats. --hrivera, 2005/02/08 11:56:49
	 */
	Cookie.prototype.saveCookie = function(name, value, expires, path, domain, secure) {
		/* Check that a name and a value were passed. */
		if (!name || !value) {
			return false;
		}

		var cookie = new Object();
		cookie.name = name;
		cookie.value = value;
		cookie.store = true;

		if (expires) {
			cookie.expires = expires;
		} else {
			cookie.expires = null;
		}
		if (path) {
			cookie.path = path;
		} else {
			cookie.path = null;
		}
		if (domain) {
			cookie.domain = domain;
		} else {
			cookie.domain = null;
		}
		if (secure) {
			cookie.secure = true;
		} else {
			cookie.secure = false;
		}

		this[name] = cookie;

		this._storeCookie(name);

		return true;

	}

	/**
	 * Retrieves a cookie value.  The source that created the cookie does not matter.
	 *
	 * @param string name Name of the cookie to retrieve.
	 * @return mixed
	 */
	Cookie.prototype.getCookie = function(name) {
		if (!this[name]) {
			return false;
		}

		return unescape(this[name].value);

	}


	/**
	 * Deletes a cookie.  The cookie to be deleted may have been created by this class, or another application.
	 *
	 * @param string name Name of the cookie to delete.
     * @return void
	 */
	Cookie.prototype.deleteCookie = function(name) {
		var expireDate = new Date((new Date()).getTime() - 3600000).toGMTString();
		var cookieString = name + '=null; expires=' + expireDate;

		this._document.cookie = cookieString;

		return;
	}

