Ext.ns('Ext.ux.state')

Ext.ux.state.LocalStorage = function(config) {
	Ext.ux.state.LocalStorage.superclass.constructor.call(this);
	Ext.apply(this, config);
	// get all items from localStorage
	this.state = this.readLocalStorage();
};

Ext.extend(Ext.ux.state.LocalStorage, Ext.state.Provider, {
	namePrefix: 'ys-',
	
	set: function(name, value) {
		if (typeof value == "undefined" || value === null) {
			this.clear(name);
			return;
		}
		// write to localStorage
		localStorage.setItem(this.namePrefix + name, this.encodeValue(value));
		Ext.ux.state.LocalStorage.superclass.set.call(this, name, value);
	},
	'clear': function(name) {
		localStorage.removeItem(this.namePrefix + name);
		Ext.ux.state.LocalStorage.superclass.clear.call(this, name);
	},
	
	readLocalStorage: function() {
		var data = {};
		for (i = 0; i <= localStorage.length - 1; i++) {
			name = localStorage.key(i);
			if (name && name.substring(0, this.namePrefix.length) == this.namePrefix) {
				data[name.substr(this.namePrefix.length)] = this.decodeValue(localStorage.getItem(name));
			}
		}
		return data;
	}
});

if (window.localStorage) {
	Ext.state.Manager.setProvider(new Ext.ux.state.LocalStorage({namePrefix:'my-'}))
} else {
	var thirtyDays = new Date(new Date().getTime()+(1000*60*60*24*30))
	Ext.state.Manager.setProvider(new Ext.state.CookieProvider({expires: thirtyDays}))
}