var timer = 0;
var old_value = '';
var show = false;
var value = '';
var select_num = 0;
var select_value = '';
var count = 0;

function getSearch(value) {
    JsHttpRequest.query(
        '/search/?sstring=' + value + '&getAjax',
        {  },
        function(result, errors) {
            if (errors) {
                alert(errors);
                return;
            }
            addList(result['items'], result['count']);
        },
        true
    );
}

function setKeyDown() {
    if (timer) {
        clearTimeout(timer);
    }
    timer = setTimeout(setKeyUp, 1000);
}

function setKeyUp() {
    value = document.getElementById('sstring').value;
    if (value == old_value) return false;
    old_value = value;
    getSearch(value);
}

function addList(res, all_count) {
    if (res.length == 0) {
        hideItems();
        return false;
    }
    if (res.length > 0) showItems();
    
    count = res.length;
    
    var ul = document.getElementById('search-ul');
    ul.innerHTML = '';
    
    for (var i = 0; i < res.length; i++) {
        li = document.createElement('li');
        li.innerHTML = res[i]['head'];
        var id = i + 1;
        li.setAttribute('id', 'res_' + id);
        li.setAttribute('onmouseover', 'setSelect(' + id + ');');
        li.setAttribute('onclick', 'setValue(this.innerHTML);');
        ul.appendChild(li);
    }
    
    document.getElementById('all_count').innerHTML = all_count;
}

function hideItems() {
    document.getElementById('search-list').style.display = 'none';
    show = false;
    old_value = select_value;
    select_num = 0;
}

function showItems() {
    document.getElementById('search-list').style.display = 'block';
    show = true;
}

function setKeyPress(event) {
    if (event && event.keyCode && show) {
        var code = event.keyCode;
        
        var num = select_num;
        
        if (code == 38) { // up
            if (select_num <= 1) {
                num = count;
            }
            else {
                num--;
            }
            setSelect(num);
        }
        
        if (code == 40) { // down
            if (select_num == count) {
                num = 1;
            }
            else {
                num++;
            }
            setSelect(num);
        }
        
        if (code == 13) { // enter
            setValue();
            return false;
        }
    }
}

function setValue(val) {
    document.getElementById('sstring').value = (val ? val : select_value);
    hideItems();
}

function setSelect(num) {
    var old = select_num;
    if (document.getElementById('res_' + old)) {
        var el = document.getElementById('res_' + old);
        el.className = '';
        select_value = '';
    }
    if (document.getElementById('res_' + num)) {
        var el = document.getElementById('res_' + num);
        el.className = 'hover';
        select_value = el.innerHTML;
    }
    select_num = num;
}

window.onclick = function() {
    if (show == true)
        hideItems();
}

