/**
 * Cookie Plugin
 * Copyrigt (c) 2010 - Tyson Lloyd Cadenhead
 * http://tysonlloydcadenhead.com
 * 
 * @param {Object} variable - The variable that contains all of the information for the cookie function.
 * @example cookie({ type: 'add', name: 'cookie_name', value: 'cookie_value' });
 * @desc Creates a new cookie or modifies an existing cookie by the same name
 * @example cookie({ type: 'remove', name: 'cookie_name' });
 * @desc Remove a cookie by the name that is given
 * @example cookie({ type: 'enabled' });
 * @desc Checks whether cookies are enabled in the browser. If they are, the script returns true, if not, the script returns false.
 * @example cookie({ name: 'cookie_name' });
 * @desc Returns the value of the cookie being gotten
 * 
 */

function cookie(variable)
{
	
	switch(variable.type)
	{
		
		// Add / Modify a cookie
		case 'add' : 
			document.cookie = (variable.name + '=' + variable.value);
			return variable.name;
		;break;
		
		// Remove a cookie (expires the cookie)
		case 'remove' :
			document.cookie = variable.name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
		;break;
		
		// Checks if cookies are enabled in the browser. Returns true or false
		case 'enabled' :
			var cookieEnabled = (navigator.cookieEnabled) ? true : false;
			if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
			{
				document.cookie="testcookie";
				cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
			}
			return (cookieEnabled);
		;break;
		
		// Get a cookie value
		default :
			var search = variable.name + "=", returnvalue = "";
			if (document.cookie.length > 0)
			{
			    offset = document.cookie.indexOf(search)
			    if (offset != -1)
				{
			      offset += search.length
			      end = document.cookie.indexOf(";", offset);
			      if (end == -1) end = document.cookie.length;
			      returnvalue=unescape(document.cookie.substring(offset, end))
			    }
		   }
  		   return returnvalue;
		;break;
	}
}

