var listChanger = Class.create(); listChanger.prototype = { initialize:function(container_id, class_name) { this.container = $(container_id); this.elements = this.container.getElementsByClassName(class_name); this.changer = this.container.getElementsByTagName("select")[0]; this.options = this.changer.getElementsByTagName("option"); if( !this.elements.length ) { return false; } this.hidden = 'hc_hidden'; this.cookie = this.changer.id + '_last_index'; this.currentIndex = Cookie.get(this.cookie); if( this.currentIndex ) { this.currentIndex = parseInt(this.currentIndex); } if( !(this.currentIndex) || this.currentIndex < 0 || this.currentIndex > (this.options.length-1)) { this.currentIndex = false } for( var i=0; i < this.options.length; i++ ) { if( this.options[i].selected ) { if( !this.currentIndex) { this.currentIndex = i; } else { this.options[i].selected = false; this.elements[i].addClassName(this.hidden); } } } if( !this.currentIndex || this.currentIndex == 0 ) { this.currentIndex = 2 } this.options[this.currentIndex].selected = true; this.elements[this.currentIndex].removeClassName(this.hidden); Event.observe( this.changer, 'change', this.changeOption.bind(this) ); //Event.observe( this.changer, 'keypress', this.checkKey.bindAsEventListener(this) ); return true; }, checkKey: function(event) { this.code = event.keyCode; if(this.code == Event.KEY_UP || this.code == Event.KEY_LEFT ) { if( (this.currentIndex - 1 ) > -1 ) { this.changer.selectedIndex = this.currentIndex - 1; } if( navigator.userAgent.indexOf('MSIE') != -1 && this.code == Event.KEY_UP ) { this.changer.selectedIndex += 1; } this.changeOption() ; } if(this.code == Event.KEY_DOWN || this.code == Event.KEY_RIGHT ) { if( (this.currentIndex + 1 ) < this.options.length ) { this.changer.selectedIndex = this.currentIndex + 1; } if( navigator.userAgent.indexOf('MSIE') != -1 && this.code == Event.KEY_DOWN ) { this.changer.selectedIndex -= 1; } this.changeOption() ; } return true; }, changeOption: function() { if( this.currentIndex == this.changer.selectedIndex ) { return false; } this.elements[ this.currentIndex ].toggleClassName(this.hidden); this.options[this.currentIndex].selected = false; this.currentIndex = this.changer.selectedIndex; this.elements[ this.currentIndex ].toggleClassName(this.hidden); this.options[this.currentIndex].selected = true; Cookie.set( this.cookie, this.currentIndex, 30); return true; } }