menu = {
    sections: [],
    elock: 0,
    highlight: 1,
    correctcursor: 1,

    init: function() {
        var secs = $(document).getElementsByClassName("section");
        for (var i=0; i<secs.length; i++) {
           menu.sections[i] = new section(secs[i]);
        }
    },

    effectLock: function(flag) {
        menu.elock=flag;
        if (menu.correctcursor) {
            for (var i=0; i<menu.sections.length; i++) {
                if (menu.elock) {
                   menu.sections[i].heading.style.cursor='default';
                } else {
                   menu.sections[i].heading.style.cursor='pointer';
                }
            }
        }
    },

    closeAll: function(obj) {
        for (var i=0; i<menu.sections.length; i++) {
            if (menu.sections[i] != obj) {
                menu.sections[i].close();
            }
        }
    }
};

function section(section) {
    this.heading = section.getElementsByTagName("h1")[0];
    this.content = section.getElementsByTagName("div")[0];
    if (!this.content) return false;
    this.content.style.display='none';
    this.heading.getElementsByTagName("a")[0].href='#';
    this.bindEvents();
}

section.prototype = {
    close: function() {
        if (!this.content) return false;
        var me = this;
        Effect.BlindUp(this.content, {
            beforeStart: function() {
                menu.effectLock(1);
                var subdiv = me.content.getElementsByTagName("div");
                if (subdiv.length) subdiv[0].style.overflow='hidden';
            },
            afterFinish: function () {
                menu.effectLock(0);
                var subdiv = me.content.getElementsByTagName("div");
                if (subdiv.length) subdiv[0].style.overflow='auto';
            }
        });
    },
    
    bindEvents: function() {
        Event.observe(this.heading, 'click', this.toggle.bindAsEventListener(this));
        this.heading.style.cursor='pointer';
    },

    toggle: function() {
        if (menu.elock) return false;
        if (menu.highlight) new Effect.Highlight(this.heading, {startcolor: '#202420'});
        if (this.isOpen()) {
            this.close();
        } else {
            this.open();
        }
    },

    isOpen: function () {
        return (this.content.style.display!="none");
    },

    open: function() {
        if (!this.content) return false;
        var me = this;
        menu.closeAll(this);
        Effect.BlindDown(this.content, {
            beforeStart: function() {
                menu.effectLock(1);
                var subdiv = me.content.getElementsByTagName("div");
                if (subdiv.length) subdiv[0].style.overflow='hidden';
            },
            afterFinish: function () {
                menu.effectLock(0);
                var subdiv = me.content.getElementsByTagName("div");
                if (subdiv.length) subdiv[0].style.overflow='auto';
            }
        });
    }
};

Event.observe(window, 'load', menu.init.bindAsEventListener(menu));
