/* Manage concurrent togglable panels.
 * ----------------------------------------------------------------------------
 *
 * Copyright (C) 2009-2010 Raphaël Bois.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------------
 * toggle-contents.js
 */

if (!MooTools || MooTools.version != '1.2.4') {
  throw "This module requires MooTools 1.2.4 !";
}

/* Group object. */
var ToggleGroup = new Class({
  initialize: function (name) {
    this.name = name;
    this.elements = {};
    this.callback = null;
    this.isActive = false;
    this.allowHideOthers = true;
  },
  addElement: function (id) {
    this.elements[id] = $(id); /* document.getElementById(id); */
  },
  removeElement: function (id) {
    delete this.elements[id];
  },
  setActive: function (active) {
    for (id in this.elements) {
      if (active) {
        this.elements[id].setStyle('display', "block");
        this.elements[id].setStyle('visibility', "visible");
      } else {
        this.elements[id].setStyle('display', "none");
        this.elements[id].setStyle('visibility', "hidden");
        /* this.elements[id].addClass("hide"); */
      }
    }
    this.isActive = active;
    if (this.callback) {
      this.callback(this);
    }
  },
  show: function () {
    this.setActive(true);
  },
  hide: function () {
    this.setActive(false);
  },
  toggle: function () {
    this.setActive(!this.isActive);
    return this.isActive;
  }
});

var Toggle = new Class({
  initialize: function () {
    this.callback = null;
    this.groups = {};
    this.addGroup = function (grp) {
      this.groups[grp.name] = grp;
      return grp;
    }
  },
  createGroup: function (name) {
    return this.addGroup(new ToggleGroup(name));
  },
  getGroup: function (name) {
    return this.groups[name];
  },
  deleteGroup: function (name) {
    delete this.groups[name];
  },
  /* Helper to add a "one element" group. */
  addSingle: function (id) {
    g = this.createGroup(id);
    g.addElement(id);
    return g;
  },
  hasGroup: function (name) {
    return !(!this.groups[name]);
  },
  toggleHideOthers: function (grpName) {
    for (g in this.groups) {
      if (g != grpName) {
        gr = this.groups[g];
        if (gr.allowHideOthers) {
          gr.hide();
        }
      }
    }
  },
  show: function (grpName) {
    this.groups[grpName].show();
    this.toggleHideOthers(grpName);
    if (this.callback) {
      this.callback(true, this);
    }
  },
  hide: function (grpName) {
    this.groups[grpName].hide();
    this.toggleHideOthers(grpName);
    if (this.callback) {
      this.callback(false, this);
    }
  },
  hideAll: function () {
    this.toggleHideOthers("");
    if (this.callback) {
      this.callback(false, this);
    }
  },
  toggle: function (grpName) {
    ret = this.groups[grpName].toggle();
    this.toggleHideOthers(grpName);
    if (this.callback) {
      this.callback(ret, this);
    }
  }
});

