/*
simple cookie library
usage is something like:

Cookie.set('foo', 'bar');
alert(Cookie.get('foo'));
*/

var Cookie = Class.create();

Cookie.name = "pic_downloader_data";
Cookie.data = {};

Cookie.set = function(key, value) {
  Cookie.getData();
  Cookie.data[key] = value;
  Cookie.saveData();
}

Cookie.get = function(key) {
  Cookie.getData();
  return Cookie.data[key]
}

Cookie.remove = function(key) {
  Cookie.getData();
  delete Cookie.data[key]
  Cookie.saveData();
}

Cookie.getData = function() {
  if($H(Cookie.data).keys.length != 0) return;
  
  var start = document.cookie.indexOf(Cookie.name + "=");

  if(start == -1)
    return null;

  if(Cookie.name != document.cookie.substr(start, Cookie.name.length))
      return null;

  var len = start + Cookie.name.length + 1;
  var end = document.cookie.indexOf(';', len);

  if(end == -1) {
      end = document.cookie.length;
  } 
  var dataAsString = myunescape(document.cookie.substring(len, end));
  if(dataAsString)
    Cookie.data = dataAsString.evalJSON();
}

Cookie.saveData = function() {
  document.cookie = Cookie.name + '=' + escape(Object.toJSON(Cookie.data)) + ';path=/;';
}

/*******************************************************************************

"myunescape ()", by Charlton Rose

Permission is granted to use and modifiy this script for any purpose,
provided that this credit header is retained, unmodified, in the script.

*******************************************************************************/


// This function is included to overcome a bug in Netscape's implementation
// of the escape () function:

function myunescape (str)
{
  str = "" + str;
  while (true)
  {
    var i = str . indexOf ('+');
    if (i < 0)
    break;
    str = str . substring (0, i) + '%20' +
    str . substring (i + 1, str . length);
  }
  return unescape (str);
}