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

var Website = {
  run: function() {
    this.init();
    this.searchForm();
    this.loginForm();
    this.homepage();
    this.applicationForm();
    //this.handleSearchForm();
    this.handleFAQ();
    this.handleMovieWidget();
    this.handleForms();
    this.handleVertalingForm();
    this.blogFancyBox();
  },
  
  init: function() {
    $('body') // tell CSS that JS is here!
      .removeClass('no-js')
      .addClass('js');
	  
	  $("form input:has(+ .error):eq(0)").focus();
  },
  
  // Label acting like placeholder attribute fo the search form
  searchForm: function() {
    $("#search, .placeholder")
      .focus(function() {
        $(this).prev().addClass("focus");
      })
      .keyup(function() {
        $(this).prev().hide();
        if ($(this).val() == "") {
          $(this).prev().show();
          $(this).prev().addClass("focus");
        }               
      })
      .blur(function() {
        if ($(this).val() == "") {
            $(this).prev().show();
        }
        $(this).prev().removeClass("focus");
      });
  },
  
  // Login form show and hide
  loginForm: function() {
  /*
    $("#login").click(function(event){
      $("#login-form").stop(false, true).slideToggle(200); // show/hide on click
      
      // hide on click anywhere on the document (except login form)
      $(document).one('click',function() { 
        $("#login-form").stop(false, true).slideUp(200);
      });
      
      return false;
    });

    $("#login-form").click(function(event){
      event.stopPropagation(); // prevent form hidding on document click
    });
  */
  },
  
  // homepage stuff
  homepage: function() {
    // init slideshow
    $('#slideshow').slides({
      play: 5000,
      preload: true,
      generateNextPrev: true
    });
  },
  
  
  /***************************************************************
  *  Job application form
  ***************************************************************/
  applicationForm: function() {
    // display selected file via :after on label
    $('#application-form span.after').click(function(){
      $(this).next('input[type="file"]').click();
    });
  
    
    $("#application-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 ($("#letter").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;
    });
  },
  
  handleSearchForm: function() {
    $("form #search").siblings("button").click(function(e){
      e.preventDefault();
      var key = $(this).siblings("#search").val();
      var pattern = /\^[\w ]*$/;
      if (re.test(ww)==false) {  
        alert('fout');
        return false;
      }
    });
  },
  
  handleFAQ: function(){
    $('.faq h3').click(function(){
      $(this).toggleClass('active')
           .next('p')
           .slideToggle('fast');
    });
  },
  
  handleMovieWidget: function(){
    $('#sidebar .movie a').click(function(e){
      e.preventDefault();
      $.fancybox({
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'href': this.href.replace(new RegExp('watch\\?v=', 'i'), 'v/'),
        'type': 'swf',
        'swf': {
          'wmode': 'transparent',
          'allowfullscreen': 'true'
        }
      });
    });
  },
  
  handleForms: function(){
    $('.vertaling-form').jqTransform();
    $('.feedback-form').jqTransform();
    $('.contact-form').jqTransform();
    $('.view-brochure-form').jqTransform();
  },
  
  blogFancyBox : function(){
     $('#blog-post-content').each(function(){
      var $img = $(this).find('img').attr('rel', 'gallery'),
       $url = $img.attr('src');
       
       $img.each(function(){
          var $url = $(this).attr('src');
          $(this).wrap('<a href="' + $url + '" rel="fancybox">');
          $link = $(this).parent('a');
          $link.fancybox();
       });
     });
   },
  
  handleVertalingForm: function(){
    var $form       = $('.vertaling-form'),
        $select     = $form.find('#version'),
        $vertaling  = $select.parent().parent().next('.vertaling');
    
    $form.find('.jqTransformSelectWrapper ul a').click(function(){
      var $val = $(this).text();
      
      if ($val == 'vertaling aanvragen') {
        $select.parent().parent().nextAll('div').toggleClass('hidden');
      } else {
        $vertaling.addClass('hidden');
        $vertaling.nextAll('div').removeClass('hidden').show();
      }
    });
  }
  
};

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