﻿var topSearchResultsCloseTimer = null;
$(function() {
    $(".topSearchInputIdentifier").focus(function() {
        if ($(this).val() == "Sökord fritext...") {
            $(this).val("");
        }
        cancelHideTimer();
    });
    $(".topSearchInputIdentifier").blur(function() {
        if ($(this).val() == "") {
            $(this).val("Sökord fritext...");
        }
        setHideTimer();
    });
});

var currentlySelectedLink = null;
function doTopSearch(searchText, path, searchField) {
    $.getJSON(path + "/Handlers/FundSearchHandler.ashx", { s: searchText }, function(data) {
        $("#topSearchResultsList").empty();
        currentlySelectedLink = null;
        if (data != null && data.length > 0) {
            if ($("#topSearchResultsList").length == 0) {
                $("<div id=\"topSearchResultsList\"></div>").appendTo("body");
                var top = $(searchField).closest("fieldset").offset().top + 34;
                var left = $(searchField).closest("fieldset").offset().left;
                var width = $(searchField).closest("fieldset").width();
                $("#topSearchResultsList").css({ display: "none", position: "absolute", top: top + "px", left: left + "px", height: "100px", width: width + "px" });
                $("#topSearchResultsList").focus(function() {
                    cancelHideTimer();
                });
            }
            var resultsList = $("#topSearchResultsList");
            $("<ul/>").appendTo(resultsList);
            var ul = resultsList.find("ul");
            for (var i = 0; i < data.length; i++) {
                $("<li><a href=\"" + path + "/Funds/Quicktake/Overview.aspx?perfid=" + data[i].p + "&programid=" + data[i].i + "\">" + data[i].f + "</a></li>").appendTo(ul);
            }
            var links = ul.find("a");
            links.keyup(function(e) {
                if (e.keyCode == 27) {
                    $(".topSearchInputIdentifier").focus();
                    resultsList.hide();
                }
            });
            links.keydown(function(e) {
                registerKeyUpDown(this, e);
            });
            links.focus(function() {
                $(this).addClass("focused");
                cancelHideTimer();
            });
            links.blur(function() {
                $(this).removeClass("focused");
                setHideTimer();
            });
            $(searchField).keydown(function(e) {
                registerKeyUpDown(this, e, links.eq(0));
            });
            resultsList.fadeIn(300);
        }
    });
}

function registerKeyUpDown(link, e, next) {
    if (e.keyCode == 13) {
        $("#topSearchResultsList").hide();
    }
    if (next != null && e.keyCode == 40) {
        if (next.length > 0) {
            next.focus();
            return cancelEvent(e);
        }
    } else {
        if (e.keyCode == 40) {
            var next = $(link).parent().next().children("a");
            if (next.length > 0) {
                $(link).blur();
                next.focus();
            }
            return cancelEvent(e);
        } else if (e.keyCode == 38) {
            var next = $(link).parent().prev().children("a");
            if (next.length > 0) {
                $(link).blur();
                next.focus();
            } else {
                $(link).blur();
                $(".topSearchInputIdentifier").focus();
            }
            return cancelEvent(e);
        }
    }
}

function cancelHideTimer() {
    if (topSearchResultsCloseTimer != null) {
        window.clearTimeout(topSearchResultsCloseTimer);
        topSearchResultsCloseTimer = null;
    }
}

function setHideTimer() {
    if ($("#topSearchResultsList").css("display") != "none") {
        topSearchResultsCloseTimer = window.setTimeout(function() {
            $("#topSearchResultsList").fadeOut(200);
        }, 500);
    }
}