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,
    effects: [],
    animating: false,

    //  
    //  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: 'vertical',
            onEvent: 'click'
        }, options || {});

		// Disable animations because in IE flicks
        this.duration = 0;	//((11 - this.options.resizeSpeed) * 0.15);

        var accordions = $$('#' + container + ' .' + this.options.classNames.toggle);
        accordions.each(function(accordion) {
            
			var showSubItems = false;
			var contents = $(accordion.parentNode).getElementsByClassName('accordion_content');
			if(contents.length > 0)
			{
				var content = contents[0];
				if(content.children.length > 0)
					showSubItems = true;
			}
						
			Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion, showSubItems), false);
						
            if (this.options.onEvent == 'click') {
                accordion.onclick = function() { return false; };
            }

			this.currentAccordion = $(accordion.next(0));
			
            if (this.options.direction == 'horizontal') {
                this.currentAccordion.style.width = "0px";
            } else {
                this.currentAccordion.style.height = "0px";
            }
            //options.update({display: "none"});
			
			
			this.currentAccordion.style.display = "none";
			
            //this.currentAccordion = $(accordion.next(0)).setStyle(options);
			//this.currentAccordion = $(accordion.next(0));
        } .bind(this));
    },

    //
    //  Activate an accordion
    //
    activate: function(accordion, showSubItems) {
        if (this.animating) {
            return false;
        }

        this.effects = [];

        this.currentAccordion = $(accordion.next(0));
        if(showSubItems)
		{
			this.currentAccordion.setStyle({
				display: 'block'
			});
			
			var img = this.currentAccordion.parentNode.parentNode.children[0].children[0];
			img.src = this.options.imageBase + 'flecha2.gif';
		}
		
		this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

        if (this.options.direction == 'horizontal') {
            this.scaling = $H({
                scaleX: true,
                scaleY: false
            });
        } else {
            this.scaling = $H({
                scaleX: false,
                scaleY: true
            });
        }

        if (this.currentAccordion == this.showAccordion) {
            this.deactivate();
        } else {
            this._handleAccordion();
        }
    },
    // 
    // Deactivate an active accordion
    //
    deactivate: function() {
	
		var duration = this.duration;
        if (this.currentAccordion.childNodes.length == 0)
            duration = 0;

        var options = {
            duration: duration,
			scaleFrom: 100, 
			scaleTo: 0,
            afterFinish: function() {
                if (this.showAccordion) {
                    this.showAccordion.setStyle({
                        height: 'auto',
                        display: 'none'
                    });
                }
                this.showAccordion = null;
                this.animating = false;
            } .bind(this)
        };
		
		var img = this.showAccordion.parentNode.parentNode.children[0].children[0];
		img.src = this.options.imageBase + 'flecha1.gif';
				
        this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);

        new Effect.BlindUp(this.showAccordion, options);
    },

    //
    // Handle the open/close actions of the accordion
    //
    _handleAccordion: function() {
	
		
		if (this.currentAccordion.innerHTML != "")
		{
			this.effects.push(
				new Effect.BlindDown(this.currentAccordion, {scaleFrom: 0, scaleTo: 100})
			);
		}

        if (this.showAccordion) {
            this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
			
			var img = this.showAccordion.parentNode.parentNode.children[0].children[0];
			img.src = this.options.imageBase + 'flecha1.gif';

			this.effects.push(
				new Effect.BlindUp(this.showAccordion, {scaleFrom: 100, scaleTo: 0})
			);
        }

        var duration = this.duration;
        if (this.currentAccordion.childNodes.length == 0)
            duration = 0;

        new Effect.Parallel(this.effects, {
            duration: duration,
			queue: {
                position: 'end',
                scope: 'accordionAnimation'
            },
            beforeStart: function() {
                this.animating = true;
            } .bind(this),
            afterFinish: function() {
                if (this.showAccordion) {
                    this.showAccordion.setStyle({
                        display: 'none'
                    });
                }
                $(this.currentAccordion).setStyle({
                    height: 'auto'
                });
                this.showAccordion = this.currentAccordion;
                this.animating = false;
            } .bind(this)
        });
    }
}
	
