/**
 * Faq
 * 
 * @require     MooTools
 * 
 * @author      Steffen Maechtel <s.maechtel@netzbewegung.com>
 * @version     1.00 alpha
 */

Nb_Faq = new Class({
    initialize: function(element)
    {
        this.element = element;
        
        this.currentExpanded = null;
        
        this.element.getElements('.faq-item-title').each(function(element) {
            element.addEvent('click', this.onClick.create({bind: this, event: true, arguments: element}));
        }.bind(this));
    },
    onClick: function(event, titleElement)
    {
        event.stop();
        
        var textElement = titleElement.getNext('.faq-item-text');
        
        if (this.currentExpanded && this.currentExpanded == textElement && titleElement.hasClass('faq-item-title-active'))
        {
            this.collapse(this.currentExpanded);
            return;
        }
        
        if (this.currentExpanded)
        {
            this.collapse(this.currentExpanded);
        }
        
        this.expand(textElement);
    },
    expand: function(textElement)
    {
        this.currentExpanded = textElement;
        
        var titleElement = textElement.getPrevious('.faq-item-title');
        
        textElement.setStyle('height',  0);
        textElement.setStyle('display', 'block');
        
        var textBodyElement = textElement.getChildren('.faq-item-text-body')[0];
        var height = textBodyElement.getSize().y + 10;

        textElement.tween('height', height);
        
        titleElement.addClass('faq-item-title-active');
        
    },
    collapse: function(textElement)
    {
        this.currentExpanded = null;
        
        var titleElement = textElement.getPrevious('.faq-item-title');
        
        var animation = new Fx.Tween(textElement, {
            onComplete: function()
            {
                this.element.setStyle('display', 'none');
            }
        });
        
        titleElement.removeClass('faq-item-title-active');
        
        animation.start('height', 0)
    }
});

