﻿$(document).ready(function () {
    $(".searchbox").keyup(function (event) {

        $(this).doTimeout('text-type', 375, function () {

            //$('.preloader').show();

            if ($('.searchbox').val().length > 1) {
                ReadWebservice($('.searchbox').val());

                $('.searchbox').focusout(function () {
                    $('.searchresult').focusout(function () {
                        $('.searchresult').hide();
                    });
                });

                //$('.preloader').hide();
            }
            else {
                $('.searchresult').hide();
            }
        });
    });

    $('.searchbox').click(function () {
        if ($('.searchbox').val().length > 1) {
            ReadWebservice($('.searchbox').val());
        }
    });

    $('form').click(function () {
        $('.searchresult').hide();
    });
});

/*Sökfunktion*/
function ReadWebservice(tbxSearch) {
    tbxSearch = tbxSearch.replace("'", "");

    $.ajax({
        type: "POST",
        url: "/SearchMagazines.asmx/ReturnMagazines",
        data: "{'sSearchQuery': '" + tbxSearch + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            AjaxSucceeded(msg);
        },
        error: AjaxFailed
    });
}

function AjaxSucceeded(result) {
    $('#SearchResult').html(result.d);
    $('#SearchResult').show();
}

function AjaxFailed(result) {
    $('#SearchResult').html("<ul class=searchresult><li><em>(" + result.status + ') ' + result.statusText + "</em></li></ul>");
}

(function ($) { var a = {}, c = "doTimeout", d = Array.prototype.slice; $[c] = function () { return b.apply(window, [0].concat(d.call(arguments))) }; $.fn[c] = function () { var f = d.call(arguments), e = b.apply(this, [c + f[0]].concat(f)); return typeof f[0] === "number" || typeof f[1] === "number" ? this : e }; function b(l) { var m = this, h, k = {}, g = l ? $.fn : $, n = arguments, i = 4, f = n[1], j = n[2], p = n[3]; if (typeof f !== "string") { i--; f = l = 0; j = n[1]; p = n[2] } if (l) { h = m.eq(0); h.data(l, k = h.data(l) || {}) } else { if (f) { k = a[f] || (a[f] = {}) } } k.id && clearTimeout(k.id); delete k.id; function e() { if (l) { h.removeData(l) } else { if (f) { delete a[f] } } } function o() { k.id = setTimeout(function () { k.fn() }, j) } if (p) { k.fn = function (q) { if (typeof p === "string") { p = g[p] } p.apply(m, d.call(n, i)) === true && !q ? o() : e() }; o() } else { if (k.fn) { j === undefined ? e() : k.fn(j === false); return true } else { e() } } } })(jQuery);
