﻿
var g_Text = '';
var g_Timeout = null;
var xmlHttpReq;
var READYSTATE_COMPLETE = 4;
var g_uid;
var g_showResult;
var g_txtSearchID, g_searchResultID;
var g_TextInProcess;
var g_Calls = 0;
var g_enterKeyPressed = false;

function onKeyUp2(e, txt) {

    g_showResult = true;
    var keyCode = e ? (e.which ? e.which : e.keyCode) : event.keyCode;
    if (keyCode == 38) {
        g_uid = getUidMessage(g_uid, -1);
        selectMessage(g_uid);
        if (g_uid && g_uid != '')
            updateScroll(document.getElementById(g_uid), document.getElementById('divQuickSearch'), $('#tbl tr'));
        return;
    }
    else if (keyCode == 40) {
        g_uid = getUidMessage(g_uid, 1);
        selectMessage(g_uid);
        if (g_uid && g_uid != '')
            updateScroll(document.getElementById(g_uid), document.getElementById('divQuickSearch'), $('#tbl tr'));
        return;
    }
    else if (keyCode == 13) {
        g_showResult = false;
        clearTimeout(g_Timeout);
        $('#divQuickSearch').fadeOut('300');
        $('#divWait').fadeOut('300');
        goToArtDtl(g_uid);
        return;
    }
    else if (keyCode == 27) {
        if ($('#divQuickSearch').is(':visible') || $('#divWait').is(':visible')) {
            $('#divQuickSearch').fadeOut('300');
            $('#divWait').fadeOut('300');
            txt.value = g_Text;
        }
        else {
            txt.value = '';
            g_Text = '';
        }

        g_showResult = false;
        return;
    }

    if (g_Text != txt.value || txt.value == '') {
        g_Text = txt.value;
        clearTimeout(g_Timeout);
        $('#divQuickSearch').fadeOut('300');
        
        if (g_Text.length > 1 && g_Calls == 0) {
            g_Timeout = setTimeout("runQuickSearch('" + g_Text + "')", 500);
        }
    }
}

function createXMLHttpRequest() {
    try { return new XMLHttpRequest(); } catch (e) { }
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }
    alert("XMLHttpRequest not supported");
    return null;
}

function runQuickSearch(txt) {

    if (txt == g_Text) {
        g_Calls++;
        g_TextInProcess = g_Text;
        $("#divQuickSearch").fadeOut('300');
        $('#divWait').fadeIn('300', function() {
            xmlHttpReq = createXMLHttpRequest();
            xmlHttpReq.open("GET", "Service/QuickSearch.ashx?key=" + txt, true);
            xmlHttpReq.onreadystatechange = displayQuickSearch;
            xmlHttpReq.send(null);
        });
    }
}

function displayQuickSearch() {
    if (xmlHttpReq.readyState == READYSTATE_COMPLETE) {
        g_Calls--;

        if (!g_showResult)
            return;
        
        var txtSearch = document.getElementById(g_txtSearchID);
        var searchResult = document.getElementById(g_searchResultID);

        if (g_TextInProcess == g_Text) {
            var array = xmlHttpReq.responseText.split('#####');
            if (array.length > 1)
                $('#divQuickSearchProducts').html(array[1]);
            else
                $('#divQuickSearchProducts').html("");

            var count = parseInt(array[0]);
            if (count > 10 && searchResult) {
                $('#hrefSearchResult').text(searchResult.value.replace('[xxx]', array[0]));
                $('#divQuickSearchBottom').show();
            }
            else {
                $('#divQuickSearchBottom').hide();
            }

            $('#divNoProducts').hide();
            if ($('#tbl tr').length > 0) {
                $('#divWait').fadeOut('300', function() {
                    $("#divQuickSearch").show("blind", {}, 600);
                });
            }
            else {
                $('#divWait').fadeOut('300', function() {
                    $('#divNoProducts').show("blind", {}, 600, function() {
                        setTimeout("$('#divNoProducts').fadeOut('300')", 2400);
                    });
                });
            }

            if (txtSearch)
                txtSearch.focus();
        }
        else {
            runQuickSearch(g_Text);
        }
    }
}

function getUidMessage(uid, offset) {

    var index = -1;
    var uids = new Array();

    $('#tbl tr').each(function(i) {
        if (this.id == uid)
            index = i;
        uids[i] = this.id;
    });
    
    if (uids.length <= 0)
        return null;

    if (index + offset < 0)
        index = uids.length-1;
    else if (index + offset >= uids.length)
        index = 0;
    else
        index += offset;

    return uids[index];
}

function selectMessage(uid) {
    if (uid && uid != '') {
        $('#tbl tr').css({ 'background-color': '#ffffff' });
        $('#' + uid).css({ 'background-color': '#e7e7e7' });
    }
}

function goToArtDtl(uid) {
    if (uid && uid != '') {

        $('#divQuickSearch').fadeOut('300'); 
        var path = "";
        var elements = window.location.pathname.split('/');

        if (elements.length > 0 && elements[1] != null && elements[1] != "" && elements[1].indexOf(".aspx") < 0)
            path = window.location.protocol + "//" + window.location.host + "/" + elements[1];
        else
            path = window.location.protocol + "//" + window.location.host;

        path += "/ArtDtl.aspx?it=" + uid.replace("#", "")
        window.location = path;
    }
}

function showAll() {
    $('#divQuickSearch').fadeOut('300', function() {
        
        runQuickSearch(g_Text, 'y');
        
    });
}

// make visible selected item
function updateScroll(obj, container, lines) {
    var height = 0, clientHeight = 0;

    if (obj) {
        for (i = 0; i < lines.length; i++) {
            height = height + lines[i].clientHeight + 0;
            clientHeight = lines[i].clientHeight + 0;
            if (obj.id == lines[i].id)
                break;
        }

        if (height - clientHeight < container.scrollTop)
            container.scrollTop = height - clientHeight;

        if (height - container.scrollTop >= container.clientHeight)
            container.scrollTop = height - container.clientHeight + 10;
    }
}

function searchClick() {
    g_showResult = false;
    $('#divWait').fadeOut('300');
    if (g_enterKeyPressed && $('#divQuickSearch').is(':visible')) {
        $('#divQuickSearch').fadeOut('300');
        if (g_uid) {
            goToArtDtl(g_uid);
            return false;
        }
    }
    return true;
}
