// accordion.js v2.0
//
// Copyright (c) 2007 stickmanlabs
// Author: Kevin P Miller | http://www.stickmanlabs.com
// 
// Accordion is freely distributable under the terms of an MIT-style license.
//
// I don't care what you think about the file size...
//   Be a pro: 
//	    http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
//      http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
//

/*-----------------------------------------------------------------------------------------------*/

if (typeof Effect == 'undefined') 
        throw("accordion.js requires including script.aculo.us' effects.js library!");

var accordion = Class.create();
accordion.prototype = {

        //
        //  Setup the Variables
        //
        showAccordion : null,
        currentAccordion : null,
        duration : null,
        delay : null,
        effects : [],
        animating : false,
        currentAnimator : null,
        container : null,
        elements : [],
        elementsCurrentDims : [],
        elementsCurrentScale : [],
        //
        //  Initialize the accordions
        //
        initialize: function(container, options) {
                if (!$(container)) {
                        throw(container+" doesn't exist!");
                        return false;
                }
	  
                this.options = Object.extend({
                        resizeSpeed : 8,
                        classNames : {
                                toggle : 'accordion-toggle',
                                toggleActive : 'accordion-toggle-active',
                                content : 'accordion-content'
                        },
                        defaultSize : {
                                height : null,
                                width : null
                        },
                        direction : 'horizontal',
                        onEvent : 'click'
                }, options || {});
		
                this.duration = ((11-this.options.resizeSpeed)*0.15);

                this.container = container;
                var accordions = $$('#'+container+' .'+this.options.classNames.toggle);
                var i = 0;
                accordions.each(function(accordion) {
                        Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion), false);
                        /*if (this.options.onEvent == 'click') {
                                accordion.onclick = function() {
                                        return false;
                                };
                        }*/
                        this.elements[i] = accordion;
                        this.elementsCurrentScale[i] = 100;
                        this.elementsCurrentDims[i] = accordion.next(0).getDimensions();
                        i++;
                }.bind(this));
        },
	
        //.Scale
        //  Activate an accordion
        //
        activate : function(accordion) {
                if (this.animating) {
                        return false;
                }
		
                this.effects = [];
	
                this.currentAccordion = $(accordion.next(0));
		
                this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

                if (this.currentAccordion == this.showAccordion) {
                        this.deactivate();
                } else {
                        this._handleAccordion();
                }
        },

        getElementDimensions : function(element) {
                for(var i = 0; i < this.elements.length; i++) {
                        if (this.elements[i] == element)
                                return this.elementsCurrentDims[i];
                }
                return null;
        },

        getElementScale : function(element) {
                for(var i = 0; i < this.elements.length; i++) {
                        if (this.elements[i] == element)
                                return this.elementsCurrentScale[i];
                }
                return 100;
        },

        setElementScale : function(element, newScale) {
                for(var i = 0; i < this.elements.length; i++) {
                        if (this.elements[i] == element)
                                this.elementsCurrentScale[i] = newScale;
                }
        },

        //
        // Deactivate an active accordion
        //
        deactivate : function() {
                this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);

                new Effect.Scale(this.showAccordion, 0, {
                        sync : true,
                        scaleX : false,
                        scaleY : true,
                        scaleFrom : this.getElementScale(this.showAccordion.previous(0)),
                        scaleContent: false,
                        transition: Effect.Transitions.sinoidal,
                        scaleMode: {
                                originalHeight: this.getElementDimensions(this.showAccordion.previous(0)).height,
                                originalWidth:  this.getElementDimensions(this.showAccordion.previous(0)).width
                        },
                        queue: {
                                position: 'end',
                                scope: 'accordionAnimation'
                        },
                        afterFinish: function() {
                                this.showAccordion = null;
                                this.animating = false;
                        }.bind(this)
                });
                this.setElementScale(this.showAccordion.previous(0), 0);
        },

        //
        // Handle the open/close actions of the accordion
        //
        _handleAccordion : function() {
                /*if (this.currentAnimator != null && !this.animating) {
                        this.currentAnimator.cancel();
                        this.effects.clear();
                }*/
                var aniEffects = [];

                if (this.showAccordion) {
                        this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
                }
                var preShow = 0;
                this.elements.each(function(accordion) {
                        if (accordion.next(0) == this.currentAccordion) {
                                aniEffects.push(new Effect.Scale(this.currentAccordion, 100, {
                                        //duration: this.duration,
                                        sync: true,
                                        scaleX : false,
                                        scaleY : true,
                                        scaleFrom : this.getElementScale(this.currentAccordion.previous(0)),
                                        scaleContent: false,
                                        transition: Effect.Transitions.sinoidal,
                                        scaleMode: {
                                                originalHeight:  this.getElementDimensions(this.currentAccordion.previous(0)).height,
                                                originalWidth: this.getElementDimensions(this.currentAccordion.previous(0)).width
                                        }
                                }));
                                this.setElementScale(this.currentAccordion.previous(0), 100);
                                preShow = 0;
                        } else {
                                //alert(this.getElementScale(accordion) + ' : ' + preShow);
                                aniEffects.push(new Effect.Scale(accordion.next(0), preShow, {
                                        //duration: this.duration,
                                        sync: true,
                                        scaleX : false,
                                        scaleY : true,
                                        scaleFrom : this.getElementScale(accordion),
                                        scaleContent: false,
                                        transition: Effect.Transitions.sinoidal,
                                        scaleMode: {
                                                originalHeight: this.getElementDimensions(accordion).height,
                                                originalWidth: this.getElementDimensions(accordion).width
                                        }
                                }));
                                this.setElementScale(accordion, preShow);
                                if (preShow > 0) {
                                        preShow = preShow - 15;
                                } else if (preShow < 0) {
                                        preShow = 0;
                                }
                        }
                }.bind(this));
                
                this.currentAnimator = new Effect.Parallel(aniEffects, {
                        duration: this.duration,
                        delay : this.delay,
                        queue: {
                                position: 'end',
                                scope: 'accordionAnimation'
                        },
                      /*  beforeStart: function() {
                                this.animating = true;
                        }.bind(this),*/
                        afterSetup: function() {
                                this.animating = true;
                        }.bind(this),
                        afterFinish: function() {
                                this.showAccordion = this.currentAccordion;
                                this.animating = false;
                                if ($(this.showAccordion).clientTop < 0) {
                                        new Effects.ScrollTo(this.showAccordion);
                                }
                        }.bind(this)
                });
        }
}
	
