﻿$(document).ready(function() {
    $(".textarea").each(function() {
        $(this).click(function() {
            $(this).select();
        });
    });

    timeout_handler = null;
    var $input;
    $("#nav-2>input").each(function() {

        $input = $(this);
        $input.click(function() {
            download_pdf($(this));
        });

        $input.keyup(function(e) {
            var keypress = '';
            if (e.which > 31 && e.which != 127)
                keypress = String.fromCharCode(e.which);
            var inputfield = $(this);
            setTimeout(function() {
                download_pdf(inputfield);
            }
            , 200
            );

        });

        $input.focus(function() {
            if (timeout_handler != null) {
                clearTimeout(timeout_handler);
                timeout_handler = null;
            }
        });
        $input.blur(function() {
            timeout_handler = setTimeout(function() {
                $input.val('Välj Produktblad (PDF)');
                hide_pdfshow();
            }, 200);
        });
    });
    $("#pdf_show").focus(function() {
        if (timeout_handler != null) {
            clearTimeout(timeout_handler);
            timeout_handler = null;
        }
    });
    $("#pdf_show").blur(function() {
        timeout_handler = setTimeout(function() {
            $input.val('Välj Produktblad (PDF)');
            hide_pdfshow();
        }, 200);
    });
});

function hide_pdfshow() {
    $("#pdf_show").hide();
}


function download_pdf($this)
{
    if ($this.val() == 'Välj Produktblad (PDF)')
        $this.val('');
    
    var searchfor = $this.val().toLowerCase();
    
    var pdf_show = $("#pdf_show");
    pdf_show.show();
    var str = "";
    for (var i = 0; i < pdfs.length; i++)
    {
        if (searchfor == '' || pdfs[i].text.toLowerCase().indexOf(searchfor) == 0)
            str += '<div><a target="_blank" href="' + location.protocol + '//' + location.hostname + pdfs[i].value + '">' + pdfs[i].text + '</a></div>';
    }
    pdf_show.html(str);
}