/*
 * By Mike Rumble for Five by Five.
 *
 * http://www.mikerumble.co.uk
 * http://www.fivebyfivedigital.com
 *
 * This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License:
 * http://creativecommons.org/licenses/by-sa/3.0/
 *
 */

var UI = window.UI||{};

UI.Accordion = Class.create({
  initialize : function(element, options){
    this.element = $(element);
    if(!this.element){
      return false;
    }
    this.current = null;
    this.options = Object.extend({
      content: '.content',
      trigger: '.trigger',
      duration: 8,
      open: '.open',
      close: true,
      closed: false,
      transition: Effect.Transitions.sinoidal
    }, options||{});
    this.duration = ((11-this.options.duration)*0.15);
    this.triggers = this.element.select(this.options.trigger);
    this.children = this.element.select(this.options.content);
    this.children.each(function(element, i){
      if(element.match(this.options.open) && !this.options.closed){
        this.current = element;
        element.addClassName('open');
        this.current_trigger = this.triggers[i];
        this.current_trigger.toggleClassName('open');
      }else{
        element.setStyle({
          height: 0
        });
      }
    }, this);
    
    
    if(!this.current && !this.options.closed){
      this.current = this.children[0];
      this.children[0].addClassName('open').setStyle({height:'auto'});
      this.current_trigger = this.triggers[0];
      this.current_trigger.toggleClassName('open');
    }
    
    if(this.triggers.size()>1){   
      this.element.observe('click', this.onclick.bindAsEventListener(this));
    }
  },
  onclick : function(e){
    var element = Event.findElement(e, this.options.trigger);
    var i = this.triggers.indexOf(element);
    
    if(element){
      Event.stop(e);
      element.toggleClassName('open');
      var list = this.children[i];
      if(this.current){
        if(list!=this.current){
          this.current_trigger.toggleClassName('open');
          this.accordion(list, this.current);
          this.current = list;
          this.current_trigger = element;
        }else if(this.options.close){
          this.hide(list);
          this.current = false;
          this.current_trigger = false;
        }
      }else{
        this.show(list);
        this.current = list;
        this.current_trigger = element;
      }
    }
  },
  accordion : function(element, current){  
    current.removeClassName('open');
    new Effect.Parallel([
      new Effect.Scale(element, 100, {
        scaleContent: false,
        scaleFrom: 0,
        scaleMode: {
          originalHeight: element.scrollHeight
        },
        scaleX: false,
        transition: this.options.transition
      }),
      new Effect.Scale(current, 0, {
        scaleContent : false,
        scaleX : false,
        transition: this.options.transition
      })
    ], {
      duration: this.duration,
      afterFinish : function(){
        element.addClassName('open');
      }
    });
  },
  show : function(element){
    return new Effect.Scale(element, 100, {
      scaleContent : false,
      scaleX: false, 
      scaleFrom: 0,  
      scaleMode: {
        originalHeight: element.scrollHeight
      },
      duration: this.duration,
      transition: this.options.transition,
      afterFinish : function(){
        element.addClassName('open');
      }   
    });  
  },
  hide : function(element){
    element.removeClassName('open');
    return new Effect.Scale(element, 0, {
      scaleContent : false,
      scaleX: false,  
      duration: this.duration,
      transition: this.options.transition,
      afterFinish : function(){
      }    
    });
  }
});