// JavaScript Document
jsEncuesta = Object({
	idMunicipio : "",
	idEncuesta : "",
	contenedorEncuesta : "",
	urlAgregarVoto : "/municipio/ajax/encuesta/agregarvoto.php",
	urlObtenerResultados : "/municipio/ajax/encuesta/resultados.php",
	
	inicio : function(){
		var e = this;
		$("#btnEncuestaVotar").click(function(){
			$(this).attr("disabled", true);
			var mensaje = "";
			mensaje += (Validar.EsEntero($("input[name=Respuesta]:checked").val()))? "" : "- Seleccione una opción";
			
			if(mensaje == ""){
				var respuestaSeleccionada = $("input[name=Respuesta]:checked").val();
				var anchoContenedor = $(e.contenedorEncuesta).height();
				

				$(e.contenedorEncuesta).animate({
					opacity: 0
					}, 500, "",
					function(){
						$(e.contenedorEncuesta).html('<div style="display:block; height:'+ anchoContenedor +'px; background-image:url(/images/ajax-loader.gif);  background-repeat:no-repeat; background-position:center"></div>');
						if(e.AgregarVoto(respuestaSeleccionada)){
							e.ObtenerResultados();					
						}else{
							alert("Error");	
						}
					}
				).animate({opacity : 1}, 500);
								
			}else{
				alert(mensaje);	
				$(this).attr("disabled", false);
			}
		});
	},
	
	
	AgregarVoto : function(opcion){
		var e = this;
		var respuesta = false;
		$.ajax({
			url : e.urlAgregarVoto,
			async : false,
			type : "POST",
			data : "IdMunicipio="+ e.idMunicipio +"&IdEncuesta=" + e.idEncuesta + "&Respuesta="+opcion,
			success : function(resp2){
				if(resp2.indexOf("hecho") != -1){									
					respuesta = true;
				}
			}					
		});		
		return respuesta;
	},
	
	ObtenerResultados : function(){
		var e = this;
		
		$.ajax({
			url : e.urlObtenerResultados,
			async : true,
			type : "POST",
			data : "IdMunicipio="+ e.idMunicipio,
			success : function(html){
				if(html != ""){
					if(e.contenedorEncuesta != ""){					
						
						$(e.contenedorEncuesta).animate({
							opacity: 0
							}, 500, "",
							function(){								
								$(e.contenedorEncuesta).html(html);
							}
						).animate({opacity : 1}, 500);
						
						
					}
				}
			}					
		});	

	}
});
