/* 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/
 */


function next(elem) {
    do {
        elem = elem.nextSibling;
    } while (elem && elem.nodeType != 1);
    return elem;                
}



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-toggle",
            toggleActive: "accordion-toggle-active",
            contentClass: "accordion-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];
        if(this.accordion.select('div.'+this.options.toggleActive)!='')
		{
			try
			  {
				this.current= next(this.accordion.select('div.'+this.options.toggleActive)[0]);
			  }
			catch(err)
			  {
				this.current = defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];
			  }

			if(this.current==null)
			{
				this.current = defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];
			}

		}
		else
		{
			this.current = defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];
		}
		//this.current = defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];
        this.toExpand = null;

        this.checkMaxHeight();
        this.initialHide();
        this.initialArow();
        //this.attachInitialMaxHeight();
        this.current.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);
        var clickHandler =  this.clickHandler.bindAsEventListener(this);
        this.accordion.observe('click', clickHandler);
    },

    expand: function(el) {
if((el.next()!=null)&&(el.next().className.indexOf(this.options.contentClass)>=0))

{
     this.toExpand = el.next();
       
		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) this.current.setStyle({height: this.maxHeight+"px"});
    },

    clickHandler: function(e) {
        var el = e.element();
        if(el.hasClassName(this.options.toggleClass) && !this.isAnimating) {
            this.expand(el);
        }
    },

    initialHide: function(){
        for(var i=0; i<this.contents.length; i++){
            this.contents[i].writeAttribute('height', this.contents[i].getHeight());
		if(this.contents[i] != this.current) {
		this.contents[i].hide();
		this.contents[i].setStyle({height: 0});
			

            }
        }
    },
	initialArow: function(){
		this.headings = this.accordion.select('div.'+this.options.toggleClass);
	        for(var i=0; i<this.headings.length; i++){
		        if((this.headings[i].next()==null)||(this.headings[i].next().className.indexOf(this.options.contentClass)<0))
			{
				//this.headings[i].setStyle({background-image:'none'});
				this.headings[i].style.backgroundImage= 'none';
			}
	        }
        
    },

    animate: function() {
        var effects = new Array();

		if(this.toExpand.previous('div.'+this.options.toggleClass).className.indexOf(this.options.toggleActive)<0){
		var options = {
		sync: true,
		scaleFrom: 0,
		scaleContent: false,
		transition: Effect.Transitions.sinoidal,
		scaleMode: {
		originalHeight: this.toExpand.readAttribute('height'),
		originalWidth: this.accordion.getWidth()
		},
		scaleX: false,
		scaleY: true
		};
//effects=null;
		effects.push(new Effect.Scale(this.toExpand, 100, options));
	}
	else{
		 options = {
		    sync: true,
		    scaleContent: false,
		    transition: Effect.Transitions.sinoidal,
		    scaleX: false,
		    scaleY: true
		};
	//effects=null;
		effects.push(new Effect.Scale(this.toExpand, 0, options));


	}

        var myDuration = 0.75;

        new Effect.Parallel(effects, {
            duration: myDuration,
            fps: 35,
            queue: {
                position: 'end',
                scope: 'accordion'
            },
            beforeStart: function() {
               this.isAnimating = true;
                //this.current.previous('div.'+this.options.toggleClass).removeClassName(this.options.toggleActive);

		if(this.toExpand.previous('div.'+this.options.toggleClass).className.indexOf(this.options.toggleActive)<=0){
                	this.toExpand.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);
        	}
		else{
			this.toExpand.previous('div.'+this.options.toggleClass).removeClassName(this.options.toggleActive);
		}

            }.bind(this),
            afterFinish: function() {

		if(this.toExpand.previous('div.'+this.options.toggleClass).className.indexOf(this.options.toggleActive)<=0){
		this.toExpand.hide();
		}

                //this.current.hide();
				//this.toExpand.setStyle({ height: this.maxHeight+"px" });
                this.toExpand.setStyle({ height: this.toExpand.readAttribute('height')+"px" });
                //this.current = this.toExpand;
                this.isAnimating = false;

            }.bind(this)
        });
    }

});

document.observe("dom:loaded", function(){
    accordion = new Accordion("test-accordion", 1);
})

