window.addEvent("domready", function() {

	// menu
	var menu = new Menu("ulMenu", {
		offsetLeft: 0,
		offsetTop: 8,
		delay: 150,
		speed: 500,
		opacity: 0.7,
		minWidth: 150
	});
	
	// slide show
	var slideShowContent = $("slideShowContent");
	if (slideShowContent) {
		var slideShow = new SlideShow(slideShowContent, {
			fxDuration: 1000,
			period: 3000
		});
	}
	
	// contact
	var contact = new Contact("contactContainer", {
		zIndex: 1,
		moveDuration: 500,
		opacityDuration: 500
	});
	
	var inscription = $("inscription");
	if (inscription) {
		inscription.addEvent("click", function() {
			inscriptionPopup.open();										
		});
		
		var inscriptionPopup = new Popup({
			width: 400,
			offsetHeight: -0.1, // % between [-1, 1]
			zIndex: 5,
			filter: {
				opacity: 0.70,
				bgColor: "#000000"
			},
			draggable: true,
			duration: 100,
			className: "contactPopup"
		});
		var closeImg = new Element("div", {
		    "class": "closeWindow",
		    "events": {
		    	"click": function() {
		    	    inscriptionPopup.close();
			}
		    }
		});
		inscriptionPopup.grabHeader("contactPopupHead");
		closeImg.inject(inscriptionPopup.top, "top");
		if(window.pngFix) {
		    pngFix.fixElement(closeImg);
		}
		inscriptionPopup.grabContent("contactPopupContent");
		inscriptionPopup.grabBottom("contactPopupBottom");
	}
	
	
	// calendar
	if($("from") && $("to")) {
		var from = new Calendar({from : 'd/m/Y' }, { classes: ['dashboard'], direction: 1 });
		var to = new Calendar({to : 'd/m/Y' }, { classes: ['dashboard'], direction: 1 });
	}
	
	
});

function validateForm(form, errorContainer) {
    form = $(form);
    errorContainer = $(errorContainer);
    // clean the form and perform the check
    var from = null;
    for(var i=0; i<form.elements.length; i++) {
	el = $(form.elements[i]);
	var nodeName = el.nodeName.toLowerCase();
    	if(nodeName == "input" || nodeName == "select") {
    	    el.removeClass("invalidFormEl");
    	    if(el.value == '') {
    		el.addClass('invalidFormEl');
    		errorContainer.innerHTML = "Tous les champs sont obligatoires!";
    		el.focus();
    		return false;
    	    }
    	    if(el.id == "mail" && !el.value.test("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$")) {
    		el.addClass('invalidFormEl');
    		errorContainer.innerHTML = "Vueillez entrer une adresse email valide! (ex: jean.dupond@mail.com)";
    		el.focus();
    		return false;
    	    }
    	    if(el.id == "telephone" && !el.value.test("^0[0-9]{9}$")) {
    		el.addClass('invalidFormEl');
    		errorContainer.innerHTML = "Veuillez entrer un numéro de téléphone valide! (ex: 0645825693)";
    		el.focus();
    		return false;
    	    }
    	    if(el.id == "age") {
    		var age = el.value.toInt();
    		if(isNaN(age) || age < 5 || age > 40) {
    		    el.addClass('invalidFormEl');
    		    errorContainer.innerHTML = "Veuillez entrer un âge valable (ex: 11)!";
    		    el.focus();
    		    return false;
    		}
	    }
    	    if(el.id == "from") {
    		from = el;
    	    }
    	    if(el.id == "to" && from) {
    		var fromSplite = from.value.split("/");
    		var toSplite = el.value.split("/");
    		var fromDate = new Date(fromSplite[2], fromSplite[1], fromSplite[0]);
    		var toDate = new Date(toSplite[2], toSplite[1], toSplite[0]);
    		if(fromDate.getTime() > toDate.getTime()) {
    		    el.addClass('invalidFormEl');
    		    errorContainer.innerHTML = "La date de fin doit être postérieure à la date de début!";
    		    el.focus();
    		    return false;
    		}
    	    }
	}
    }
    return true;
}