var storage = {
	
	domain: '',	// Cookie domain (defaults to current domain)
	path: '/',	// cookie path (default root directory)
	expiry: 30,	// Cookie Expiry (30 days default)
	
	data: [],	// Array that holds all the stored data

	// Add data to storage
	add: function(key,data)
	{
		storage.data[key] = data;
	},
	
	// remove data from storage
	remove: function(key)
	{
		var index = 0;
		var bFound = false;
		for(x in storage.data)
		{
			if(String(x) == String(key))
			{
				bFound = true;
				break;
			}
			index ++;
		}
		
		if(bFound)
		{
			storage.data.splice(index,1);
			return true;
		}
		else
			return false;
	},
	
	// Fetch data from storage
	get: function(key)
	{
		try
		{
			return storage.data[key];
		}
		catch(e){
			return null;
		}
	},
	
	// save the stored data
	commit: function(name)
	{
		var output = '';
		
		for(x in storage.data)
			output += String(x) + '=>'+String(storage.data[x])+'|';
		
		output = output.substr(0,Number(output.length) - 1);
		
		storage.setCookie( name, output, storage.expiry, storage.path,storage.domain);
	},
	
	// load the stored data
	load: function(name)
	{
		storage.data = [];
		var input = storage.getCookie(name);
		
		if(input == null)
			return input;
		
		input = input.split('|');
		
		for(i = 0; i < input.length; i++)
		{
			var temp = input[i].split('=>');
			storage.data[temp[0]] = temp[1];
		}
	},
	
	// deletes the storage from cache
	unload: function(name)
	{
		storage.deleteCookie(name,storage.path,storage.domain);
	},
	
	////////////////////////////
	// Cookie helper functions//
	////////////////////////////
	getCookie: function ( name ) 
	{
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ';', len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	},

	setCookie: function ( name, value, expires, path, domain, secure ) 
	{
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires ) {
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+'='+escape( value ) +
			( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
			( ( path ) ? ';path=' + path : '' ) +
			( ( domain ) ? ';domain=' + domain : '' ) +
			( ( secure ) ? ';secure' : '' );
	},

	deleteCookie: function ( name, path, domain ) 
	{
		if ( storage.getCookie( name ) ) document.cookie = name + '=' +
				( ( path ) ? ';path=' + path : '') +
				( ( domain ) ? ';domain=' + domain : '' ) +
				';expires=Thu, 01-Jan-1970 00:00:01 GMT';
	}
};

