/*
Accordion is freely distributable under the terms of an MIT-style license.

The MIT License

Copyright (c) 2007 Mats Lindblad <mats.lindblad [AT] it.su.se>

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.

  Inspiration: accordion.js found at script.aculo.us, find it yourself!
*/
var Accordion = Class.create();
Accordion.prototype = {
  initialize: function(container, options){
    this.container = $(container);
    this.setOptions(options);
    this.togglers = this.container.select(this.options.toggler);
    this.togglees = this.container.select(this.options.togglee);
    this.togglees.invoke("hide");
    this.togglers.each(function(t, idx){
      t.observe('click', this.show.bindAsEventListener(this, idx));
    }.bind(this));
    this.setCurrent();
  },
  show: function(event, index) {
    var element = Event.element(event);
    if (this.open.toggler == element) return;
    var current = this.open;
    var next = {toggler: element, togglee: this.togglees[index]};
    new Effect.Parallel([
      new Effect.BlindUp(current.togglee, {transition:Effect.Transitions.exponential, fps:90}),
      new Effect.BlindDown(next.togglee,  {transition:Effect.Transitions.exponential, fps:90})
    ], {duration: 0.5});
    current.toggler.removeClassName(this.options.activeClassName);
    next.toggler.addClassName(this.options.activeClassName);
    this.open = next;
  },
  setOptions: function(options){
    this.options = {
      toggler:          '.toggler', // className of the element you click
      togglee:          '.togglee', // className of the element you toggle
      activeClassName:  'visible', // className to put on the open elements
      defaultTogglee:   null
    };
    Object.extend(this.options, options || {});
  },
  setCurrent: function(){
    if (this.options.defaultTogglee != null) {
      this.togglees.each(function(t, idx){
        if (t.id == this.options.defaultTogglee) {
          t.show();
          this.open = {toggler: this.togglers[idx], togglee: this.togglees[idx]};
          this.togglers[idx].addClassName(this.options.activeClassName);
          return;
        }
      }.bind(this));
    }
    else {
      this.open = {toggler: this.togglers.first(), togglee: this.togglees.first()};
      this.togglers.first().addClassName(this.options.activeClassName);
      this.togglees.first().show();
    }
  }
};

/*
This is an extension to Effects, found at http://wiki.script.aculo.us/scriptaculous/show/EffectsTreasureChest
I thought it could be an aternative to using Effect.Parallel
*/
if (typeof Effect == "object") {

  Effect.PhaseIn = function(element) {
    element = $(element);
    new Effect.Parallel([
      new Effect.BlindDown(element, arguments[1] || {}),
      new Effect.Appear(element, arguments[2] || arguments[1] || {})
    ]);
  }

  Effect.PhaseOut = function(element) {
    element = $(element);
    new Effect.Parallel([
      new Effect.Fade(element, arguments[1] || {}),
      new Effect.BlindUp(element, arguments[2] || arguments[1] || {})
    ]);
  }

  Effect.Phase = function(element) {
    element = $(element);
    if (element.style.display == 'none')
      new Effect.PhaseIn(element, arguments[1] || {}, arguments[2] || arguments[1] || {});
    else
      new Effect.PhaseOut(element, arguments[1] || {}, arguments[2] || arguments[1] || {});
  }

  Effect.Transitions.exponential = function(pos) {
    return 1-Math.pow(1-pos,2);
  }

}
