$(document).ready(function() {
			
	//SLIDER
	rotatePics(1);
	
	//CONTACT
	$('#nav a.contact').click(function(){
	  $.scrollTo('.rechts', 'slow');
	  return false;
	});
	
	//TRACK
	$('#secondary').css('height', $(document).height());

	//AUTOS
	$('.img-container').hover(function(){
	  
	  $(this).find('.blackandwhite').fadeOut();
	
	}, function(){
	
	  $(this).find('.blackandwhite').fadeIn();
	
	});
	
	
  //LIGHTBOX
  $('.img-container').click(function(){

    var $id = $(this).attr('id');
  
    $('#overlay').fadeIn('fast', function(){
      $('#loadrContainer').show();
      $('#overlay').load('autos/lightbox', {car: $id}, function(){
        $('#loadrContainer').hide();
        $('.lightbox').fadeIn();
      });
      
    });  
    
  });

  //CLOSE
  $('.lightbox .top .close').live('mouseover', function(){
  
    $(this).fadeTo('fast', 1);
  
  });
  
  $('.lightbox .top .close').live('mouseout', function(){
    
    $(this).fadeTo('fast', 0.8);
  
  });
  
  $('.lightbox .top .close').live('click', function(){
    
    $('#loadrContainer').fadeOut('fast');
    $('#overlay').fadeOut('fast');
    $('.lightbox').fadeOut('fast');
     
  });

  //GALLERY
  $('.lightbox .bottom .thumbs img').live('mouseover', function(){
  
    $(this).fadeTo('fast', 1);
  
  });
  
  $('.lightbox .bottom .thumbs img').live('mouseout', function(){
  
    $(this).fadeTo('fast', 0.9);
  
  });
  
  $('.lightbox .bottom .thumbs img').live('mouseover', function(){
    
    var $level = $(this).index();
    
    if( $level !== $('.lightbox .top .active').index() ){
    
      $('.lightbox .top .active').fadeOut('fast', function(){ $(this).removeClass('active') });
      $('.lightbox .top .big').eq($level).fadeIn('fast', function(){ $(this).addClass('active') });
    
    }

  });
  
  serviceSlider(1);
  
	//AJAX-SERVICES
	$('#diensten a.service').click(function(){
	  
	  var identifier = this;
	  
	  //TEXT LADEN
	  $('#service_info .contents').fadeOut('fast', function(){
	  
	    $(this).load('/services/services', {id: $(identifier).attr('id')}, function(){
	    
	      $(this).fadeIn();
	    
	    });
	  
	  });
	  
	  //AFBEELDING LADEN
	  $('#services_slider img').fadeOut('fast', function() {
	    $('#services_slider').load('/services/images',{
      id: $(identifier).attr('id')},
      function(){
         $('#services_slider img').fadeIn('fast', function() {
          serviceSlider(1);
         });
      });
	  });
	  
	  return false;
	  
	});

});

//FUNCTIONS
function rotatePics(currentPhoto) {

	var numberOfPhotos = $('#slider img').length;
	currentPhoto = currentPhoto % numberOfPhotos;
	
	$('#slider img').eq(currentPhoto).fadeOut('slow', function() {
		$('#slider img').each(function(i) {
			$(this).css(
				'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos
			);
		});
		
		$(this).show();
		setTimeout(function() {rotatePics(++currentPhoto);}, 4000);
	
	});
		
}

function serviceSlider(currentPhoto){
  var numberOfPhotos = $('#services_slider img').length;
  if(numberOfPhotos!=1){
    currentPhoto = currentPhoto % numberOfPhotos;
    
    $('#services_slider img').eq(currentPhoto).fadeOut('slow', function() {
      $('#services_slider img').each(function(i){
        $(this).css(
          'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos
        );
      });
      
      $(this).show();
      setTimeout(function() {serviceSlider(++currentPhoto);}, 2000);
      
    });
    
  }
}
