jQuery.fn.defuscate = function( settings ) {
		settings = jQuery.extend({
				link: true,
				find: /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi,
				replace: '$1@$2'
		}, settings);
		return this.each(function() {
				if ( $(this).is('a[@href]') ) {
						$(this).attr('href', $(this).attr('href').replace(settings.find, settings.replace));
						var is_link = true;
				}
				$(this).html($(this).html().replace(settings.find, (settings.link && !is_link ? '<a href="mailto:' + settings.replace + '">' + settings.replace + '</a>' : settings.replace)));
		});
};

$(document).ready(function(){
	
	if ($('html').attr('lang') == 'fr') {
		langue = 'fr';
	} else {
		langue = 'nl';
	}
	
	// Galerie de type "diaporama"
	$(".diaporama").wrap("<div class='galerie-wrap'></div>").innerfade({
		speed: 3000,
		timeout: 4000,
		type: 'random',
		containerheight: '150px'
	});
	
	// Galerie de type "numerotée"
	$(".numerotee").wrap("<div class='galerie-wrap'></div>").css({height:'150px', margin:0}).after('<p class="numerotation"></p>').children().css({position:'absolute'}).hide().each(
		function(i){
			id = i + 1;
			titre = $(this).children('a').children('img').attr('alt');
			$(this).attr('id', 'photo-'+id);
			$(".numerotation").append('<a id="#lien-'+id+'" href="#photo-'+id+'" title="Show '+titre+'">'+id+'</a>');
		}
	).first().show();
	
	$(".numerotation").children().click(
		function(){
			id = $(this).attr('id').substring(6);
			if($(this).attr('class')!='actif'){
				$(".numerotation").children().removeClass('actif');
				$(this).removeAttr('href').addClass('actif');
				$(".numerotee").children().fadeOut('normal');
				$('#photo-'+id).fadeIn('normal');
			}
		}
	).first().addClass('actif');

	$('a.fancybox').fancybox({
		//'titlePosition':'over'
	 });

	// ajout d'une marge à gauche pour les imgae flottantes à droite
	$('img').each(function(index) {
		if ($(this).css('float') == 'right') {
			$(this).css({'margin':'0 0 18px 18px'});
		};
	});

	$('#reservationForm').validationEngine();
	$('#contactForm').validationEngine();
	$('#commentaireForm').validationEngine();
	
	$('.adresse').defuscate();

	$('.calendar').datepicker({
		dateFormat: 'dd/mm/yy',
		minDate: new Date(),
		showAnim: 'fadeIn',
		speed: 'fast',
		showOn: 'button',
		buttonText: 'Choose',
		buttonImage: '/images/calendar.png',
		buttonImageOnly: true
	});	

	 // Gmaps 
	if ($('#map').length) {
		var markers = [];
		var noms = [];
		var iterator = 0;
		var mapOptions = { 
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			center: new google.maps.LatLng(50.85,4.36),
			mapTypeControl: false,
			streetViewControl: false,
			zoom: 16
		};
		var map = new google.maps.Map(document.getElementById("map"), mapOptions);
		
		google.maps.event.addListener(map, 'click', function() {
			infoWindow.close();
		});
				
		function drop() {
			for (var i = 0; i < markers.length; i++) {
				setTimeout(function() {
					addMarker();
				}, i * 200);
			}
		}
		
		function addMarker() {
			var marker = new google.maps.Marker({
				position: markers[iterator],
				map: map,
				draggable: false,
				animation: google.maps.Animation.DROP
			})
			marker.html = markers[iterator]['html'];
			markers.push(marker);
			google.maps.event.addListener(marker, 'click', onMarkerClick);
			google.maps.event.addListener(marker, 'mouseover', highligthAddress);
			iterator++;
		}

		function highligthAddress() {
			console.log('mouse is over a marker');
		}

		function AutoCenter() {
			var bounds = new google.maps.LatLngBounds();
			$.each(markers, function (index, marker) {
				bounds.extend(markers[index]);
			});
			map.fitBounds(bounds);
			var listener = google.maps.event.addListener(map, "idle", function() { 
				if (map.getZoom() > 16) map.setZoom(16); 
				google.maps.event.removeListener(listener); 
			});
		}
		
		var infoWindow = new google.maps.InfoWindow();
		var onMarkerClick = function() {
			var marker = this;
			console.log(marker);
			// var latLng = marker.getPosition();
			infoWindow.setContent(this.html);
			infoWindow.open(map, marker);
		};
		
		var infoWindow = new google.maps.InfoWindow;

		$.getJSON('?json', function(data) {
			for (var i = 0; i < data.length; i++) {
				markers[i] = new google.maps.LatLng(data[i]._data.latitude, data[i]._data.longitude);
				markers[i]['html'] = '';
				markers[i]['html'] += '<p style="margin-bottom:0;">';
				markers[i]['html'] += '<strong>' + data[i]._data.titre + ' ' + data[i]._data.prenom + ' ' + data[i]._data.nom + '</strong>';
				markers[i]['html'] += '<br />';
				markers[i]['html'] += data[i]._data.rue + ' ' + data[i]._data.numero;
				markers[i]['html'] += '<br />';
				markers[i]['html'] += data[i]._data.codepostal + ' ' + data[i]._data.localite;
				markers[i]['html'] += '</p>';
			};
			drop();
			AutoCenter();
		});
		
	};
		

	$.ga.load('UA-15162274-1'); // Google Analytics nouvelle formule
	
});

