﻿
/* ---------------------------------------------------------------------------------- */
/* ----- Functies: onLoad ----------------------------------------------------------- */
/* ---------------------------------------------------------------------------------- */

$(document).ready(function () {
  if ($.isFunction($.fn.lightbox)) {
    $("a[rel*='lightbox']").lightbox();
    $("#overlay").css("opacity", "0.6");
  }
  initSearch();
});


/* ---------------------------------------------------------------------------------- */
/* ----- Functies: Algemeen --------------------------------------------------------- */
/* ---------------------------------------------------------------------------------- */

// Input-elementen voorzien van blur en focus events om standaard-tekst te tonen/verbergen
function bindInputEvents(inputElement) {
  if ($(inputElement).length > 0){
    var defaultValue = $(inputElement).val();
    $(inputElement).bind("focus", function (e) {
      if (this.value == defaultValue) $(this).val("").addClass("active");
    }).bind("blur", function (e) {
      if (this.value == "") $(this).val(defaultValue).removeClass("active");
    });
  }
}

function initSearch(){
  bindInputEvents("#search-field");
  $("#search-field").bind("keydown", function(e){if ((document.layers ? evt.which : e.keyCode) == 13) {return false;}});
  $("#search-field").bind("keyup", function(e) { if ((document.layers ? evt.which : e.keyCode) == 13) {return search("#search-field", "#search-button","Je zoekopdracht...");} });
  $("#search-button").bind("click", function() {return search("#search-field", "#search-button","Je zoekopdracht..."); });
}

/* ---------------------------------------------------------------------------------- */
/* ----- Functies: Zoeken ----------------------------------------------------------- */
/* ---------------------------------------------------------------------------------- */

var currentTimer = null;

function suggest(inputString) {
  clearTimeout(currentTimer);
  if (inputString.length == 0) { $('#search-suggestions').fadeOut(); }
  else {
    currentTimer = setTimeout(function() {
      $('#search-wrapper').addClass('load');
      $.get("/pages/ajax/zoekpreview.aspx", { zoek: "" + inputString + "" }, function(data) {
        if (data.length > 0) {
          $('#search-suggestions').html(data);
          $('#search-suggestions').fadeIn(100);
        } else {
          $('#search-suggestions').fadeOut(100);
        }
        $('#search-wrapper').removeClass('load');
      });
    }
  , 400)
  }
}

function fill(thisValue) {
  $('#search-field').val(thisValue);
  setTimeout("$('#search-suggestions').fadeOut();", 200);
}

function search(fieldSelector, buttonSelector, strIgnore) {
  var strZoek = $(fieldSelector).val();
  if (strZoek.length > 0 && strZoek != strIgnore) {
    //strZoek = escape(strZoek);
    strZoek = strZoek.replace(/ /g, '+').replace(/\%20/g, '+');
    document.location.href = $(buttonSelector).attr("rel") + strZoek;
  }
  return false;
}
