/* 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/
 */
 
document.observe("dom:loaded", function(){

if(document.getElementById('narrow-by-list')){

if (typeof Effect == 'undefined')
  throw("You must have the script.aculo.us library to use this accordion");
var arr = new Array();
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('dd.'+this.options.contentClass);
		this.header = this.accordion.select('dt.'+this.options.toggleClass);
        this.isAnimating = false;
        this.maxHeight = 0;
        this.current = null; //defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];
        this.toExpand = null;
		this.clicked = null;

        //this.checkMaxHeight();
        //this.initialHide();
        //this.attachInitialMaxHeight();
		
		//this.header.addClassName(this.options.toggleActive);
		
		for(var i=0; i<this.contents.length; i++){
			arr[this.contents[i].identify()] = this.contents[i].getHeight();
        }

		var io = this.options.toggleActive;

		this.header.each(function(item) {
			item.addClassName(io);
		});

        var clickHandler =  this.clickHandler.bindAsEventListener(this);
        this.accordion.observe('click', clickHandler);
    },

    expand: function(id) {
		if(this.clicked.hasClassName(this.options.toggleActive)){
			this.current = this.clicked.next('dd.'+this.options.contentClass);
			this.animateHide();
		}
		else{
			this.toExpand = this.clicked.next('dd.'+this.options.contentClass);
			this.toExpand.show();
			this.current = null;
			this.animate(id);
		}
		/*if(this.toExpand != this.current || (!this.toExpand && !this.current)){
			this.toExpand = this.clicked.next('dd.'+this.options.contentClass);
			this.toExpand.show();
			this.animate(id);
			alert('hi');
		}
		else{
			this.animateHide();
		}*/
    },

    clickHandler: function(e) {
		this.clicked = e.element();
        if(this.clicked.hasClassName(this.options.toggleClass) && !this.isAnimating) {
			id = this.clicked.next('dd.'+this.options.contentClass).readAttribute('id');
			if(this.current != this.clicked.next('dd.'+this.options.contentClass))
				this.toExpand = null;
            this.expand(id);
        }
    },

    initialHide: function(){
        for(var i=0; i<this.contents.length; i++){
			arr[this.contents[i].identify()] = this.contents[i].getHeight();
            this.contents[i].hide();
            this.contents[i].setStyle({height: 0});
        }
    },
	
	animateHide: function(id) {
        var effects = new Array();

        options = {
            sync: true,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleX: false,
            scaleY: true
        };
		
		if(this.current)
        	effects.push(new Effect.Scale(this.current, 0, options));
			
		var myDuration = 0.75;

        new Effect.Parallel(effects, {
            duration: myDuration,
            fps: 35,
            queue: {
                position: 'end',
                scope: 'narrow-by-list'
            },
            beforeStart: function() {
                this.isAnimating = true;
				if(this.current)
                	this.current.previous('dt.'+this.options.toggleClass).removeClassName(this.options.toggleActive);
            }.bind(this),
            afterFinish: function() {
				if(this.current)
                	this.current.hide();
                this.current = null;
                this.isAnimating = false;
            }.bind(this)
		});
	},

    animate: function(id) {
        var effects = new Array();
        var options = {
            sync: true,
            scaleFrom: 0,
            scaleContent: true,
            transition: Effect.Transitions.sinoidal,
            scaleMode: {
                originalHeight: arr[id],
                originalWidth: this.accordion.getWidth()
            },
            scaleX: false,
            scaleY: true
        };

        effects.push(new Effect.Scale(this.toExpand, 100, options));

        options = {
            sync: true,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleX: false,
            scaleY: true
        };
		
		if(this.current)
        	effects.push(new Effect.Scale(this.current, 0, options));

        var myDuration = 0.75;

        new Effect.Parallel(effects, {
            duration: myDuration,
            fps: 35,
            queue: {
                position: 'end',
                scope: 'narrow-by-list'
            },
            beforeStart: function() {
                this.isAnimating = true;
				if(this.current)
                	this.current.previous('dt.'+this.options.toggleClass).removeClassName(this.options.toggleActive);
                this.toExpand.previous('dt.'+this.options.toggleClass).addClassName(this.options.toggleActive);
            }.bind(this),
            afterFinish: function() {
				if(this.current)
                	this.current.hide();
                this.toExpand.setStyle({ height: arr[id] });
				this.current = this.toExpand;
                this.isAnimating = false;
            }.bind(this)
        });
    }

});

		//accordion = new Accordion("narrow-by-list", 0);
}
	});
