/*
 * Helper object
 * Allows the application to be bookmarked in a specific state.
 * Changes the hash portion of the document's location.
 */
 
var bookmarkable =  {

        "storeState" : function(key, value){
            var oState = this.getState();

            // set new value for key
            oState[key] = value;

            // produce uri fragment with the key-value pairs
            var keyPairs = [];
            for(var keyName in oState){
                if (keyName.length != 0){
                    var keyPair = keyName +"="+ escape(oState[keyName]);
                    keyPairs.push(keyPair);
                }
            }

            window.location.hash = keyPairs.join("&");
        },

        "getState" : function(key){

            var colKeys = {};
            var hash = window.location.hash.substr(1);
            var keys = hash.split("&");
            for(var i=0; i < keys.length; i++){
                var keyName = keys[i].split('=')[0];
                var keyValue = unescape(keys[i].split('=')[1]);
                colKeys[keyName] = keyValue;
            }

            if (typeof key == 'undefined'){
                return colKeys;
            }else{
                if (typeof colKeys[key] != 'undefined'){
                    return colKeys[key];
                }else{
                    return null;
                }
            }
        }

};
