/* Copyright 2011 Thijs Brilleman */

// GLOBAL VARS (DO NOT CHANGE!!)

// INIT
$(document).ready(function() {
  var sections = $("div.section");
  var menuOffsetTop = $("#mainNav").offset().top;
  var lastId;

  // Set up hover handlers (for background changes).
  $("#mainNav a").hover(
    function() {
      $(this).css({
        textDecoration: "underline"
      });
    },
    function() {
      $(this).css({
        textDecoration: "none"
      });
    }
  );

  // Set up event handler for scrolling.
  $(window).scroll(function() {
    var offsetTop, outerHeight, height, id;
    sections.each(function() {
      if ($(this).css("display") !== "none") {
        offsetTop = $(this).offset().top || 0;
        outerHeight = $(this).outerHeight() || 0;
        height = $(this).height() || 0;
        var offset = offsetTop + ( outerHeight + height ) / 2 - $(window).scrollTop();
        if(offset >= $(window).height() / 3) {
          id = $(this).attr('id').replace("Section","");
          if (id === "citaat" || id === "cursus") id="home";
          if(lastId !== id) {
            $("#" + lastId).css({backgroundColor: "transparent"});
            $("#" + id).css({backgroundColor: "white"});
          }
          lastId=id;
          return false;
        }
      }
    });
  });

  $(window).trigger("scroll");

  // enables button disabling
  initFormCheck();

  // laat andere messages zien als net aanvraag/inschrijving ingediend
  initReaction();

  // init scroll event handler (not if ie6)
  var msie6 = $.browser == 'msie' && $.browser.version < 7;
  if(!msie6) { $(window).scroll(updateMenuVisibility); }

  // init smooth scroll on menu item click
  $.localScroll({
    lazy:true, // dynamic updating
    stop: true, // prevents queueing
    hash:true // Changes hash in URL bar. Known to sometimes cause
    // compatibility problems.
  });

  function initFormCheck() {
    // disable submit buttons
    $("#inschrijvenButton").attr('disabled', true);
    $("#informatieButton").attr('disabled', true);

    // attach event listeners
    $("#inschrijvenCheckbox").click(
      function() { toggleButton($("#inschrijvenButton"),$(this)); }
    );
    $("#informatieCheckbox").click(
      function() { toggleButton($("#informatieButton"),$(this)); }
    );

  }

  function initReaction() {
    switch (window.location.hash) {
      case "#inschrijving_verzonden":
      $("#inschrijvenSection").detach();
      $("#inschrijving_verzonden").show();
      $("#inschrijving_verzonden").attr("id","inschrijvenSection");
      $.scrollTo("#inschrijvenSection");
      break;
      case "#informatieaanvraag_verzonden":
      $("#contactSection").detach();
      $("#informatieaanvraag_verzonden").show();
      $("#informatieaanvraag_verzonden").attr("id","contactSection");
      $.scrollTo("#contactSection");
      break;
    };
  }

  // EVENT HANDLERS

  function toggleButton(button,checkbox) {
    button.attr('disabled', !$(checkbox).attr('checked'));
  }

  // Called when scrolling; check if menu height has to be adjusted.
  function updateMenuVisibility () {
    var scrollTop = $(window).scrollTop();

    // if menu would become invisible; make fixed
    if ( scrollTop > menuOffsetTop ) {
      $("#mainNav").addClass("fixed-at-top");
    } 
    else {
      $("#mainNav").removeClass("fixed-at-top");
    }
  }
});
