/* Simple Accordion Script 
 * Requires Prototype and Script.aculo.us Libraries
 * By: Brian Crescimanno <brian.crescimanno@gmail.com>
 * http://briancrescimanno.com
 * This work is licensed under the Creative Commons Attribution-Share Alike 3.0
 * http://creativecommons.org/licenses/by-sa/3.0/us/
 */

if (typeof Effect == 'undefined')
  throw("You must have the script.aculo.us library to use this accordion");

var Accordion = Class.create({
    initialize: function(id, defaultExpandedCount) {
        if(!$(id)) throw("Attempted to initalize accordion with id: "+ id + " which was not found.");
        this.accordion = $(id);
        this.options = {
            toggleClass: "accordion_un-toggle",
            toggleActive: "accordion_un-toggle-active",
            contentClass: "accordion_un-content"
        }
        this.contents = this.accordion.select('div.'+this.options.contentClass);
        this.isAnimating = false;
        this.maxHeight = 0;
        this.current = defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];
        this.toExpand = null;

        this.checkMaxHeight();
        this.initialHide();
        this.attachInitialMaxHeight();

        var clickHandler =  this.clickHandler.bindAsEventListener(this);
        this.accordion.observe('click', clickHandler);

	 
    },

    expand: function(el) {
        this.toExpand = el.next('div.'+this.options.contentClass);
        if(this.current != this.toExpand){
		this.toExpand.show();
            this.animate();
        }
    },

    checkMaxHeight: function() {
        for(var i=0; i<this.contents.length; i++) {
            if(this.contents[i].getHeight() > this.maxHeight) {
                this.maxHeight = this.contents[i].getHeight();
            }
        }
    },

    attachInitialMaxHeight: function() {
		this.current.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);
		if(this.current.getHeight() != this.maxHeight){
					currentHeight = this.current.getHeight();
					this.current.setStyle({height: currentHeight+"px"});
	  		} 

    },

    clickHandler: function(e) {
        //var el = e.element();
	  var el = e.findElement("div");
	  //alert(el.className + ', '+ e.findElement("div").className);
       		
	  if(el.hasClassName(this.options.toggleClass) && !this.isAnimating) {
        	this.expand(el);
        }
    },

    initialHide: function(){
        for(var i=0; i<this.contents.length; i++){
            if(this.contents[i] != this.current) {
							initHeight = this.contents[i].getHeight();
							this.contents[i].setStyle({height: initHeight});
							this.contents[i].hide();
              this.contents[i].previous('div').setStyle({background: 'url('+arrow_down+') no-repeat'});
          	}
						else{
							this.contents[i].previous('div').setStyle({background: 'url('+arrow_up+') no-repeat'});
						}
        }
    },

    animate: function() {
        var effects = new Array();
        var myDuration = 0;


        new Effect.Parallel(effects, {
            duration: myDuration,
            fps: 35,
            queue: {
                position: 'end',
                scope: 'accordion_un'
            },
            beforeStart: function() {
                this.isAnimating = true;
		    this.current.previous('div.'+this.options.toggleClass).removeClassName(this.options.toggleActive).setStyle({background:'url('+arrow_down+') no-repeat'});
		    this.toExpand.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive).setStyle({background: 'url('+arrow_up+') no-repeat'});
	      }.bind(this),
            afterFinish: function() {
                this.current.hide();
                expandHeight = this.toExpand.getHeight();
                this.toExpand.setStyle({ height: expandHeight+"px" });
                this.current = this.toExpand;
                this.isAnimating = false;
            }.bind(this)
        });
    }

});

document.observe("dom:loaded", function(){
   var accordion = new Accordion("accordion_un", 1);
})