// inc4.js 
//

/*-----------------------------------------------------------------------------------------------*/

if (typeof Effect == 'undefined')
	throw("inc4.js requires inc2.js!");

var accordion = Class.create();
accordion.prototype = {
    //
    //  Setup the Variables
    //
    showAccordion: null,
    currentAccordion: null,
    duration: null,
    effects: [],
    animating: false,
    //  
    //  Initialize the accordions
    //
    initialize: function(container, options) {
        this.options = Object.extend({
            resizeSpeed: 6,
            classNames: {
                toggle: 'accordion_toggle',
                toggleActive: 'accordion_toggle_active',
                content: 'accordion_content'
            },
            defaultSize: {
                height: 250,
                width: null
            },
            direction: 'vertical',
            onEvent: 'click'
        }, options || {});

        this.duration = ((11 - this.options.resizeSpeed) * .15);

        var accordions = $$(container + ' .' + this.options.classNames.toggle);
        accordions.each(function(accordion) {
            Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion), false);
            accordion.onclick = function() { return false; };

            if (this.options.direction == 'horizontal') {
                var options = $H({ width: '0px' });
            } else {
                var options = $H({ height: '0px' });
            }
            options.merge({ display: 'none' });
            this.currentAccordion = $(accordion.next(0)).setStyle(options);

        } .bind(this));
    },
    //
    //  Activate an accordion
    //
    activate: function(accordion) {
        if (this.animating) {
            return false;
        }

        this.effects = [];

        this.currentAccordion = $(accordion.next(0));
        this.currentAccordion.setStyle({
            display: 'block'
        });
        //accordion.children[0].style.display = "inline";
        hideAccordionHelp(accordion.id);
        
        if (this.currentAccordion == this.showAccordion) {
            return false;
        }

        this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

        if (this.options.direction == 'horizontal') {
            var adjustments = $H({
                scaleX: true,
                scaleY: false
            });
        } else {
            var adjustments = $H({
                scaleX: false,
                scaleY: true
            });
        }

        var options = $H({
            sync: true,
            scaleFrom: 0,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleMode: {
                originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight - 10,
                originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
            }
        });
        options.merge(adjustments);

        this.effects.push(
			new Effect.Scale(this.currentAccordion, 100, options)
		);

        if (this.showAccordion) {
            this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);

            options = $H({
                sync: true,
                scaleContent: false,
                transition: Effect.Transitions.sinoidal
            });
            options.merge(adjustments);

            this.effects.push(
				new Effect.Scale(this.showAccordion, 0, options)
			);
        }

        new Effect.Parallel(this.effects, {
            duration: 0.37,
            queue: {
                position: 'end',
                scope: 'accordionAnimation'
            },
            beforeStart: function() {
                this.animating = true;
                //stopLoadMarkers;
            } .bind(this),
            afterFinish: function() {
                if (this.showAccordion) {
                    this.showAccordion.setStyle({
                        display: 'none'
                    });
                }
                this.showAccordion = this.currentAccordion;
                this.animating = false;
                //restartLoadMarkers;
            } .bind(this)
        });
    }
}

	
