/* Simple Accordion Script 
 * Requires Prototype and Script.aculo.us Libraries
 * By: Brian Crescimanno <brian.crescimanno@gmail.com>
 * http://briancrescimanno.com
 * This work is licensed under the Creative Commons Attribution-Share Alike 3.0
 * http://creativecommons.org/licenses/by-sa/3.0/us/
 */

if (typeof Effect == 'undefined')
    throw("You must have the script.aculo.us library to use this accordion");

var Accordion = Class.create({

    initialize: function(id) {
        if(!$(id)) {
            return false;
        }
        this.accordion = $(id);
        this.options = {
            toggleClass: "accordion-toggle",
            toggleActive: "accordion-toggle-active",
            contentClass: "accordion-content"
        }
        this.contents = this.accordion.select('.'+this.options.contentClass);
        this.togglers = this.accordion.select('.'+this.options.toggleClass);
        this.isAnimating = false;
        this.maxHeight = 0;
        this.current = null;
        this.toExpand = null;

//        this.checkMaxHeight();
        this.initialHide();
//        this.attachInitialMaxHeight();

        var clickHandler =  this.clickHandler.bindAsEventListener(this);
//        var overHandler =  this.overHandler.bindAsEventListener(this);
//        var outHandler =  this.outHandler.bindAsEventListener(this);
        this.accordion.observe('click', clickHandler);
//        this.accordion.observe('mouseover', overHandler);
//        this.accordion.observe('mouseout', outHandler);

        return true;
    },

    expandDefault: function(index) {
        this.expand(this.togglers[index-1]);
    },

    expand: function(el) {
        this.togglers.each(function(el){
            el.removeClassName('expanded');
        });
        el.addClassName('expanded');
        this.toExpand = el.next('.'+this.options.contentClass);
        if(this.current != this.toExpand){
            this.toExpand.show();
            if(this.current) {
                this.current.hide();
            }
            this.current = this.toExpand;
        }
    },

    clickHandler: function(e) {
        var el = e.element();
        if(el.hasClassName(this.options.toggleClass) && !this.isAnimating) {
            this.expand(el);
        }
    },

//    overHandler: function(e) {
//        var el = e.element();
//        if(el.hasClassName(this.options.toggleClass) && !this.isAnimating) {
//            var toExpand = el.next('.'+this.options.contentClass);
//            if(this.current != toExpand) {
//                toExpand.show();
//            }
//        }
//    },
//
//    outHandler: function(e) {
//        var el = e.element();
//        if(el.hasClassName(this.options.toggleClass) && !this.isAnimating) {
//            var toExpand = el.next('.'+this.options.contentClass);
//            if(this.current != toExpand) {
//                toExpand.hide();
//            }
//        }
//    },

    initialHide: function(){
//        this.current.previous('.'+this.options.toggleClass).addClassName('expanded');
        for(var i=0; i<this.contents.length; i++){
            if(this.contents[i] != this.current) {
                this.contents[i].hide();
//                this.contents[i].setStyle({
//                    height: 0
//                });
            }
        }
    }
});