var DropdownTime = []; TimeBehaviour = { initialise : function () { this.isOpen = false; this.icon = document.getElementById( this.id + '-icon' ); this.icon.onclick = this.toggle.bind( this ); this.dropdowntime = document.getElementById( this.id + '-dropdowntime' ); var dropdown = ''; this.dropdowntime.innerHTML = dropdown; DropdownTime[ DropdownTime.length ] = this.dropdowntime; this.selectTag = document.getElementById( this.id + '-dropdowntime' + '-select' ); this.selectTag.onchange = this.updateValue.bind( this ); }, toggle : function() { if( this.isOpen ) this.close(); else this.open(); }, open : function() { this.isOpen = true; for( var i = 0; i < DropdownTime.length ; i++ ) { var dropdowntime = DropdownTime[i]; if( dropdowntime == this.dropdowntime ) Element.addClassName( dropdowntime, 'focused' ); else Element.removeClassName( dropdowntime, 'focused' ); } }, close : function() { this.isOpen = false; Element.removeClassName( this.dropdowntime, 'focused' ); }, updateValue : function() { if( this.selectTag.selectedIndex != null ) { var timeValue = this.selectTag.options[ this.selectTag.selectedIndex ].value; this.value = timeValue; if(this.onchange) this.onchange(); } this.close(); } }; Behaviour.register({ 'div.dropdowntime input' : TimeBehaviour, 'li.dropdowntime input' : TimeBehaviour });