Правка от lovepsone.)
[cswow.git] / js / my_livesearch.js
blob954285190bb500ab8957db93ed0c712248e035dd
1 /*
2 * Live search script
3 */
4 var ls_delay = 500; // Search delay after key up
5 var ls_hidedelay = 500; // Hide delay after lose focus (must be > 0)
7 var ls_mainDiv = 0, // Main div
8 ls_begin_timer = new Number(0);
10 function ls_postLoad()
12 var ls_input = getElementsByClass('ls_search', null, 'input');
13 if (ls_input == null)
14 return;
15 for (i=0;i<ls_input.length; i++)
17 var input = ls_input[i];
18 input.setAttribute("autocomplete", "off");
19 addEvent(input, 'keyup', liveSearch);
20 addEvent(input, 'focus', liveSearch);
21 addEvent(input, 'input', liveSearch);
22 addEvent(input, 'blur', ls_hide);
25 function ls_Init()
27 ls_body = document.body || document.documentElement;
28 ls_mainDiv = insertElement(ls_body, 'DIV', 'ls_my_livesearch');
29 ls_mainDiv.style.position = "absolute";
30 ls_mainDiv.style.zIndex = 10;
31 addLoadEvent(ls_postLoad);
33 function ls_dosearch(parent)
35 my_AJAX.GETupload('ajax.php?ls=' + parent.alt + '&name=' + parent.value, function (text){ls_show(parent, text);});
37 function liveSearch()
39 var name = this.value, input = this;
40 if (name.length < 2)
42 ls_hide();
43 return;
45 if (ls_delay)
46 ls_begin_timer.Timer(function (){ls_dosearch(input)}, ls_delay, true);
47 else
48 ls_dosearch(input);
50 function ls_show(parent, text)
52 ls_mainDiv.innerHTML = text;
53 parseHref(ls_mainDiv);
54 var bounds = getBounds(parent);
55 p = getPageRect(),
56 x = bounds.left,
57 y = bounds.top + bounds.height+1,
58 max_x = p.width - ls_mainDiv.offsetWidth,
59 max_y = p.height - ls_mainDiv.offsetHeight,
60 x = x > max_x ? max_x : x;
61 y = y > max_y ? max_y : y;
62 var css = ls_mainDiv.style;
63 css.left = (x < p.left ? p.left : x) + 'px';
64 css.top = (y < p.top ? p.top : y) + 'px';
65 css.display = "block";
67 function ls_doHide()
69 ls_mainDiv.style.display = "none";
71 function ls_hide()
73 ls_begin_timer.Timer('ls_doHide()', ls_hidedelay, true);
75 ls_Init();