/**
 *  jail accordion.js v0.0.2, Fri June 05 2008
 *    (c) 2008 Melissa Forrest and is released under the MIT License:
 *    http://www.opensource.org/licenses/mit-license.php
 *
 *  The jail javascript library development team:  
 *    Melissa Forrest (http://www.melforrest.com)
 *    Kevin Heard (http://www.kevinheard.com)
 *
 */

if (typeof Effect == 'undefined')
 throw("accordion.js requires script.aculo.us effects.js");

var Accordion = Class.create();
Accordion.prototype = {
 animating: false,
 current: null,
 showing: null,
 effects: [],

 initialize: function(container, options) {

   if (!$(container)) {
     throw(container+' doesn\'t exist!');
     return false;
   }

   this.container = container;
   this.options = this._set_options(options);
   this.controls = $$("#"+this.container+" "+this.options.controls);
   this.contents = $$("#"+this.container+" "+this.options.contents);

   if(this.controls.length != this.contents.length) {
     throw Error('number of controls and number of contents do not match');
   }

   for(var i = 0; i < this.controls.length; i++) {		
     this.controls[i].style.cursor = "pointer";
     this.contents[i].style.display = "none";
     Event.observe(this.controls[i], this.options.trigger, this.activate.bind(this, i));
   }

   if (this.options.open=="random") {
     this.options.open = Math.floor(Math.random()*this.controls.length)+1;
   }

   $(this.container).addClassName('accordionized');
   this.show(this.options.open-1);

 },

 activate: function(index, e) {
   if (!this.options.allow_collapse_all && this.current==index) return false;
   Event.stop(e);
   this.show(index);
 },

 show: function(index) {

   if ((index >= this.length) || (index < 0) || (this.animating) ||
      (this.options.trigger=='mouseover' && this.current==index)) return false;

   this.showing = index; 
   this.effects = [];

   if (this.current != null) {

     hide_element=this.contents[this.current];

     if(this.options.fade==true) {         
       this.effects.push(
         new Effect.Fade(hide_element),
         new Effect.BlindUp(hide_element)
       );
     } else {
       this.effects.push(
         new Effect.BlindUp(hide_element)
       );
     }

     hide_control=this.controls[this.current];
     hide_control.up(this.options.item_container).removeClassName(this.options.active_class);

   }

   if (this.current != this.showing) { 

     show_element=this.contents[this.showing];

     if(this.options.fade==true) {
       this.effects.push(
         new Effect.Appear(show_element),
         new Effect.BlindDown(show_element)
       );
     } else {
       this.effects.push(
         new Effect.BlindDown(show_element)
       );
     }

     show_control = this.controls[this.showing];
     show_control.up(this.options.item_container).addClassName(this.options.active_class);

     this.current = index;

   } else {

     this.current = null;

   }

   this.showing = null;

   new Effect.Parallel( 

     this.effects, {

     duration: this.options.duration,

     beforeStart: function() {
       this.animating = true;
     }.bind(this),

     afterFinish: function() {
       this.animating = false;
     }.bind(this)

   });

 },

 _option_defaults: {
   duration: 0.3,
   trigger: 'click',
   open: 1,
   fade: false,
   allow_collapse_all: false,
   active_class: 'active',
   controls: '.control',
   contents: '.content',
   item_container: '.container'
 },

 _set_options: function(options) {
   if(typeof options != "undefined") {		
     var results = [];
     for(option in this._option_defaults) {
       if(typeof options[option] == "undefined") {
         results[option] = this._option_defaults[option];
       } else {
         results[option] = options[option];
       }
     }
     return results;
   } else {
     return this._option_defaults;
   }
 }

}
