var Zoeken = {
    init: function() {
        var self = this;
        self.setListeners();
        EventHandler.addListeners(self.listeners);
    },
    setListeners: function() {
        var self = this;
        self.listeners = [
            {parent: '#searchForm', child:'input[type=text]', event: 'focus', functie: self.onFocus},
            {parent: '#searchForm', child:'input[type=text]', event: 'keyup', functie: self.onKeyUp},
            {parent: '#searchForm', child:'input[type=text]', event: 'focusout', functie: self.onFocusOut},
            {parent: '#searchForm', child:'input[type=text]', event: 'keydown', functie: self.showLiveHints},
            {parent: '#searchForm', child:'input[type=text]', event: 'click', functie: self.onKeyUp},
            {parent: '#searchForm', child:'input[type=text]', event: 'click', functie: self.showLiveHints},
            {parent: '#searchForm', child:'input[type=text]', event: 'blur', functie: self.hideLiveHints},
        ];
    },
    onFocus: function(){
        if($(this).val() == 'Search' )
        {
            $(this).val('');
            $(this).focus();
        }
    },
    onFocusOut:function(){
        if($(this).val() == '' )
        {
            $(this).val('Search');
        }
    },
    onKeyUp: function(){
        clearTimeout($(document).data('timeoutId_Zoeken'));
        if($(this).val().length > 2)
        {
            Zoeken.showLiveHints();
            var timeoutId = setTimeout(function(){Zoeken.liveHints()}, 500);
            $(document).data('timeoutId_Zoeken', timeoutId); 
        }
        else
        {
            Zoeken.hideLiveHints();
        }
    },
    liveHints: function(){
        $('#zoekenNavLiveHints ul li.loader').show();
        $('#zoekenNavLiveHints ul li').not('.loader, .title').remove();
        AjaxRequest.request(false, {'mode': 'zoekenLiveHints', 'hint': $('#searchForm input[type=text]').val()}, Zoeken.liveHintsResultaat);
    },
    liveHintsResultaat: function(data)
    {
        var data = JSON.parse(data);
        if (data['succes'])
        {
            $('#zoekenNavLiveHints').html(data['response']);
            $('#zoekenNavLiveHints ul li.loader').hide();
        }
    },
    hideLiveHints: function(){
        var timeoutIdHide = setTimeout(function(){
            $('#zoekenNavLiveHints').hide()
            }, 500);
        $(document).data('timeoutIdHide_Zoeken', timeoutIdHide); 
    },
    showLiveHints: function()
    {
        clearTimeout($(document).data('timeoutIdHide_Zoeken'));
        if($('#zoekenNavLiveHints').is(':hidden') && $('#searchForm input[type=text]').val().length > 2)
        {
            $("#flyoutNav").hide();
            $("#WinkelMandjeKleinFlyout").hide();
            $("#inloggenKleinFlyout").hide();
//            $("#zoekenNavLiveHints").hide();
        
            $('#zoekenNavLiveHints').show();
        }
    }
}


/* oud!!!
            window.ZoekFilterVar = "pzoek:{%zoekformulier.PZOEK%};";

            $("#searchForm").submit(function(event){
                // prevent default posting of form
                event.preventDefault();

                window.ZoekFilterVar = "pzoek:"+encodeURI($('#searchForm input[type=text]').val())+";";
                var pdoelgroep = $('#searchForm select').val();
                var pdoelgroepData = "";
                if(pdoelgroep != undefined)
                {
                    pdoelgroepData = "pdoelgroep:"+$('#searchForm select').val()+";";
                }
                window.location.href = "/shop.html#/?filterupdate=true&vars="+pdoelgroepData+"pzoek:"+encodeURI($('#searchForm input[type=text]').val())+";";
            });*/