window.onload = function() {
	if(document.getElementById("flashmob_link")) document.getElementById("flashmob_link").onclick = swap.curry("inscription");
	if(document.getElementById("repetition_link")) document.getElementById("repetition_link").onclick = swap.curry("inscription2");
	if(document.getElementById("form")) document.getElementById("form").onsubmit = record;
	
	$('#gallery').show().galleria();

}

var timer;

function adapt(element, targetHeight, step) {
	var height = element.offsetHeight-22;
	if(Math.abs(height - targetHeight) < step) {
		element.style.height = targetHeight+"px";
		cancelInterval(timer);
	} else if(height < targetHeight) element.style.height = (height+step)+"px";
	else element.style.height = (height-step)+"px";
}

function swap(id) {
	var xmlhttp = getXHR();
	if(!xmlhttp) return;
	
	var div = document.getElementById('presentation');
	div.style.height = (div.offsetHeight-22)+"px";
	div.innerHTML = "";
	timer = setInterval(adapt.curry(div, 0, 10), 1);
	
	xmlhttp.open("GET", id+"_form.php" , true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState != 4) return;
		if(!xmlhttp || xmlhttp.status != '200') window.location.href = id+".php";
		else {
			clearInterval(timer);
			div.innerHTML = xmlhttp.responseText;
			var height = div.style.height;
			div.style.height = "";
			timer = setInterval(adapt.curry(div, div.offsetHeight-22, 10), 1);
			div.style.height = height;
			document.getElementById("form").onchange = check;
			document.getElementById("form").onsubmit = record;
		}
	}
	xmlhttp.send(null);
	return false;
}



function check() {
	if(document.getElementById('hommen').value != '')
		document.getElementById('hommer').checked = true;
	if(document.getElementById('femmen').value != '')
		document.getElementById('femmer').checked = true;
}

Function.prototype.curry = function () {
	var __method = this;
	var args = []; for(var i=0; i<arguments.length; i++) args.push(arguments[i]);
	return function() {
		var args2 = []; for(var i=0; i<arguments.length; i++) args2.push(arguments[i]);
		return __method.apply(this, args.concat(args2));
	};
};

function getXHR() {
	return Try.these(
      function() {return new XMLHttpRequest()},
      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
      function() {return new ActiveXObject('Microsoft.XMLHTTP')}
    ) || false;
}

function record() {
/*
	var xmlhttp = getXHR();
	if(!xmlhttp) { alert('pas glop') ; return; }
	var link = document.getElementById("send");
	xmlhttp.open("POST", "record.php" , true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState != 4) return;
		if(!xmlhttp || xmlhttp.status != '200') link.value = "Erreur :-(";
		else link.value = "C'est noté !";
	}
	xmlhttp.send(null);
*/
	var form = document.getElementById("form");
	if(form.email.value == "") {
		alert("Veuillez indiquer une adresse e-mail.");
		return false;
	}
	return;
}

var Try = {
  these: function() {
    var returnValue;

    for (var i = 0, length = arguments.length; i < length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) { }
    }

    return returnValue;
  }
};

