/* ===============================================================
||                                                              ||
||    Website gemaakt door / Website created by                 ||
||   _______ _____  _____ ____          _        _   _ _        ||
||  |__   __|  __ \|_   _|  _ \   /\   | |      | \ | | |       ||
||     | |  | |__) | | | | |_) | /  \  | |      |  \| | |       ||
||     | |  |  _  /  | | |  _ < / /\ \ | |      | . ` | |       ||
||     | |  | | \ \ _| |_| |_) / ____ \| |____ _| |\  | |____   ||
||     |_|  |_|  \_\_____|____/_/    \_\______(_)_| \_|______|  ||
||                                                              ||
=============================================================== */

var Website = {
  run: function() {
    this.init();
    this.homepage();
    this.locationSlider();
    this.autosizeMenus();
    this.clickThroughBannerDecoration();
    this.vacatureForm();
    this.readButton();
    this.printButton();
    this.filterForm();
  },
  
  /***************************************************************
  *  Init website
  ***************************************************************/
  init: function() {
    // tell CSS that JS is here!
    $('body')
      .removeClass('no-js')
      .addClass('js');
    
    $('a[href$=".jpg"], a[href$=".png"], [href$=".JPG"], a[href$=".PNG"]').fancybox(); // fancyboxfox images
    
    $('body').textChanger(); // init font-size changer
    
    
    // Capture right click
    $(".section.banner").rightClick( function(e) {
        return false;
    });
 
    // Capture right mouse down
    $(".section.banner").rightMouseDown( function(e) {
        return false;
    });
 
    // Capture right mouseup
    $(".section.banner").rightMouseUp( function(e) {
        return false;
    });
    
    
    
    
  },
  
  /***************************************************************
  *  Set widths of menu items
  ***************************************************************/
  autosizeMenus: function() {
    // main menu
    initAutoScalingNav({
      menuId: "main-menu"
    });
    
    // bottom menu
    initAutoScalingNav({
      menuId: "bottom-nav",
      tag: "li"
    });
  },
  
  /***************************************************************
  *  Poor pointer-events: none; implementation for IE 7 and 8
  ***************************************************************/
  clickThroughBannerDecoration: function() {
    $(".ie .decoration").click(function(e) {
      $(".banner a").each(function() {
        // check if clicked point (taken from event) is inside element
        var mouseX = e.pageX;
        var mouseY = e.pageY;
        var offset = $(this).offset();
        var width = $(this).width();
        var height = $(this).height();

        if (mouseX > offset.left && mouseX < offset.left+width 
          && mouseY > offset.top && mouseY < offset.top+height) {
          // force click event but check if it isn't used in the script
          if ($(this).click()) {
            window.location.href = $(this).attr('href');
          }
        }
      });
    });
  },
  
  /***************************************************************
  *  Homepage stuff
  ***************************************************************/
  homepage: function() {
    // init slideshow
    $('#slideshow').slides({
      play: 5000,
      effect: "fade",
      preload: true
    });
    
    if ($(".widget1286").length > 0) {
      var top = $(".widget1286").position().top;
      $(".first.side.column > a:first-child")
        .css("paddingTop", top + "px")
        .css("display", "block");
    }
  },
  
  /***************************************************************
  *  Sliding location's photos
  ***************************************************************/
  locationSlider: function() {
    $(".location-slider").each(function(){
      var list = $(this).find('ul');
      var width = 0;
      $(this).find("li").each(function(){
        width += $(this).width();
      });
      $(this).find("ul").width(width);
      
      var step = $(this).width();
      var scroll = 0;
      var maxScroll = width - $(this).width();
      
      if (maxScroll > 0) {
        $(this).prepend('<a href="#" class="control prev">&lt;</a>');
        $(this).append('<a href="#" class="control next">&gt;</a>');
        
        $(this).find('.prev').click(function(){
          if (scroll - step > 0 ) {
            scroll -= step;
            $(list).animate({
              marginLeft: -1 * scroll
            }, 200);
          } else {
            $(list).animate({
              marginLeft: 0
            }, 200);
            scroll = 0;
          }
          return false;
        });
        
        $(this).find('.next').click(function(){
          if (scroll + step < maxScroll ) {
            scroll += step;
            $(list).animate({
              marginLeft: -1 * scroll
              }, 200);
          } else {
            $(list).animate({
              marginLeft: (-1 * maxScroll)
            }, 200);
            scroll = maxScroll;
          }
          return false;
        });
      }
    });
  },
  
  /***************************************************************
  *  Print button
  ***************************************************************/
  printButton: function() {
    $("#print").click(function(){
      window.print();
      return false;
    });
  },
  
  /***************************************************************
  *  Readspeaker button
  ***************************************************************/
  readButton: function() {
    $("#read").click(function(){
      //alert("Put readspeaker API call here.");
      
    });
  },
  
  /***************************************************************
  *  Locations filter form
  ***************************************************************/
  filterForm: function() {
    $("#filterForm input[type='checkbox']").change(function(){
      $("#filterForm").submit();
    });
  },
  
  /***************************************************************
  *  Job application form
  ***************************************************************/
  vacatureForm: function() {
  
    $('#content .vacature-form span.after').click(function(){
      $(this).next('input[type="file"]').click();
    });
  
    
    $("#content .vacature-form input[type='file']").change(function(){
      //$(this).prev().attr("rel", $(this).val().split('\\').pop());
      $(this).prev('span.after').find('span').text($(this).val().split('\\').pop());
    });
    
    $("#application-form").submit(function(e){
      var toReturn = true;
      
      if ($("#application-form .errors")) {
        $("#application-form .errors").remove();
      }
      
      $("#application-form .submit").before('<div class="errors" />');
      
      var err = $("#application-form .errors");
	  
	  if ($("#name").val() == '') {
        $(err).append('<p>Naam veld is verplicht.</p>');
        toReturn = false;
      }
	  var patt=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/g;
	  var result=patt.test($('#email').val());
	  
	  if ($("#email").val() == '') {
        $(err).append('<p>Emailadres veld is verplicht.</p>');
        toReturn = false;
      } else if(result == false){
		$(err).append('<p>Emailadres veld is niet correct.</p>');
        toReturn = false;
	  }
      
      if ($("#brief").val() == '') {
        $(err).append('<p>Kies een te uploaden bestand: sollicitatiebrief.</p>');
        toReturn = false;
      }
      
      if ($("#cv").val() == '') {
        $(err).append('<p>Kies een te uploaden bestand: curriculum vitae.</p>');
        toReturn = false;
      }
      
      if(!toReturn) {
        e.preventDefault();
      }
      return toReturn;
    });
  }
};

$(function(){
  Website.run();
});
