/* Copyright (c) 2008 Daniel J. Hood
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * A cookie creator/reader.  Largely ported from mootool's Cookie class  
 * implemention (http://docs.mootools.net/Remote/Cookie.js).  Additional 
 * method inspired by Carlos Reche's script.aculo.us based Cookie 
 * implemention (http://wiki.script.aculo.us/scriptaculous/show/Cookie).
 *
 * Requires Prototype 1.6
 */
var Cookie = {

	/**
	 * The default values for the optional values.
	 */
	options: {
		domain: false, 
		path: false,
		duration: false,
		secure: false
	},
	
	/**
	 * Set a cookie with the specified key and value.  The last argument 
	 * "options" is an object which my set the following options.  Returns 
	 * an object representation of the cookie which was set (including URI 
	 * encoding of the value).
	 *
	 * domain: the domain to restrict the cookie to (defaults to none)
	 * path: the specific path to restrict the cookie to (defaults to none)
	 * duration: time (in days) to persist the cookie (defaults to session cookie)
	 * secure: should client only transmit the cookie over https? (defaults to false) 
	 */
	set: function(key, value, options) {
		options = Object.extend(Object.clone(Cookie.options), options);
		value = encodeURIComponent(value);
		if (options.domain) {
			value += '; domain=' + options.domain;
		}
		if (options.path) { 
			value += '; path=' + options.path;
		}
		if (options.duration){
			var date = new Date();
			date.setTime(date.getTime() + options.duration * 24 * 60 * 60 * 1000);
			value += '; expires=' + date.toGMTString();
		}
		if (options.secure) {
			value += '; secure';
		}
		document.cookie = key + '=' + value;
		return Object.extend(Object.clone(options), {'key': key, 'value': value});
	},
	
	/**
	 * Retrieves the value of the cookie with the specified key.
	 */
	get: function(key) {
		var value = document.cookie.match('(?:^|;)\\s*' + key.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1') + '=([^;]*)');
		return value ? decodeURIComponent(value[1]) : false;
	},

	/**
	 * Removes the specified cookie.  The first argument my either be the 
	 * key the cookie is stored under, or optionally on object returned by
	 * Cookie.set().  Additional options may be passed in to constrain the
	 * domain and path.
	 */	
	remove: function(cookie, options) {
		if (cookie!= null && typeof cookie == "object" && 'key' in cookie) { 
			Cookie.set(cookie.key, '', Object.extend(Object.clone(cookie), {duration: -1}));
		} else { 
			Cookie.set(cookie, '', Object.extend(Object.clone(options), {duration: -1}));
		}
	},
	
	/**
	 * Best attempt at determining whether or not the client accepts cookies.
	 */
	accept: function() {
		if (typeof navigator.cookieEnabled == 'boolean') {
      		return navigator.cookieEnabled;
    	}
	    Cookie.set('_test', '1');
    	return (Cookie.erase('_test') === '1');
    }
};

