 // Function to switch headers
 function switchHeaders() {
 	var newClass = '';
 	var currentClass = $('#headerSwitch').attr('class');
 	var currentClassInt = parseInt(currentClass.replace(/header-/, ''));
 	if(currentClassInt == 23) {
 		newClass = 'header-1';
 	} else {
 		newClass = 'header-' + (currentClassInt + 1);
 	}
 	$('#headerSwitch').fadeTo(250, 0.1, function() {
 		$('#headerSwitch').removeClass(currentClass);
 		$('#headerSwitch').addClass(newClass);
 		$('#headerSwitch').fadeTo(700, 1);
 	});
 	setTimeout('switchHeaders()', 10000);
 }
 
 /**
  * Calculate the total distance from and to location
  *
  * @return void
  */
 $(document).ready(function(){
	// Make lightbox out of .lightbox items
	 $('.autolightbox').lightBox();
	 
	 // Sales contract: calculate distance
	 $('#submit-sales-contract').click(function(){
		 var $street = $('#idStreet').val();
		 var $streetNumber = $('#idStreetNumber').val();
		 var $city = $('#idCity').val();

		 // if address isn't empty -> calculate distance
		 if (GBrowserIsCompatible() && $city)
		 {
	 		var dirn = new GDirections();

	 		// add error listener
	 		GEvent.addListener(dirn,"error", function() {
				//submit form without distance
	 			$('#idDistance').val(0);
	 			$('#idFormContact').submit();
	 		});

	 		// set location
	 		var loc = $street+' '+$streetNumber+' '+$city;
	 		var beversLoc = 'Drijhoek 44 2310 Rijkevorsel';

	 		// load directions
	 		dirn.load("from: "+beversLoc+" to: "+loc+" to: "+beversLoc, {getSteps:false});
	 		
	 		// add load listener
	 		var totalDistance = 0;
	 		GEvent.addListener(dirn,"load", function() {
	 			for (var i=0; i<dirn.getNumRoutes(); i++) {
	 	 			totalDistance +=dirn.getRoute(i).getDistance().meters;
	 			}
	 			$('#idDistance').val(
 					totalDistance
 				 );
	 			// submit form
	 			 $('#idFormContact').submit();
	 			 return true;
	       });
		 }
		 else
		 {
			// submit form
			 $('#idDistance').val(0);
 			 $('#idFormContact').submit(); 
		 }
		 
		 return false;
	 });
	 
	 // for every unit that's checked: show the details
	 $('input[type="checkbox"].unit').each(function(){
		if ($(this).attr('checked'))
		{
			var $divDetailId = '#'+$(this).attr('id')+'-detail';
			$($divDetailId).show();
		}
	 });
	 
	 // show/hide unit details
	 $('input[type="checkbox"].unit').click(function(){

		var $unitType = $(this).attr('rel');
		var $unitTypeCount = $('input[rel="'+$unitType+'"].unit:checked').length;
		var $unitTypeMax = 2;
			
		//if user wants to add a unit, check if the maximum amount of available
		//units is reached
		if ($(this).attr('checked'))
		{
			if ($unitTypeCount > $unitTypeMax) 
			{
				//msg
				alert($('#unitExtraMsg').html());
				
				//uncheck
				$(this).attr('checked', false);
				
				//show extra container
				$('#unitExtraContainer:hidden').slideDown();
				return false;
			}
		}
		//if user unchecked, we possible want to hide the "extra unit" container
		else if($unitTypeCount < $unitTypeMax)
		{
			$('#unitExtraContainer:visible').slideUp();
		}
		 
		 //show/hide unit
		 var $divDetailId = '#'+$(this).attr('id')+'-detail';
		 $($divDetailId).slideToggle();
	 });
	 
	 // show description when user checks unitextra
	 $('#unitExtra').click(function(){
		unitExtra();
	 });
	 unitExtra();
	 function unitExtra()
	 {
		 if ($('#unitExtra').attr('checked')) $('#unitExtraDescription').slideDown();
		else $('#unitExtraDescription').slideUp();
	 }
	 
	// for every option that's checked: show the items
	 $('input[type="checkbox"].option').each(function(){
		if ($(this).attr('checked'))
		{
			var $divOptionItemId = '#'+$(this).attr('id')+'-item';
			$($divOptionItemId).show();
		}
	 });
	 
	// show/hide unit details
	 $('input[type="checkbox"].option').click(function(){
		 var $divOptionItemId = '#'+$(this).attr('id')+'-item';
		 $($divOptionItemId).slideToggle();
	 });
	 
	 // hide country specific texts
	 function countryTexts()
	 {
		 if ($('#idCountry').val() == 'be')
		 {
			 $('span.nl').hide();
			 $('span.be').show();
		 }
		 else
		 {
			 $('span.be').hide();
			 $('span.nl').show();
		 }
	 }
	 
	 countryTexts();
	 $('#idCountry').change(function(){
		 countryTexts();
	 });
	 
	 // Start switching headers:
	 setTimeout('switchHeaders()', 10000);
});
