/*
* AJAXCache 1.0
*
* (c) 2007 Jaroslaw Kostrz
* e-mail: jkostrz@gmail.com
* www:  http://ajaxcache.ajaxin.pl/
*       http://ajaxin.pl/
* http://creativecommons.org/licenses/by/2.5/pl/
*/

function AJAXCache() {

	this.garbage = [];
	
	this._getIndexById = function(id) {
		for (var i = this.garbage.length-1; i >= 0; i--) 
		{
			if (this.garbage[i][0] == id)
			return i;
		}
		return -1;
	};
	
	this._getNewItemId = function() {
		return this.garbage.length;
	};
	
	this._add = function(i, id, t, o) {	
		if (o == null)
            o = {};
		if (o.expiration)
		{
			var to = new Date(new Date().getTime()+o.ttl*1000).getTime();
			var x = this;
			setTimeout(function(){x.remove(i)}, o.expiration*1000);
		}
		this.garbage[i] = [id, t, to];
	};
	
	this.save = function(id, t, o) {
		var i = this._getIndexById(id);
		if (i >= 0) {
		  if (this._isExpired(i) || o.overwrite) {
			this._add(i, id, t, o);
		  }  
		}
		else {	
			this._add(this._getNewItemId(), id, t, o);
		}
	};
	
	this.get = function(id) {
		var i = this._getIndexById(id);
		if (i < 0 || this._isExpired(i)) {
			return null;
		}
		else {
			return this.garbage[i][1];
		}
	};

	this.remove = function(i) {
		if (this.garbage[i])
		 this.garbage[i].splice(0, 2);
	};
	
	this.flush = function() {
		this.garbage.splice(0, 2);
	};

	this._isExpired = function(i) {
		var now = new Date().getTime();
		if (!this.garbage[i][2] || (this.garbage[i][2] - now) > 0) {		
			return 0;	
		}
		return 1;
	};
}


/**************************** rozwiazanie konkurencyjen ********************************/


var TCache = function(domain) {
  if(typeof globalStorage != "undefined") {
      // jesli FF
      if(typeof domain == "undefined") {
          var domain = location.host;
      }
      this.storage = globalStorage[domain];
  } else {
      // jesli IE
      if(!document.getElementById('globalStore')) {
          // dynamicznie dodanie warstwy dla cache
          var storageObj = document.createElement('DIV');
          storageObj.id = 'globalStore';
          storageObj.style.behavior = 'url(#default#userdata)';
          storageObj.style.display = 'none';
          document.body.appendChild(storageObj);
      }
      this.storage = globalStore;
  }
}
TCache.prototype = {
  /**
   * Magazyn danych
   *
   * @var object
   */
  storage : null,
  /**
   * Funkcja zapisuje cache
   *
   * @param string name   Nazwa zmiennej cache 
   * @param mixed data    Dane do zapisania
   * @param int expires   Okres waznosci danych (w sek., domyslnie bez limitu)
   * @return bool
   */
  save : function(name, data, expires) {
      if(typeof globalStorage != "undefined") {
          // jesli FF
          this.storage.setItem(name, data);
          if(typeof expires != "undefined") {
              var _this = this;
              setTimeout(function(){this.remove(name)}, expires*1000);
          }
          return true;
      } else if(typeof this.storage.XMLDocument != "undefined") {
          // jesli IE
          if(typeof expires != "undefined") {
              var oTimeNow = new Date();
              oTimeNow.setSeconds(oTimeNow.getSeconds() + expires);
              this.storage.expires = oTimeNow.toUTCString();
          }
          this.storage.setAttribute(name, data);
          this.storage.save('cache');
          return true;
      } else {
          return false;
      }
  },
  /**
   * Funkcja pobiera dane z cache
   *
   * @param string name   Nazwa zmiennej cache
   * @return mixed
   */
  get : function(name) {
      if(typeof globalStorage != "undefined") {
          // jesli FF
          if(this.storage.getItem(name)) {
              var result = this.storage.getItem(name).value;
          }
      } else if(typeof this.storage.XMLDocument != "undefined") {
          // jesli IE
          this.storage.load('cache');
          var result = this.storage.getAttribute(name);
      }
      return (result) ? result : null;
  },
  /**
   * Funkcja usuwa zmienna z cache
   *
   * @param string name   Nazwa zmiennej
   */
  remove : function(name) {
      if (typeof globalStorage != "undefined") {
          // jesli FF
          this.storage.removeItem(name);
          return true;
      } else if(typeof this.storage.XMLDocument != "undefined") {
          // jesli IE
          this.storage.load('cache');
          this.storage.setAttribute(name, '');
          this.storage.save('cache');
          return true;
      } else {
          return false;
      }
  }
}