/*
 * Argilla Memento (Cookie Handling)
 * version 1.0
 *
 * last revision: 2006 07 19
 * 
 * by Davide 'Folletto' Casali <folletto AT gmail DOT com>
 *  web: digitalhymn.com
 *
 * Copyright (C) 2005/2006
 * Feel free to distribute under GPL.
 *
 */

/****************************************************************************************************
 * Memento Class
 * Cookies handling made easy.
 *
 */
var aMemento = function() { this.initialize.apply(this, arguments); } // Prototype-like Constructor

aMemento.prototype = {
	name: null,
	days: 7,
	
	/****************************************************************************************************
	 * Constructor
	 */
	initialize: function(name, days) {
		this.name = name;
		this.days = days || this.days;
	},
	
	/****************************************************************************************************
	 * Set the cookie value.
	 * null to destroy
	 * 
	 * @param		string value
	 */
	set: function(value) {
		var expires = "";
		
		if (this.days) {
			var date = new Date();
			date.setTime(date.getTime() + (this.days*24*60*60*1000));
			var expires = "; expires=" + date.toGMTString();
		}
		
		document.cookie = this.name + "=" + value + expires + "; path=/";
	},
	
	/****************************************************************************************************
	 * Get the cookie value.
	 *
	 */
	get: function() {
		var nameEQ = this.name + "=";
		var ca = document.cookie.split(';');
		
		for(var i=0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ')
				c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length, c.length);
		}
		return null;
	},
	
	/****************************************************************************************************
	 * Destroy the cookie.
	 *
	 */
	destroy: function() {
		this.set(null, -1);
	}
}