jQuery(document).ready(function() {

	if (document.getElementById('submit_geo')) {
		jQuery('#submit_geo').bind('click', function() {
			localiser();
		});
	}
	
	if (document.getElementById('submit_ville')) {
		jQuery('#submit_ville').bind('click', function() {
			situer();
		});
	}
	
	if (document.getElementById('searchInput')) {
		jQuery('#searchInput').focus(function() {
			this.value="";
		});
	}
	
	if (document.getElementById('villeDepartement')) {
		jQuery('#villeDepartement').focus(function() {
			this.value="";
		});
	}
		
	if (document.getElementById('form_proximite')) {
		jQuery('#form_proximite').submit(function() {
			situer();
			return false;
		});
	}
	
	if (document.getElementById('mycarousel')) {
		jQuery('#mycarousel').jcarousel({
			scroll:1,
			size:3,
			animation: "slow",
			wrap: "last",
			auto:5,
			initCallback: mycarousel_initCallback
		});
	}
	
	jQuery(".calendrier").datepicker();
	jQuery.datepicker.setDefaults(jQuery.datepicker.regional['fr']);
			
	if (document.getElementById('form_reservation')) {
		jQuery('#form_reservation').submit(function() {
			if (verifierReservation()) return true; else return false;
		});
	}
});

function localiser() {
	// Try W3C Geolocation method (Preferred)
	if(navigator.geolocation) {
		browserSupportFlag = true;
		navigator.geolocation.getCurrentPosition(function(position) {
			initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
			carte.setCenter(initialLocation);
			carte.setZoom(7);
			contentString = "<b>Vous \352tes ici.</b><br /><span style='font-size:9px'>La geo-localisation d\351pend de votre navigateur et peut donc \352tre erron\351e, utilisez le champs d'adresse si cela ne correspond pas \340 votre position.</span>";
			carte.setCenter(initialLocation);
			infowindow.setContent(contentString);
			infowindow.setPosition(initialLocation);
			infowindow.open(carte);
		}, function() {
			handleNoGeolocation(browserSupportFlag);
		});
	} else if (google.gears) {
		// Try Google Gears Geolocation
		browserSupportFlag = true;
		var geo = google.gears.factory.create('beta.geolocation');
		geo.getCurrentPosition(function(position) {
		initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
		carte.setCenter(initialLocation);
		carte.setZoom(7);
		contentString = "<b>Vous \352tes ici.</b><br /><span style='font-size:9px'>La geo-localisation d\351pend de votre navigateur et peut donc \352tre erron\351e, utilisez le champs d'adresse si cela ne correspond pas \340 votre position.</span>";
		carte.setCenter(initialLocation);
		infowindow.setContent(contentString);
		infowindow.setPosition(initialLocation);
		infowindow.open(carte);
		}, function() {
			handleNoGeolocation(browserSupportFlag);
		});
	} else {
		// Browser doesn't support Geolocation
		browserSupportFlag = false;
		handleNoGeolocation(browserSupportFlag);
	}
}

function handleNoGeolocation(errorFlag) {
  if (errorFlag == false) { alert("Erreur : votre navigateur n'est pas compatible avec la geolocalisation. Merci d'utiliser le champs d'adresse."); }
}


function situer() {
	geocoder = new google.maps.Geocoder();
	adresse = jQuery('#villeDepartement').val();
	geocoder.geocode( { 'address': adresse}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			carte.setCenter(results[0].geometry.location);
			carte.setZoom(7);
			contentString = "<b>Vous \352tes ici.</b>";
			carte.setCenter(results[0].geometry.location);
			
			infowindow.setContent(contentString);
			infowindow.setPosition(results[0].geometry.location);
			infowindow.open(carte);
		} else {
			if (status == "ZERO_RESULTS") status = "aucune r\351ponse n'a \351t\351 trouv\351e.";
			alert("Erreur de localisation : " + status);
		}
    });
}

function verifierReservation() {
	var chateau = jQuery('#searchInput').val();
	var email = jQuery('#emailInput').val();
	var debut_reservation = jQuery('#debut_reservation').val();
	var fin_reservation = jQuery('#fin_reservation').val();
	
	if ((chateau == "") || (email == "") || (debut_reservation == "") || (fin_reservation == "")) { alert("Merci de remplir tous les champs"); return false; }
	if ((chateau == "") || (email == "") || (debut_reservation == "") || (fin_reservation == "")) { alert("Merci de remplir tous les champs"); return false; }
	if (!verifMail(email)) { alert("L'adresse email est erron\351e"); return false; }
	if (jQuery('#checkCGV').attr("checked") == false) { alert("Merci d'accepter les conditions de cr\351ation de s\351jour"); return false; }
	
	return true;
}

function verifMail(mail) {
	if(mail!=""){
		if ((mail.indexOf("@")>=0)&&(mail.lastIndexOf(".")>mail.indexOf("@"))) {
			return true
		} else {
			return false
		}
	}else{
		return true
	}
}

function mycarousel_initCallback(carousel) {
	jQuery('.jcarousel-control a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});
}

function insertInput(chateau) {
	alert(chateau);
}
			
			
