var Drawers = Class.create(); Drawers.setup = function (container) { return new Drawers(container); }; Drawers.prototype = { initialize: function (container) { var self = this; this.container = $(container); this.container.getElementsBySelector(".drawer-content").each(Element.hide); var first = this.container.firstDescendant().addClassName('first'); if (first.match(':last-child')) { first.addClassName('last'); } else { first.next(':last-child').addClassName('last'); } this.opened = null; this.container .getElementsBySelector(".drawer-handle") .each(function (handle) { handle.observe('click', function (event) { Event.stop(event); var next = Event.element(event).next(); if (self.opened) { var isSelf = (self.opened == next); Effect.SlideUp(self.opened, {duration: 0.5}); self.opened.up().removeClassName('open').removeClassName('last-open'); self.opened = null; if (isSelf) return; } self.opened = next; Effect.SlideDown(next, {duration: 0.5}); var el = next.up().addClassName('open'); if (el.match(":last-child")) el.addClassName('last-open'); }); }); } }; Element.observe(window, 'load', function (event) { $$("ul.drawers").each(Drawers.setup); });