// Element.Storage API
// each element contains an ID attribute, which is the key of the Element.Storage hash

// storing separate pseudo-attributes on each element can cause memory leaks in IE,
// but by only holding the element's ID with the element, and then a hash of all the
// element's pseudo-attributes with the ID as a key we can reduce the memory leaks
// and only have one unique point of access for all elements, even if they have been
// removed from the document.
var Storage = {
	create: function (element) {
		if (typeof window.Element.Storage === 'undefined') {
			Element.Storage = new Hash();
		}
		
		element = $(element);
		var storage = Element.Storage,
		id = element.identify();

		if (!storage.keys().include(id)) {
			storage.set(id, new Hash());
		}

		return element;
	}
};

Element.addMethods({
	store: function (element, rules, value) {
		element = Storage.create(element);
		var storage = Element.Storage.get(element.identify()),
		attributes = { };
		
		if (typeof rules === 'object') {
			attributes = rules;
		} else {
			attributes[rules] = value;
		}
		
		for (var key in attributes) {
			if (attributes.hasOwnProperty(key) && typeof attributes[key] !== 'undefined') {
				storage.set(key, attributes[key]);
			}
		}
	
		return element;
	},

	retrieve: function (element, property_name, property_default_value) {
		element = Storage.create(element);
		var storage = Element.Storage.get(element.identify());
	
		if (!storage.keys().include(property_name)) {
			element.store(property_name, property_default_value);
		}
	
		return storage.get(property_name);
	},

	eliminate: function (element, property_name) {
		element = Storage.create(element);
		var storage = Element.Storage.get(element.identify());
	
		if (storage.keys().include(property_name)) {
			storage.unset(property_name);
		}
	
		return element;
	}
});