var App_Search = { _formId: 'search-menu-form', _queryInputObj: null, _searchResultsClass: 'search_result', _url: '', _maxItemsCount: 3, _autocompleteBlock: null, _query: '', _key: {RETURN: 13, ESC: 27, DEL: 46}, _searchSubmit: false, _needInit: true, _autocompleteBlockWidth: 630, _mouseOverBlock: false, init: function(params) { var timeout; App_Search._url = params.url; App_Search._queryInputObj = $('form#' + App_Search._formId).find('input[name="query"]'); if (App_Search._queryInputObj.length > 0 && App_Search._url.length > 0) { $.browser.opera && $('form#' + App_Search._formId).bind("submit.search", function() { if (App_Search._searchSubmit) { App_Search._searchSubmit = false; return false; } return true; }); App_Search._queryInputObj.attr("autocomplete", "off"); App_Search._queryInputObj.focusin(function() { App_Search.search(); }).focusout(function() { if (!App_Search._mouseOverBlock && !App_Search._needInit) { App_Search._autocompleteBlock.hide(); } }); App_Search._queryInputObj.bind($.browser.opera ? 'keypress' : 'keydown' + '.search', function(event) { App_Search.initAutocompleteBlock(); switch(event.keyCode) { case App_Search._key.RETURN: App_Search._searchSubmit = true; break; case App_Search._key.ESC: App_Search._autocompleteBlock.hide(); break; case App_Search._key.DEL: if (App_Search._queryInputObj.val().length == 0) { App_Search._autocompleteBlock.hide(); } break; default: clearTimeout(timeout); timeout = setTimeout(App_Search.search, 10); break; } }); } return false; }, search: function() { var query = App_Search.trimWords(App_Search._queryInputObj.val().toLowerCase()); if (query.length > 0) { App_Search._queryInputObj.addClass('loading'); $.ajax({ dataType: 'html', url : App_Search._url, data : ({query: query, limit: App_Search._maxItemsCount}), type : 'GET', success : function (data, textStatus) { if (textStatus == 'success' && data.length > 0) { App_Search.displayAutocompleteBlock(data); } else { App_Search._autocompleteBlock.hide(); } return false; } }); } if (!App_Search._needInit) { App_Search._autocompleteBlock.hide(); } return false; }, trimWords: function(value) { if (!value) { return ''; } return $.trim(value.replace('%', '').replace('\\', '')); }, initAutocompleteBlock: function() { if (!App_Search._needInit) { return false; } App_Search._autocompleteBlock = $("
") .hide() .addClass(App_Search._searchResultsClass) .css("position", "absolute") .appendTo(document.body); App_Search._needInit = false; App_Search._autocompleteBlock.bind("mouseover", function() { App_Search._mouseOverBlock = true; $('body').unbind(); }); App_Search._autocompleteBlock.bind("mouseleave", function() { App_Search._mouseOverBlock = false; $('body').bind('click', function() {App_Search._autocompleteBlock.hide()}) }); }, displayAutocompleteBlock: function(data) { App_Search._autocompleteBlock.empty().append(data); if ( $.fn.bgiframe ) { App_Search._autocompleteBlock.bgiframe(); } var offset = App_Search._queryInputObj.offset(); var width = App_Search._autocompleteBlockWidth; var inputDOM = App_Search._queryInputObj.get(0); App_Search._autocompleteBlock.css({ width: typeof width == "string" || width > 0 ? width : App_Search._autocompleteBlock.width(), top: offset.top + inputDOM.offsetHeight, left: offset.left }).show(); App_Search._queryInputObj.removeClass('loading'); } };