///<reference path="../TESCO.js" />
///<reference path="cookie.js" />
/*
	Example:
	
	var cache = new TESCO.system.Cache();
		cache.add("myId", {});
		cache.add("myOtherId", document.createElement("div"));
		
		document.body.appendChild(cache.get("myOtherId"));
		cache.removeItem("myOtherId");
*/

TESCO.system.Cache = (function() {

    //  constructor
    function _constructor() {
        /// <summary>storage mechanism for any type</summary>
        /// <returns>instance</returns>
        this.cache = {}    
        return this;
    }

    _constructor.prototype = {
        "NAME" : "TESCO.system.Cache",
        "setItem" : function(key, value) {
            this.cache[key] = value;
        },
        "removeItem" : function(key) {
            this.cache[key] = null;
        },
        "exists" : function(key) {
            return !!this.getItem(key);
        },
        "getItem" : function(key) {
            return this.cache[key];
        },
        "clear" : function() {
			this.cache = {}
        }
    }

    //  return _constructor as function pointer
    return _constructor;
})();

/*
	Example:
	
	TESCO.system.event.document.addEventListener("load",
        function() {
            var _ul2 = document.getElementById("ul2");
            var _cache = new TESCO.system.Cache.Session();
            
            function _set(display) {
                _ul2.style.display = display;
                _cache.setItem(_ul2.id, display);
            }
            
            function _toggle(display) {
                _set((display === "block") ? "none" : "block");
            } 
            _set(_cache.getItem(_ul2.id) || TESCO.system.DOM.node.getStyle(_ul2, "display")); 
            
            document.getElementById("li1").onclick = function() {
                _toggle(_ul2.style.display);
                return false;
            }
        }
    );
*/

TESCO.system.Cache.Session = (function() {

    var _html5 = !!window.sessionStorage;
    
    function _extractKeyFromString(id, str) {
        return str.slice(id.length, str.length);
    }

    //  constructor
    function _constructor(id, useCookie) {
        /// <summary>session storage for strings</summary>
        /// <param name="useCookie">use a cookie if browser does not support sessionStorage (optional)</param>
        /// <returns>instance</instance>
        _constructor.base.constructor.call(this);
        this.id = id;
        this.useCookie = useCookie && !_html5;
        this.useBase = false;
        if (_html5) {
            this.cache = sessionStorage;
        } else if (this.useCookie) {
			//  use TESCO.system.Cookie for browsers without sessionStorage where the value must be persisted
			var _cookie = new TESCO.system.Cookie(id, null, TESCO.sites.Configuration.application.domain.cookie);
			//  alias cookie methods
			_cookie.getItem = function(str) {
			    return _cookie.getValueByName(_extractKeyFromString(id, str));
			}
			_cookie.setItem = function(str, value) {
			    _cookie.setValueByName(_extractKeyFromString(id, str), value);
			}
			_cookie.removeItem = function(str) {
			    _cookie.removeValueByName(_extractKeyFromString(id, str));
			}
			this.cache = _cookie;
        } else {
            this.useBase = true;
        }
        
        return this;
    }
    _constructor.extend(TESCO.system.Cache);

    _constructor.prototype.NAME = "TESCO.system.Cache.Session";

    _constructor.prototype.setItem = function(key, value) {
        this.useBase ? _constructor.base.setItem.call(this, key, value) : this.cache.setItem(this.id + key, value);
    }

    _constructor.prototype.removeItem = function(key) {
        this.useBase ? _constructor.base.removeItem.call(this, key) : this.cache.removeItem(this.id + key);
    }

    _constructor.prototype.getItem = function(key) {
        return this.useBase ? _constructor.base.getItem.call(this, key) : this.cache.getItem(this.id + key);
	}
	
	_constructor.prototype.clear = function() {
	    if (_html5) {
			for (var i = 0; i < sessionStorage.length; i++) {
				var _key = sessionStorage.key(i);
				if (_key.startsWith(this.id)) {
					this.removeItem(_extractKeyFromString(this.id, _key));
					i--;
				}
			}
		} else if (this.useBase) {
			_constructor.base.clear.call(this);
		} else if (this.useCookie) {
			this.cache.setAllValues("");
		}
    }

    //  return _constructor as pointer
    return _constructor;
})();