$(function() {
	$('body').addClass('js');
     $('a[rel=external]').attr('target','_blank');
	
	/* maillinks */
	$('span.email').each(function(){
		var email = $(this).html().replace('(at)','@');
		$(this).html('<a href="mailto:'+email+'">'+email+'</a>');
	});
	
	/* input fields, hidden labels */
	$('input[type=text],input[type=password],textarea').bind('focus', function(){
		$(this).removeClass('fail');
		$(this).addClass('hastext');
	});
	$('input[type=text],input[type=password],textarea').bind('blur', function(){
		if($(this).val()==''){
			$(this).removeClass('hastext');
		}
	});
	 
	/* carousel */
	/*console.log($('#slidercontainer ul'));*/
	$('#slidercontainer').jcarousel({
    	wrap: 'circular',       
    	auto: 5,
        initCallback: mycarousel_initCallback,
    	scroll: 1, 
    	itemVisibleInCallback: {
            onAfterAnimation:  updateContactLine
        }
    });

    /* lightbox */
     $("a[rel^='lightbox']").prettyPhoto({social_tools:'',overlay_gallery:''});

     /* print buttons */
     $('.print').click(function(){window.print();});

     /* select filter*/
     $("select[name=province]").change(function(){
        //console.log($(this).val());
        selectedProvince = $(this).val();
        cityselector = $("select[name=city]");
        
        cityselector.find('optgroup').each(function(){
           $(this).removeClass('hide');
           if(selectedProvince!=''){
              if($(this).hasClass('province_'+selectedProvince)){
                 $(this).removeClass('hide');
              } else{
                 $(this).addClass('hide');
              }
           }

        });
     });
     /* make it filter onload */
      $("select[name=province]").trigger('change');
	  
	  /* add to newsletter */
	  $('#newsLetterBtn').bind('click', function(){
		var email = $('#newsLetter').val();
		if(!validMail(email)){
			$('#error').html('Uw e-mailadres is niet correct.');
			$('#error').show();
			$('#correct').hide();
		}else{
			$.ajax({
				url		: '/ajax.php',
				type	: 'POST',
				data	: {
					emailAdres : email
				},
				success	: function(){
					$('#newsLetter').val('Uw e-mailadres')
					$('#error').html('');
					$('#correct').html('U bent nu ingeschreven op de nieuwsbrief.');
					$('#error').hide();
					$('#correct').show();
				}
			
			})	
		}
		return false;
	});
	
});
function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
    /* custom controls */
    jQuery('#slider .next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#slider .prev').bind('click', function() {
        carousel.prev();
        return false;
    });
    /* make thumbnails work */
 /*   $('.detail .thumbs li').each(function(index){
		$(this).find('a').click(function(){
			carousel.scroll(index+1);
			return false;
		});
	});*/
};
function updateContactLine(carousel, item, idx, state) {
    /*console.log('Item #' + idx + ' is now visible');
    console.log($(item));*/
    newHref = $(item).find('a').attr('href');
    $('#contactline .bekijken a').attr('href',newHref);
    
    newPhone = $(item).find('.phone').html();
    $('#contactline .phone').html(newPhone);
    
    newMail = $(item).find('.email').html();
    $('#contactline .mail').html(newMail);
};

