/** TACX FAQ collapsibles / metanavigation / composite visuals */
$(document).ready(function(){

      $('.tacx-visual-composite a').each(function() {
          $(this).hover(function() {
              $(this).children('img').stop().animate({ opacity: 0.6 }, 500);
          },
         function() {
             $(this).children('img').stop().animate({ opacity: 1.0 }, 300);
         });
      });

	initExtras();
	function initExtras(){

		$('.collapsible div').stop().hide();
		$('.collapsible li').each(function(){
			$(this).click(function(){
				$(this).toggleClass('active').children('div').slideToggle();
				return false;
			});
			$(this).hover(function() {
				if ($(this).hasClass('active') == false) {
					$(this).stop().addClass("hovered").children('div');					
				}
			}, function() {
				if($(this).hasClass('active') == false) {
					$(this).stop().removeClass("hovered");
				}
			});
		});
		$('.collapsible div.no-initial-hide').stop().show();
	}
	
	var searchInputFocus = false;
	
	var searchElement = $('#metasearch');
	var searchForm = $('#metasearch ul');
	$(searchForm).css('position', 'absolute');
	$(searchForm).css('margin-left', $(searchElement).offset().left + $(searchElement).innerWidth() - $('#metanav').offset().left - $(searchForm).innerWidth() - 20);
	$('#metanav li:has(ul) ul').hide();
	
	$('#metanav a').hover(function() {
		$(this).next('ul').addClass('hovered').stop(true, true).slideDown();
	}, function() {
		$(this).next('ul').removeClass('hovered');
		$(this).oneTime(500, 'remover', function() {
			if ($(this).next('ul').hasClass('hovered') == false)
				$(this).next('ul').slideUp();
		});
	});

	$('#metanav a + ul').hover(function() {
		$(this).addClass('hovered');
	});

	$('#metanav a + ul').mouseleave(function() {
		if (searchInputFocus == false)
		{
			$(this).removeClass('hovered');
			$(this).oneTime(500, 'remover', function() {
				if ($(this).hasClass('hovered') == false)
					$(this).slideUp();
			});
		}
	});
	
	$('#metanav input.searchInput').focus(function() {
		searchInputFocus = true;
	});
	
	$('#metanav input.searchInput').blur(function() {
		searchInputFocus = false;
	});
});

/** TACX custom validation rules/regex */
$(document).ready(function() {
  $.validator.addMethod('name_regex', function (value) { 
      return (/^[a-zA-Z\xC0-\xC6\xE0-\xE6\xC8-\xCB\xE8-\xEB\xCC-\xCF\xEC-\xEF\xD2-\xD6\xF2-\xF6\xD9-\xDC\xF9-\xFC\xD1\xF1\ \-\'\.]+$/.test(value) || !value);  
  }, 'Je naam bestaat uit vreemde tekens');
  $.validator.addMethod('postcode_regex', function (value) { 
      return (/^[0-9][0-9][0-9][0-9]\ ?[a-zA-Z][a-zA-Z]$/.test(value) || !value); 
  }, 'Je postcode bestaat uit vreemde tekens');
  $.validator.addMethod('telephone_regex', function (value) { 
      return (/^[(0|+)][0-9]{1}[0-9- ]?[0-9- ]+$/.test(value) || !value); 
  }, 'Je telefoonnummer bestaat uit vreemde tekens');
  $.validator.addMethod('nl_bank_account', function (value) { 
    if (value)
    {

      bankrekeningnummer = value;
      // verwijder alle tekens die geen cijfers zijn
      bankrekeningnummer=bankrekeningnummer.replace(/\D/, "");
      aantal_tekens=bankrekeningnummer.length;
      if (aantal_tekens >=3 && aantal_tekens <= 7)
      {
        return true;
      }
      else
      {
        var som=0;
        // loop door de 9 cijfers met de 11 proef formule
        for (i=1; i<10; i++) 
        {
          getal=bankrekeningnummer.charAt(i-1);
          som+=getal*(10-i);
        } 
        // geef resultaat van check terug
        if (som % 11==0 && aantal_tekens==9) 
        {
          return true;
        }
        else 
        {
          return false;
        }
      }
    }
    else
    {
      return true;
    }
  }, 'Ongeldig rekeningnummer');
});
