// JavaScript Document

$(document).ready(initSondage);

function initSondage(){
	if (!$("#zoneSondage").length) return;
	
	if(($.cookie('sondage') == null) || ($.cookie('spip_session')!=null)) {
		//$("#zoneSondage").load("sondage.php", function(){alert("load");});
		$.get("sondage.php?"+Math.round(Math.random()*10000), afficheSondage );
	}
}

function afficheSondage(responseText, textStatus, XMLHttpRequest){
	$("#zoneSondage").html(responseText);
	activeSondage();
}
function activeSondage(){
	$("#zoneSondage")
	$("#sondage ul a").click(onClick);
	$("#sondage a.resultats").click(afficheResultats);
}
function onClick(){
	var nom = $(this).attr("id");
	$("#sondage select option."+nom).attr('selected','selected');
	if ($("#sondage select option."+nom).html()=="autre"){
		afficheAutre();
	} else {
		masqueAutre();
		valideForm();
	}
	return false;
}
function afficheAutre(){
	$("#sondage div.autre").show();
	$("#sondage #ok").click(valideForm);
}
function masqueAutre(){
	$("#sondage div.autre").hide();
}
function valideForm(){
	var selected = $("#sondage select option:selected").val();
	var autre = $("#sondage input#autre").val();

	$.get("sondage-store.php",
		{
			typepublic: $("#sondage select option:selected").val(),
			autre: $("#sondage input#autre").val(),
			success: function (){
				jQuery.cookie('sondage', 'done');
				afficheResultats();
			}
		}
	);
	
	return false;
}
function afficheResultats(){
	$("#sondage").addClass("resultats");
}
