
// start Accordion
var Accordion = Class.create ({
    initialize: function(container,triggers,contents,options)
    {
		this.container = container;
		this.triggers = triggers;
		this.contents = contents;
			this.length = this.contents.size();
		this.options = Object.extend(
		{
			initialIndex: 0,
			equalHeight: false, // boolean only, does not currently accept user inputed value
			activeClassName: 'active',
			speed: 0.8
		}, options || {});
		if (this.options.initialIndex >= this.length)
		{
			this.options.initialIndex = 0;
		}

		this.isAnimating = false;
		this.currentIndex = this.options.initialIndex;

        this.maxHeight = this.options.equalHeight;
        if (this.options.equalHeight)
        {
			this.checkMaxHeight();
        }

        this.initialDisplay();

		var boundClickHandler = this.__Click.bindAsEventListener(this);
        this.triggers.invoke('observe', 'click', boundClickHandler);

    },
    checkMaxHeight: function()
    {
        for (var i=0; i<this.length; i++)
        {
            if (this.contents[i].getHeight() > this.maxHeight)
            {
                this.maxHeight = this.contents[i].getHeight();
            }
        }
    },
    initialDisplay: function()
    {
		this.initialHeight = this.options.equalHeight ? this.maxHeight+'px' : 'auto';
		this.contents.invoke('setStyle', {height: '0px'});
		//this.contents.invoke('hide');
        this.contents[this.options.initialIndex].setStyle({height: this.initialHeight});
        //this.contents[this.options.initialIndex].show();
		this.triggers[this.options.initialIndex].addClassName(this.options.activeClassName);
		Cufon.refresh();
    },

    __Click: function(e)
    {
        e.stop();
        var el = Event.findElement(e, 'div');
        for (var i=0; i<this.length; i++)
        {
	        if (this.triggers[i] == el)
	        {
				if (!el.hasClassName(this.options.activeClassName) && !this.isAnimating)
				{
					this.Animate(i);
				}
	        }
        }
    },
    Animate: function(index)
    {
		//this.contents[index].show(); // show the new content

		var effects = new Array();
		var options = {
			sync: true,
			scaleFrom: 0,
			scaleContent: false,
			transition: Effect.Transitions.easeOutExpo,
			scaleMode: {
				originalHeight: this.maxHeight || this.contents[index].scrollHeight,
				originalWidth: this.container.getWidth()
			},
			scaleX: false,
			scaleY: true
		};
		effects.push(new Effect.Scale(this.contents[index], 100, options));

        options = {
            sync: true,
            scaleContent: false,
            transition: Effect.Transitions.easeOutExpo,
			scaleMode: {
				originalHeight: this.maxHeight || this.contents[this.currentIndex].scrollHeight,
				originalWidth: this.container.getWidth()
			},
			scaleX: false,
			scaleY: true
        };
        effects.push(new Effect.Scale(this.contents[this.currentIndex], 0, options));

        new Effect.Parallel(effects, {
            duration: this.options.speed,
            fps: 35,
            queue: {
                position: 'end',
                scope: 'accordionAnimation'
            },
            beforeStart: function() {
                this.isAnimating = true;
                this.triggers[this.currentIndex].removeClassName(this.options.activeClassName);
                this.triggers[index].addClassName(this.options.activeClassName);
                Cufon.refresh();
            }.bind(this),
            afterFinish: function() {
                //this.contents[this.currentIndex].hide(); // hide the old content
                this.currentIndex = index;
                this.isAnimating = false;
            }.bind(this)
        });

    }

});
// end Accordion
