// JavaScript Document

var crumbs = new function()
{
	var gc_expire = 15552000000; // approx. 6 months
	this.data = {};
	return {
		// JSON Cookie Methods
		fetch: function(name) {
			this.data = {};
			var raw = document.cookie.split('; ');
			var cdata = '';
			if(raw){
				for(var rawi = 0; rawi <raw.length; rawi++ )
				{
					cdata = unescape(raw[rawi]);
					var adata = cdata.match ( name+'=(.*?)(;|$)' );
			 		if( adata ){ this.data = JSON.parse(adata[1]); }
				}
			}
			if(this.data==false){ this.data = {}; }
			return this.data;
		},
		store: function(name, expires, path) {
			var expires = expires||(new Date((new Date()).getTime()+15552000000));
			var path = path||'/';
//			this.gc();
			document.cookie = name+'='+escape(JSON.stringify(this.data))+'; expires='+expires.toGMTString()+'; path='+path;
		},
		gc: function() { // Garbage Collection
			var expire = (new Date((new Date()).getTime() - gc_expire ));
			for( var gi in this.data )
			{
				if(gi.indexOf('$')===0)
				{
					if( this.data[gi].lastTime < expire )
					{
						delete this.data[gi];
					}
				}
			}
		},
		// Single Cookie methods
		get: function(name){
			var results = document.cookie.match ( name + '=(.*?)(;|$)' );
	 		if ( results ) return ( unescape ( results[1] ) );
		  else return null;
		},
		set: function(name,value,expires){
			expiretime = (expires)?'; expires='+expires.toGMTString()+'; path=/':'';
			document.cookie = name+'='+escape(value)+expiretime;
		},
		clr: function(name){ document.cookie = name+"=null; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/"; }
	};
};
