﻿
/* ---------------------------------------------------------------------------------- */
/* ----- Functies: onLoad ----------------------------------------------------------- */
/* ---------------------------------------------------------------------------------- */

$(document).ready(function() {
  // Formulier-validatie activeren
  formtest = $().formtest($(".regel-button a"));

  // Lightbox activeren
  $(".lightbox").lightbox();

  // Popup overlay doorzichtig maken
  $("#popup_overlay").css({ opacity: 0.7 });

  // Zoekvenster events binden
  bindInputEvents("#searchfield", "Uw zoekopdracht...", true);
  $("#searchfield").bind("keydown", function(e){if ((document.layers ? evt.which : e.keyCode) == 13) {return false;}});
  $("#searchfield").bind("keyup", function(e) { if ((document.layers ? evt.which : e.keyCode) == 13) { search(); }; suggest($(this).val()) });
  $("#searchfield").bind("focus", function(e) { suggest($(this).val()) });
  $("#searchfield").bind("blur", function(e) { fill() });
  $(".search-button").bind("click", function() { search(); });

  $("#main .filtering .soortobject").change(function() { typewoning() });

  function typewoning() {
    var value = $("#main .filtering .soortobject").val();
    $("#main .filtering .typewoning").hide();
    $("#main .filtering .typekavel").hide();
    $("#main .filtering .typeligplaats").hide();
    $("#main .filtering .typeniks").hide();
    $("#main .filtering .type" + value).show();
  }
  typewoning();

  $("#item_select").change(function() {
    var message_index

    message_index = $("#item_select").val();
    $("#message_display").empty();

    if (message_index > 0)
      $("#message_display").append(message[message_index]);
  });

  // Video's embedden
  $("#content .video").flash(null, { version: 9 }, function(htmlOptions) {
    var $this = $(this);
    htmlOptions.src = '/interface/flash/player.swf';
    htmlOptions.flashvars.image = $this.find("img").attr("src");
    htmlOptions.flashvars.url = $this.find("span").text();

    htmlOptions.flashvars.backgroundcolor = '38393B';
    htmlOptions.flashvars.maincolor = 'F37121';
    htmlOptions.flashvars.iconcolor = 'E8E8E8';
    htmlOptions.flashvars.cornerradius = '0';
    htmlOptions.width = 300;
    htmlOptions.height = 225;
    htmlOptions.allowFullscreen = 'true';
    htmlOptions.wmode = 'transparent';
    htmlOptions.flashvars.debug = 'false'
    htmlOptions.allowScriptAccess = 'always';
    $this.html('');
    $this.addClass('flash-replaced').prepend($.fn.flash.transform(htmlOptions));
  });

  // Animatie "Dromen Denken Doen" embedden (home-pagina)
  $("#dromen-denken-doen").flash(null, { version: 9 }, function(htmlOptions) {
    var $this = $(this);
    htmlOptions.src = '/interface/flash/dromen-denken-doen.swf';
    htmlOptions.width = 385;
    htmlOptions.height = 104;
    htmlOptions.allowFullscreen = 'true';
    htmlOptions.wmode = 'transparent';
    htmlOptions.flashvars.debug = 'false'
    htmlOptions.allowScriptAccess = 'always';
    $this.html('');
    $this.addClass('flash-replaced').prepend($.fn.flash.transform(htmlOptions));
  });

  // Aanbod-Filtering veld "zoekterm" events binden
  bindInputEvents("#filtering-searchbox input", "Typ uw zoekterm(en)...", false);

  var bannerrandom = Math.random();
  // Footer-logo-animatie embedden
  $(".footer-logos").flash({
    src: '/interface/flash/looper.swf',
    width: 970,
    height: 100,
    wmode: 'transparent',
    flashvars: { xml: "/pages/xml/banner.aspx?" + bannerrandom, random: "true", speed: 0.7, cache: "false" }
  });

  // Fader starten indien aanwezig (projectpagina's)
  initFader("#carousel", 2700, 750);

  // Bedrijfspresentatie tonen wanneer knop wordt aangeklikt
  $("#main .intro .button").bind("click", function(event) {
    togglePopup("popup-company-video", null, true);
    return false;
  });
});


/* ---------------------------------------------------------------------------------- */
/* ----- Functies: Algemeen --------------------------------------------------------- */
/* ---------------------------------------------------------------------------------- */

