
/* **********************************************************************
 * ExpanderControl
 * 
 * Implements an expander control (open/close panel)  
 * **********************************************************************/
function ExpanderControl(id, obs) {

  /* Attributes */
  this.container = $(id);  // DIV id to be inserted in

  /* The control observer */
  this.observer = obs; 

  /* **********************************************************************
   * PUBLIC FUNCTIONS
   * **********************************************************************    
   */

  /*!
   *\fn render
   *\brief Renders the controls
   */
  this.render = function(firstsel) {
    /* Parse the file and create the expanders */
    var container = this.container;
    var panelcount = 0;
    var divs = container.getElementsByTagName("div");
    for(var index = 0; index < divs.length; index++) {
      var panel = divs[index];
      if(panel.className == "expander_panel") {
        /* Set the panel id */
        panel.setAttribute('id', 'expander_panel' + panelcount);

        /* Set the panel title id & onclick function */
        var elts = panel.getElementsByTagName("div");
        for(var index2 = 0; index2 < elts.length; index2++) {
          var elt = elts[index2];
          switch(elt.className) {
          case "expander_title":
            elt.setAttribute('id', elt.className + panelcount);
            break
          case "expander_title_icon":
          case "expander_title_text":
            elt.setAttribute('id', elt.className + panelcount);
            $(elt.className + panelcount).observe('click', ( function(event) { if(event.target != undefined) { this.onClickTitle(event.target.id); } else { this.onClickTitle(event.srcElement.id); } } ).bind(this));
            break;
          case "expander_content":
            elt.setAttribute('id', 'expander_content' + panelcount);
            if(firstsel == panelcount) {
              $('expander_title_text' + panelcount).className = 'expander_title_text_s';
              $('expander_title_icon' + panelcount).className = 'expander_title_icon_s';
              elt.style.display = '';
            } else {
              $('expander_title_text' + panelcount).className = 'expander_title_text';
              $('expander_title_icon' + panelcount).className = 'expander_title_icon';
              elt.style.display = 'none';
            }
            break;
          }
        }
        /* Next expander title/content */
        panelcount++;
      } // NO ELSE: Not 'expander_panel'
    } // END FOR
  } 

  /* isOpen: Returns true if the panel is open */  
  this.isOpen = function(id) {
    var ct = this._getContentObject(id);
    return (ct.style.display == '');
  }
  
  /* open: Opens a closed panel */
  this.open = function(id) {
    if(!this.isOpen(id)) {
      var ct = this._getContentObject(id);
      //new Effect.SlideDown(ct, {duration: 0.1});
      ct.style.display = '';
      $('expander_title_text' + id).className = 'expander_title_text_s';
      $('expander_title_icon' + id).className = 'expander_title_icon_s';
    } 
  }
  
  /* close: Closes a open panel */
  this.close = function(id) {
    if(this.isOpen(id)) {
      var ct = this._getContentObject(id);
      //new Effect.SlideUp(ct, {duration: 0.1});
      ct.style.display = 'none';
      $('expander_title_text' + id).className = 'expander_title_text';
      $('expander_title_icon' + id).className = 'expander_title_icon';
    } 
  }
  
  /* onClickTitle: Forward the event to the observer */
  this.onClickTitle = function(id) {
    if(id.search("expander_title_text") >= 0) {
      id = id.replace("expander_title_text", "");
    } else {
      id = id.replace("expander_title_icon", "");
    }
    this.observer.onChangeSel(this, id);
  }
  
  /* **********************************************************************
   * PRIVATE FUNCTIONS
   * **********************************************************************    
   */
  
  /* _getContentObject: Returns the content object associated with the panel */  
  this._getContentObject = function(id) {
    return $('expander_content' + id);
  }

}

