$(document).ready(function() {
  if ($(window).width() >= 768) {
    $('ul.menu1>li').each(function() {
      if ($(this).parents('div.sitemap').length == 0) {
        $(this).webetui_pulldown({aniLen:300, hideDelay:500, slideUp:true});
      }
    });
    // $('div#slideshow').slideshow({intervalTime:4000, fadingTime:1000, random:true});
  }
  
  $('ul.menu1 li:last-child').addClass('last-child');
  
  tagmenu('form#zoeken input[type=text]');
  
  zoeklabel();
  
  $('div.producten a label').click(function() {
    var a = $(this).parent('a');
    location = a.attr('href');
  })
});

var showtimer = null;

function tagmenu(input_selector) {
  $(input_selector).keyup(function(e) {
    var letter = this.value;
    // if (letter.length > 0 && e.keyCode != 40 && e.keyCode != 38 && e.keyCode != 32) {
    if (e.keyCode != 40 && e.keyCode != 38 && e.keyCode != 32) {
      if (showtimer) {
        clearTimeout(showtimer);
        showtimer = null;
      }
      showtimer = setTimeout(function() {  
        var page = $(input_selector).parents('form').attr('action');
        $('div#tagmenu').load('/cgi-bin/webetui4.cgi?o=Argos&f=tagmenu&tag='+letter, '', function() {
          var height = $('div#tagmenu ul').height();
          if (height > 501) height = 501;
          $('div#tagmenu').height(height);
          $('div#tagmenu').show();
        });
      }, 500);
    }
  }).keydown(function(e) {
    if (e.keyCode == 27) { // esc
      $('#tagmenu').fadeOut('fast');
    }
    if (e.keyCode == 40) { // keydown
      var li = $('div#tagmenu li:has(a.selected)');
      if (li.length) {
        var next = li.next('li');
        if (!next.hasClass('lastchild')) {
          $('div#tagmenu li a').removeClass('selected');
          $('a', next).addClass('selected');
        }
      } else {
        $('div#tagmenu li:first-child a').addClass('selected');
      }
      // $(this).val(quote($('div#tagmenu li a.selected').text()))
      $(this).val($('div#tagmenu li a.selected').text());
    }
    if (e.keyCode == 38) { // keyup
      var li = $('div#tagmenu li:has(a.selected):not(:first-child)');
      if (li.length) {
        var prev = li.prev('li');
        $('div#tagmenu li a').removeClass('selected');
        $('a', prev).addClass('selected');
      }
      // $(this).val(quote($('div#tagmenu li a.selected').text()))
      $(this).val($('div#tagmenu li a.selected').text());
    }
  }).focus(function(e) {
    $('div#tagmenu').show();
    // $('#tagmenu').webetui_pulldown_show();
  }).blur(function(e) {
    $('#tagmenu').fadeOut('fast');
  });
}

function zoeklabel() {
  $('input#zoekterm').each(function() {
    var defvalue = $(this).attr('title') || $(this).attr('name');
    if($.trim(this.value) == "" || this.value == defvalue) {
      this.value = defvalue;
      $(this).addClass('dimmed');
    }

    $(this).focus(function() {
        if(this.value == defvalue) {
            this.value = "";
            $(this).removeClass('dimmed');
            $(this).removeClass('required');
        }
    });
    
    $(this).blur(function() {
        if($.trim(this.value) == "")
          this.value = defvalue;
        if(this.value == defvalue)
          $(this).addClass('dimmed');
    });
  });
}

