var siteSearch = Class.create(); siteSearch.prototype = { initialize: function() { this.search_form = $('search_form'); this.search_input = $('search_text'); this.search_button = $('search_button'); this.search_status = $('search_status'); this.default_value = this.search_input.title; this.init_value = this.search_input.value.strip(); this.button_value = this.search_button.title; this.last_search_keyword = ''; this.wait = 'Searching...'; this.msg_empty = 'Enter keyword'; this.msg_not_found = 'Matches not found'; this.msg_too_short = 'Keyword is too short'; this.msg_same_keyword = 'Keyword is same'; this.min_length = 2; this.search_input.value = (this.init_value.length < this.min_length) ? this.default_value : this.search_input.value; Element.hide(this.search_status); this.search_form.onsubmit = function(){ return false; } Event.observe ( this.search_form, 'submit', this.send.bind(this) ); Event.observe ( this.search_input, 'focus', this.setFocus.bind(this,this.search_input) ); Event.observe ( this.search_input, 'focus', this.showMessage.bind(this,'') ); Event.observe ( this.search_input, 'blur', this.setBlur.bind(this,this.search_input) ); return true; }, setFocus: function( input ) { input.value = ( input.value.strip() == input.title ) ? '' : input.value.strip(); return true; }, setBlur: function( input ) { input.value = ( input.value.strip() == '' ) ? input.title : input.value.strip(); return true; }, showMessage: function( message ) { if( message == '' || message == undefined ) { if( this.search_status.innerHTML.length > 0 ) { this.search_status.innerHTML = ''; Element.hide(this.search_status); } } else { this.search_status.innerHTML = message; Element.show(this.search_status); } return true; }, send: function() { this.search_input.value = this.search_input.value.strip(); if( this.search_input.value == '' || this.search_input.value == this.default_value ) { this.showMessage(this.msg_empty); return false; } if( this.search_input.value.length < this.min_length ) { this.showMessage(this.msg_too_short); return false; } if( this.search_input.value == this.init_value || this.search_input.value == this.last_search_keyword ) { this.showMessage(this.msg_same_keyword); return false; } this.last_search_keyword = this.search_input.value; this.showMessage(this.wait); this.parameters = this.search_form.serialize(); this.search_form.disable(); this.search_request = new Ajax.Request( "pre-search", { method: "get", parameters: this.parameters, onComplete: this.reply.bind(this) } ); return false; }, reply: function( request ) { if( request.responseText == 'NOT_FOUND' ) { this.search_form.enable(); this.showMessage(this.msg_not_found); this.search_button.focus(); return false } else { this.showMessage(); document.location.href = 'search?text='+this.search_input.value; } return true; } };