// Gaia Ajax Widgets Copyright (C) 2007  Frost Innovation AS. details at http://ajaxwidgets.com/

/* 
 * Gaia Ajax Widgets, an Ajax Widget Library
 * Copyright (C) 2007  Frost Innovation AS
 * All rights reserved.
 * This program is distributed under either GPL version 2 
 * as published by the Free Software Foundation or the
 * Gaia Commercial License version 1 as published by
 * Frost Innovation AS
 * read the details at http://ajaxwidgets.com/
 */




/* ---------------------------------------------------------------------------
   Class basically wrapping the ASP.Accordion WebControl class
   --------------------------------------------------------------------------- */
Gaia.Accordion = function(element, options){
  this.initialize(element, options);
}

Gaia.Accordion._objects = new Array();

// Inheriting from WebControl
Object.extend(Gaia.Accordion.prototype, Gaia.WebControl.prototype);

// Inheriting from Container
Object.extend(Gaia.Accordion.prototype, Gaia.Container.prototype);

Object.extend(Gaia.Accordion.prototype, {

  // "Constructor"
  initialize: function(element, options){
    // Calling base class constructor
    this.baseInitializeWebControl(element, options);

    // Caching this one for later usage...
    this.realParentElId = this.element.parentNode.parentNode.id;
    this.originalParentToParentElement = this.element.parentNode.parentNode;

    // Checking to see if containers are built...!
    if( !Gaia.Accordion._objects[this.realParentElId] ){
      this.initializeContainers();
    }
    Gaia.Accordion._objects[this.realParentElId].controls.push(this);

    this.setActive(this.options.active);
    this.getContentWrapper().style.height = this.options.height+'px';
    this._onClicked = this.clicked.bind(this);
    Element.observe(this.getHeader(), 'click', this._onClicked);

    // Checking to see if there's no css class given and if not creating some "default" colors...
    if( !this.options.className || this.options.className == '' ){
      this.getHeader().style.backgroundColor = 'Red';
    }
  },

  clicked: function(){
    if( this.options.active )
      return;
    var toCloseElements = new Array();
    var toCloseObject = new Array();
    Gaia.Accordion._objects[this.realParentElId].controls.each(function(idx){
      if( idx.options.active ){
        idx.options.active = false;
        toCloseElements.push(idx.getContentWrapper());
        toCloseObject.push(idx);
      }
    }.bind(this));
    this.options.active = true;
    this.getContentWrapper().style.height = '0px';
    this.setActive(true);
    this.options.active = true;
    new Effect.ReSize(this.getContentWrapper(), {
      toSize: this.options.height, 
      direction:'vert', 
      duration: this.options.animationDuration,
      otherEl:toCloseElements, 
      afterFinish: function(){
        toCloseObject.each(function(idx){
          idx.setActive(false);
        });
      },
      otherElToSize:0});
  },

  setAnimationDuration: function(value){
    this.options.animationDuration = value;
    return this;
  },

  setActive: function(value){
    if( value )
      Element.show(this.getContentWrapper());
    else
      Element.hide(this.getContentWrapper());
  },

  getContentWrapper: function(){
    if( !this.contentDiv ){
      this.contentDiv = $(this.element.id.substr(0,this.element.id.length-4)+'_DIV');
    }
    return this.contentDiv;
  },

  getHeader: function(){
    if( !this.headerDiv ){
      this.headerDiv = $(this.element.id.substr(0,this.element.id.length-4)+'_DIV_CAPTION');
    }
    return this.headerDiv;
  },

  initializeContainers: function(){
    if( !this.realParentElId || this.realParentElId == '' ){
      this.element.parentNode.parentNode.id = this.element.id + '___AccordionParentContainer';
      this.realParentElId = this.element.parentNode.parentNode.id;
    }
    Gaia.Accordion._objects[this.realParentElId] = {
      controls:new Array()
    };
  },

  // Sets the tabindex of the button
  setTabIndex: function(value){
    this.element.tabIndex = value;
    return this;
  },

  setVisible: function(value){
    if( value != true ){
      // Making sure control and child controls is destroyed...
      this.destroy();
    }
    return this;
  },

  destroy: function(){

    // Destroying all CHILDREN controls
    this.destroyChildrenControls();

    // Stopping observers...
    Element.stopObserving(this.headerDiv, 'click', this._onClicked);

    // Dispatching to container DTOR
    this.destroyContainer(this.element.id);

    var idxNo = 0;
    Gaia.Accordion._objects[this.realParentElId].controls.each(function(idx){
      if( idx == this ){
        throw $break;
      }
      idxNo += 1;
    }.bind(this));
    Gaia.Accordion._objects[this.realParentElId].controls.splice(idxNo, 1);

    // Calling Object.destroy implementation...
    this._destroyImpl();
  },
  
  _getElementPostValue: function(){
    return '';
  }
});

Gaia.Accordion.browserFinishedLoading = true;
