var Checklist = {
  name:     'ads',
  singular: 'ad',
  plural:   'ads',


  // {{{ toString()
  toString: function() {
    if(!this.usable())
      return 'no ' + this.plural;

    var c = Cookie.get(this.name);

    // Check if already added.
    if(c) {
      var adCount = c.split(/}{/).length;
      return (adCount || 'no') + ' '
           + (adCount == 1 ? this.singular : this.plural);
    }

    return 'no ' + this.plural;
  }, // }}}

  // {{{ usable()
  usable: function() {
    if(typeof(Prototype) == 'undefined') {
      alert('Error: prototype.js must be loaded');
      return 0;
    }

    if(typeof(Cookie) == 'undefined') {
      alert('Error: cookie.js must be loaded');
      return 0;
    }

    return 1;
  }, // }}}

  // {{{ add(section, adno)
  add: function(section, adno) {
    if(!this.usable()) return;

    var c = Cookie.get(this.name);

    // Check if already added.
    if(c) {
      var ads = c.substr(1,c.length-2).split(/}{/);
      for(var i=0; i<ads.length; i++) {
        var x = ads[i].split(/\|/);
        if(x[0] == section && x[1] == adno)
          return 0;
      }

      c += '{' + section + '|' + adno + '}';
    } else {
      c = '{' + section + '|' + adno + '}';
    }

    if(c) {
      Cookie.set(this.name, c, undefined, '/');
      return 1;
    }
  }, // }}}

  // {{{ del(section, adno)
  del: function(section, adno) {
    if(!this.usable()) return

    var c = Cookie.get(this.name);
    if(!c) return 0;

    var pat = new RegExp('\\{' + section + '\\|' + adno + '\\}', 'i');
    var ads = c.replace(pat, '');

    if(ads == c)
      return 0;

    Cookie.set(this.name, ads, undefined, '/');
    return 1;
  }, // }}}

  // {{{ set([data])
  set: function(data) {
    if(!this.usable()) return

    var ads = '';
    data.each(function(sect, adno) {
      ads += '{' + sect + '|' + adno + '}';
    });

    Cookie.set(this.name, ads, undefined, '/');
    return 1;
  }, // }}}

  // {{{ clear()
  clear: function() {
    if(!this.usable()) return
    Cookie.erase(this.name);
  } // }}}

};
