1 (function (){
  2 	"use strict";
  3 	
  4 	//No support for localstorage? Bail out early
  5 	if(typeof localStorage === "undefined" || typeof JSON === "undefined")
  6 	{
  7 		return;
  8 	}
  9 	
 10 	//Shortcuts for wrapper
 11 	var l = localStorage,
 12 		s = sessionStorage;
 13 
 14 	/**
 15 	 * Wrapper for localstorage / sessionstorage data serialization
 16 	 *
 17 	 * @name store
 18 	 * @namespace
 19 	 * @memberOf $_
 20 	 */
 21 	var store = {		
 22 		/**
 23 		 * Retrieves and deserializes a value from localstorage, 
 24 		 * based on the specified key
 25 		 * 
 26 		 * @param string key
 27 		 * @param bool session
 28 		 * @name get
 29 		 * @memberOf $_.store
 30 		 * @function
 31 		 * @return object
 32 		 * @type object
 33 		 */
 34 		get: function (key, sess)
 35 		{
 36 			var val = (sess) ? s.getItem(key) : l.getItem(key);
 37 		
 38 			return JSON.parse(val);
 39 		},
 40 		/**
 41 		 * Puts a value into localstorage at the specified key,
 42 		 * and JSON-encodes the value if not a string
 43 		 *
 44 		 * @param string key
 45 		 * @param mixed value
 46 		 * @param bool session
 47 		 * @name set
 48 		 * @memberOf $_.store
 49 		 * @function
 50 		 */
 51 		set: function (key, value, sess)
 52 		{
 53 			// Localstorage generally only accepts strings
 54 			value = JSON.stringify(value);			
 55 			
 56 			(sess) ? s.setItem(key, value) : l.setItem(key, value);
 57 		},
 58 		/**
 59 		 * Removes the specified item from storage
 60 		 * 
 61 		 * @param string key
 62 		 * @param bool session
 63 		 * @name remove
 64 		 * @memberOf $_.store
 65 		 * @function
 66 		 */
 67 		remove: function (key, sess)
 68 		{
 69 			(sess) ? s.removeItem(key) : l.removeItem(key);
 70 		},
 71 		/**
 72 		 * Removes all values from the same domain storage
 73 		 *
 74 		 * @param bool session
 75 		 * @name clear
 76 		 * @memberOf $_.store
 77 		 * @function
 78 		 */
 79 		clear: function(sess)
 80 		{
 81 			(sess) ? s.clear() : l.clear();
 82 		},
 83 		/**
 84 		 * Returns an object of all the raw values in storage
 85 		 * 
 86 		 * @name getAll
 87 		 * @member of $_.store
 88 		 * @function
 89 		 * @return object
 90 		 * @type object
 91 		 */
 92 		getAll: function (sess)
 93 		{
 94 			var i,
 95 				len,
 96 				data = {},
 97 				k,
 98 				o;
 99 			
100 			//Reference to session/localstorage
101 			o = (sess) ? l : s;
102 				
103 			len = o.length;
104 
105 			for (i = 0; i < len; i++)
106 			{
107 				k = o.key(i);
108 				data[k] = o.getItem(k);
109 			}
110 
111 			return data;
112 		}
113 	};
114 
115 	$_.ext('store', store);
116 }());