$(document).ready(function() {

  /* Nasty */
  
  $('.pressrelease').parent().find('.a-slot.socialMedia').remove();
  
  /* MENU */
 
 $('.a-nav-main li').each(function(){
	 
	 if($(this).parent().attr('class') === 'a-nav a-nav-main accordion nav-depth-0 clearfix'){
		 
		$(this).find('a:first').css('cursor', 'default');
		 
	 }
	 
 });
  
  /* 404 REDIRECT */
  
  if($('.error-404').length > 0){
   
    function Redirect()
    {
      location.href = '/';
    }
   
    setTimeout(Redirect,10000);
        
  }
  
  //
  
  /* FANCYBOX */

  $('a.video').each(function() {

    $(this).click(function() {
      
      $.fancybox({
          'padding'   : 0,
          'autoScale'   : false,
          'transitionIn'  : 'none',
          'transitionOut' : 'none',
          'title'     : this.title,
          'width'   : 680,
          'height'    : 495,
          'href'      : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
          'type'      : 'swf',
          'swf'     : {
               'wmode'    : 'transparent',
            'allowfullscreen' : 'true'
          }
        });
      return false;
    });  
  
  });
  
  $('a.iframe').fancybox({
    'width'       : '70%',
    'height'      : '100%',
        'autoScale'       : false,
        'transitionIn'    : 'none',
    'transitionOut'   : 'none',
    'type'        : 'iframe'
  });
  
  $("a.lightbox-image").fancybox({
    'titlePosition'  : 'over'
  });
   
 // FAQ + textExpand
  
  $('.faq-box').hide();
  
  $('.textExpand .faq-row').each( function(){
    
    $(this).children('.faq-title').click( function(){

      $(this).next().slideToggle('fast', function(){
        
        $(this).find('img').each(function(){
         
          var align = $(this).attr('align');
          
          if ( align == 'left' ){
            
            $(this).css('margin', '0 15px 0 0');
              
          }
          
          if ($(this).parent().next('ul').length != ''){
            
            $(this).css('margin', '8px 30px 10px 0px');
            
          }
          
        });
        

        var cssDisplay = $(this).css('display');
        
        if (cssDisplay === 'none'){
            
          $(this).parent('.faq-row').find('.faq-title a').attr('class', 'up');
          
        };
        
      });
     /* 
      $(this).closest('.textExpand').siblings().find('.faq-box:visible').slideUp('fast', function(){
      
        $(this).parent('.faq-row').find('.faq-title a').attr('class', 'up');
      
      });
     */    
     $(this).parent('.faq-row').find('.faq-title a').attr('class', 'down');
      
    });
    
  });
  
  //
  
  $('.faq .faq-row').each( function(){
    
    $(this).children('.faq-title').click( function(){
      
      $(this).next().slideToggle('fast');
      
      /*
      $(this).closest('.faq').siblings().find('.faq-box:visible').slideUp('fast');
      */
      
    });
    
  });
  
  //
  
  $('.a-slideshow-controls').hide();
  $('.aSlideshow').hover(
    
    function(){
      
      $('.a-slideshow-controls').show('');
      
    },
    
    function(){
      
      $('.a-slideshow-controls').hide();
      
    }
    
  );
  
  // button grid
  
  $(".wd-button-grid.three .a-slots .a-slot:nth-child(3n)").css('margin','0 0 10px 0');
  $(".wd-button-grid.two .a-slots .a-slot:nth-child(2n)").css('margin','0 0 10px 10px');
  
  var sizeThree = $(".wd-button-grid.three .a-slots .a-slot").size();
  var leftOverThree = sizeThree % 3;
  
  if ( leftOverThree == 1 ){
    
    $(".wd-button-grid.three .a-slots .a-slot:nth-child(" + sizeThree + ")").addClass('four');
    
  }
  else if( leftOverThree == 2 ){
    
    $(".wd-button-grid.three .a-slots .a-slot:nth-child(" + (sizeThree - 1) + ")").addClass('five');
        
  }
  
  var sizeTwo = $(".wd-button-grid.two .a-slots .a-slot").size();
  var leftOverTwo = sizeTwo % 2;
  
  if ( leftOverTwo == 1 ){
    
    $(".wd-button-grid.two .a-slots .a-slot:nth-child(" + sizeTwo + ")").addClass('one');
    
  }
  
  // form validation
  $("#form").validate();
  $("#vote-form").validate();
  
  $('.radio_list label.error').closest('ul').css('border', '1px dotted red');
  
  $('#a-admin-filters-container').hide();
  
  //
  
  $('.aRichText img').each(function(){
    
    var align = $(this).attr('align');
    
    if ( align == 'left' ){
      
      $(this).css('margin', '0 15px 0 0');
      
    }
    
    if ( align == 'right' ){
      
      $(this).css('margin', '0 0 0 15px');
      
    }
    
    if ($(this).parent().next('ul').length != ''){
    
      $(this).css('margin', '8px 30px 10px 0px');
      
    }
    
  });
  
  // Liegebeest
  
  function liegebeest_vote(number, id, title) {

    $('#top3 .option').each(function() {
      var text_el = $(this).find('.text');
      var b_el = $(this).find('b');
      if (text_el.text() == title) {
        $('input#liegebeest_option_'+b_el.text()).val('');
        text_el.text('');
        b_el.removeClass('active');
      } 
    });

    $('#top3 .option_'+number+' .text').text(title);
    $('#top3 .option_'+number+' b').addClass('active');
    $('input#liegebeest_option_'+number).val(id); 

  }
  
  $('.item').each(function() {
    var item = $(this);
    var title = item.find('h2').text();
    var id = item.attr('id').replace('item_', '');
    item.find('.options a').each(function() {
      var option = $(this);
      option.click(function(e) {
        e.preventDefault();
        var option_number = option.text();
        $('.item .option_'+option_number).removeClass('active');
        item.find('.options a').removeClass('active');
        option.addClass('active');
        liegebeest_vote(option_number, id, title);
      });
    });
  });

  $('.options li a').click(function(){
    
    if ( $('#vote-form').data('submitted') == 'true' ){  
    
      $('#vote-form .hidden input.required').each(function(){
        
        var $this = $(this);
        
        if ( $this.val() != '' ){
          if ( $this.hasClass('error')){
            $this.attr('class', 'required');
          }
        } else{
          $this.attr('class', 'required error');
        }
        
      });
      
      if ($('.hidden').find('input.error').length > 0 ){
        $("#error_text").css('display', 'block');
      } else{
        $("#error_text").css('display', 'none');
      }
      
    }
    
  });
     
  $("#vote-form .submit").click(function(){
    
    setTimeout(checkErrors, 1);
   
    function checkErrors(){
      $('.hidden label').remove();
      if ($('#vote-form .hidden input').hasClass('error')){
        $("#error_text").css('display', 'block');
      } else{
        $("#error_text").css('display', 'none');
      }
    }  
    
    $('#vote-form').data('submitted', 'true');
    
  });
  
  /* EXPORT MODULE */
  
  function onSelectChange(){
    var selected = $("#export_table option:selected").text();   
    
    if (selected == 'Formulieren'){
      $('form#export #export_form_id').css('display', 'block');
      $('form#export #export_form_id').prev().css('display', 'block'); 
    } else {
      $('form#export #export_form_id').css('display', 'none');
      $('form#export #export_form_id').prev().css('display', 'none');
    }  
  
  }
  
  $('form#export #export_form_id').css('display', 'none');
  $('form#export #export_form_id').prev().css('display', 'none');
  
  var selected = $("#export_table option:selected").text(); 
  
  if (selected == 'Formulieren'){
    $('form#export #export_form_id').css('display', 'block');
    $('form#export #export_form_id').prev().css('display', 'block'); 
  } else {
    $('form#export #export_form_id').css('display', 'none');
    $('form#export #export_form_id').prev().css('display', 'none');
  } 
  
  $('#export #export_table').change(onSelectChange);
  
  //----- SEARCH
  
  function search() {
    var keyword = $('#google-search').attr('data');
    
    if(keyword){
      setTimeout(function(){ 
          $('input.gsc-input').focus();
          $('input.gsc-input').val(keyword);
          $('input.gsc-search-button').click();
        }
        , 200
      );
    }
  }
  
  search();
  
});

/* FUNCTIONS */


/* COPY PASTE SLOT */

function SelectAll(id){
  document.getElementById(id).focus();
  document.getElementById(id).select();
}


