VRT = YAHOO.namespace("trp.vrt.common");

//cookie cutter

//assures singleton executed once
if (typeof VRT.cookie == "undefined")
{

VRT.cookie = function(){

	/**
	 * Cookie storage is partitiioned into two levels:
	 *		the common level,
	 *		the mnemonic prefix level (default)
	 * So applications won't overwrite each other chip.
	 */
	var _impl = YAHOO.util.Cookie;
	var _mne = null;
	// Default to jvlretail cookie and let the child class to override the cookiename
	var _sessionCookieName = "jvlretail_session";
	var _sessionCookieConfig = { path : "/", domain : ".uacollegesavings.com" };
	var _persistentCookieName = "jvlretail_persistent";
	var _persistentCookieConfig = { expires : new Date(new Date().getTime() + 1000 * 3600 * 24 * 365), path : "/", domain : ".troweprice.com" };

	/*
	 * Return the chipname prefixed with the mnemonic.  It is used in the precedence order search.
	 */
	function _getPrefixes (chipName, options /* optional */) { 
		var a = new Array();

		if (options!=null && options.setToCommonLevel==true)
		{
			// only common level.  no prefix.
			a.push(chipName);
		}
		else
		{
			// Search order prefix list.
			if (_mne==null)
			{
				_mne = "";
				//check for Retail.page?
				if (UAK.page.properties.cookieCutterPrefix!=null)
				{
					_mne = UAK.page.properties.cookieCutterPrefix;  //troweprice.js
				}
			}
			a.push(_mne + "." + chipName);
			a.push(chipName);
		}
		return a;	
	}

	function _getChip (chipName) {
		// Search session cookie.
		var hash = _impl.getSubs(_sessionCookieName);
		var prefixes = _getPrefixes(chipName);
		var r = null;

		if (hash!=null)
		{
			for (var i=0; i < prefixes.length; i++)
			{
				r = hash[prefixes[i]];
				if (r != null)
					break;
			}
		}
		if (r==null)
		{
			// Search persistent cookie
			hash = _impl.getSubs(_persistentCookieName);
			if (hash!=null)
			{
				for (var i=0; i < prefixes.length; i++)
				{
					r = hash[prefixes[i]];
					if (r != null)
						break;
				}
			}
		}
		return r;
	}

	function _setChip (chipName, chipValue, options /* optional */) {
		var prefixes = _getPrefixes(chipName, options);
		if (options!=null && options.persistent==true)
		{
			return _impl.setSub(_persistentCookieName, prefixes[0], chipValue, _persistentCookieConfig);
		}
		else
		{
			return _impl.setSub(_sessionCookieName, prefixes[0], chipValue, _sessionCookieConfig);
		}
	}

	function _removeChip (chipName, options /* optional */) {
		if (options!=null && options.persistent==true)
		{
			var hash = _impl.getSubs(_persistentCookieName);
			var prefixes = _getPrefixes(chipName, options);
			if (hash!=null)
			{
				if (hash[prefixes[0]]!=null)
				{
					delete hash[prefixes[0]];
					_impl.setSubs(_persistentCookieName, hash, _persistentCookieConfig);
				}
			}					
		}
		else
		{
			var hash = _impl.getSubs(_sessionCookieName);
			var prefixes = _getPrefixes(chipName, options);
			if (hash!=null)
			{
				if (hash[prefixes[0]]!=null)
				{
					delete hash[prefixes[0]];
					_impl.setSubs(_sessionCookieName, hash, _sessionCookieConfig);
				}
			}
		}
	}

	return{

		getChip : function(chipName) {
					return _getChip(chipName);
		},
	
		/*
		 * @param chipName
		 * @param chipValue
		 * @param options		optional field to set the persistent and set to the common level.
		 * @return {String} The created cookie string.
		 */
		setChip : function(chipName, chipValue, options /* optional */) {
					return _setChip(chipName, chipValue, options);
		},
	
		/*
		 * @param chipName
		 * @param options	optional field to set the persistent and set to the common level.
		 */
		removeChip : function(chipName, options /* optional */) {
					_removeChip(chipName, options);
		}
						
	};
	
}();


//Legacy mappings
retailcookie = VRT.cookie;


}