// ************************************************************** //
// * Au dela de l'Oraison                                       * //
// ************************************************************** //
// * Copyright (c) 2007-2008                                    * //
// * Web           http://www.samantha-bailly.com               * //
// ************************************************************** //
// ************************************************************** //
// * $ Date: 2007-10-06 19:50:46 +0200 (Sat, 06 Oct 2007)     $ * //
// * $ Developper: Dietrich Christophe                        $ * //
// * $ Developper: DESPORTES Florian                          $ * //
// ************************************************************** //
// ************************************************************** //
// * $ Source: script_roman.js                                $ * //
// * $ Source type: javascript with mootools framework        $ * //
// * $ Works with: includes/roman.inc.php                     $ * //
// ************************************************************** //


window.addEvent("domready", function() {

	$$("select").each( function(select) {
		select.addEvent("change", function(evt) {
			evt.preventDefault();
			sendRequest(select.getParent());
			return false;
		});
	});
	
});


function reset_forms() {
	$$("select").each( function(select) {
		select.options[0].selected = select.options[0];
	});
}


function sendRequest(formulaire) {
	var req_form = new Request.JSON({
		url  : formulaire.action,
		data : formulaire.toQueryString()
	});
	
	req_form.addEvent("request", function(e) { waitResponse(); });
	req_form.addEvent("success", function(tag, text) {
		if( showResponse(tag.answer) ) {
			update_libs(tag.answer);
		} else {
			update_none(tag.answer);
		}
		reset_forms();
	});
	req_form.addEvent("failure", function(xhr) { failureResponse(xhr); });
	req_form.send();
}


function waitResponse() {
	$("roman_lieu").set("html", "Envoi des donn&eacute;es...");
}


function failureResponse(xhr) {
	$("roman_lieu").set("html", "Une erreur du serveur est survenue. Merci de recommencer");
}


function showResponse(evtTag) {
	new Fx.Morph($("roman_lieu")).start({
		"opacity" : [ 0 , 1 ]
	});
	var estOK = ( evtTag.get == "OK" ? true : false );
	return estOK;
}


function update_libs(tags) {
	/*
	Create a new element which looks like
	<div class="roman_lieu">
		<h3>Les librairies de NOM_VILLE</h3>
		<span>NOM_LIBRAIRIE</span><br/>
		[, <span>NOM_LIBRAIRIE</span><br/>, [...]]
	</div>
	*/
	$("roman_lieu").set("html", "");
	
	var divRoman        = $("roman_lieu");
	var newLibHTrois    = new Element("h3", { styles : {
		"text-align" : "center"
	}});
	newLibHTrois.set("html", tags.reason);

	divRoman.adopt(newLibHTrois);

	tags.list.each(function(item) {
		var texte = "# <u>"+ item.nom +"</u>, "+ item.rue +", "+ item.codePostal +" "+ item.ville;
		var newLibSpanTexte = new Element("span");
		newLibSpanTexte.set("html", texte);
		divRoman.adopt(newLibSpanTexte);
		divRoman.adopt(new Element("br"));
	});

}


function update_none(tags) {
	$("roman_lieu").set("html", "");
	
	var divRoman        = $("roman_lieu");
	var newLibHTrois    = new Element("h3", { styles : {
		"text-align" : "center"
	}});
	newLibHTrois.set("text", tags.reason);
	divRoman.adopt(newLibHTrois);
}