// Input-elementen voorzien van blur en focus events om standaard-tekst te tonen/verbergen
function bindInputEvents(inputElement, defaultValue, overruleVal) {
  if (overruleVal == true) {
    $(inputElement).val(defaultValue);
  }
  $(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");
  });
}


/* ---------------------------------------------------------------------------------- */
/* ----- Functies: Popup ------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------- */

// Gewenste popup tonen indien een ID is meegegeven, anders alle popups verbergen
function togglePopup(popupId, iFrameUrl, boolFlash) {
  if (popupId) {
    if ($("#" + popupId + ":hidden").length > 0) {
      if ($("#popup_overlay:hidden").length > 0) {
        $("html, body").scrollTop(0);
        $("#popup_overlay").height($(document).height());
        $("#popup_overlay").fadeIn(400, function() {
          $("#" + popupId).fadeIn(400, function() {
            if (boolFlash && $("#" + popupId + " .flash-replaced").length <= 0) {
              $("#" + popupId + " .company-video").flash({
                src: "/interface/flash/windgroep.swf",
                width: 720,
                height: 405,
                wmode: 'transparent'
              });
            }
          });
        });
        if (iFrameUrl) {
          $("#" + popupId + " iframe").attr("src", iFrameUrl);
        }
      } else {
        $(".popup").not("#" + popupId).fadeOut(400);
        $("#" + popupId).fadeIn(400);
      }
    } else {
      $("#" + popupId).fadeOut(400, function() { $("#popup_overlay").fadeOut(400) });
    }
  } else {
    $("#popup_overlay, .popup").fadeOut(400);
  }
}


/* ---------------------------------------------------------------------------------- */
/* ----- Functies: Zoeksuggesties --------------------------------------------------- */
/* ---------------------------------------------------------------------------------- */

var currentTimer = null;

function suggest(inputString) {
  clearTimeout(currentTimer);
  if (inputString.length == 0) {
    $('#search-suggestions').fadeOut();
  } else {
    currentTimer = setTimeout(function() {
      $('#searchbox').addClass('load');
      $.get("/pages/ajax/zoekpreview.aspx", { zoek: "" + inputString + "" }, function(data) {
        if (data.length > 0) {
          $('#search-suggestions').html(data);
          $('#search-suggestions').fadeIn();
        } else {
          $('#search-suggestions').fadeOut();
        }
        $('#searchbox').removeClass('load');
      });
    }
  , 400)
  }
}

function search() {
  var strZoek = $("#searchfield").val();
  if (strZoek.length > 0 && strZoek != "Uw zoekopdracht...") {
    //strZoek = escape(strZoek);
    strZoek = strZoek.replace(/ /g, '+').replace(/\%20/g, '+');
    document.location.href = $(".search-button").attr("rel") + strZoek;
  }
}

function fill(thisValue) {
  $('#searchfield').val(thisValue);
  setTimeout("$('#search-suggestions').fadeOut();", 200);
}


/* ---------------------------------------------------------------------------------- */
/* ----- Functies: Fader (carousel projecten) --------------------------------------- */
/* ---------------------------------------------------------------------------------- */

function initFader(container, pauseDuration, fadeDuration) {
  var maxItems = $(container).find("li").length;
  if (maxItems > 1) {
    var nextItem = 1;
    var currentItem = 0;

    setInterval(function() {
      if (nextItem > (maxItems - 1)) { nextItem = 0; }
      if (currentItem) $(container).find("li:eq(" + currentItem + ")").fadeOut(fadeDuration, function() { $(this).css("z-index", "1") });
      $(container).find("li:eq(" + nextItem + ")").css("z-index", "10").fadeIn(fadeDuration);
      currentItem = nextItem;
      nextItem++;
    }, (pauseDuration + fadeDuration));
  }
}


/* ------------------------------------------------------------------ */
/* --- Functies: Veelgestelde Vragen (faq) -------------------------- */
/* ------------------------------------------------------------------ */

$(".faq-box input").bind("keyup", function(e) { if ((document.layers ? evt.which : e.keyCode) == 13) { eval($(".faq-box a").attr("href")); } });

function toggleFaqAntwoord(object) {
  $(object).toggleClass("question-active");
  if ($(object).next().css("position") == "absolute") {
    $(object).next().css("display", "none");
    $(object).next().css("position", "static");
  }
  $(object).next().slideToggle(200);
}