$(document).ready(function(){
	$('.encuesta input[name=email]').lol();
	$('.encuesta input[name=clave]').lol();
	$('.encuesta input[name=clave_visible]').lol();
	
	$('.encuesta div[id^=pregunta_] input:radio').click(function(){
		if (!logeado) $('#boxLoginReserva').slideDown();
	});
	
	$('.encuesta form').submit(function(){
		var parametros = {accion: "login", email: "", clave: ""};
		parametros.email = $(this).find('input[name=email]').val();
		parametros.clave = $(this).find('input[name=clave]').val();
		
		$.ajax({
			data: parametros,
			type: 'POST',
			url: 'includes/funciones_parser.php',
			success: function(response) {
				if (response.estado) {
					cargandov2(false);									
					window.location.href = REQUESTURI;
				}
			}
		});
		
		return false;
	});
	
	$('.encuesta button').click(function(){
		var encuestaBox = $(this).parent().parent().parent().parent();
		var encuestaID 	= encuestaBox.attr('class').replace('encuesta ', '');
		var votos = ''
		
		encuestaBox.find('input:radio:checked').each(function(){
			votos += '&votos[]='+$(this).val();
		});
		
		$.ajax({
			data: 'accion=guardarVotoEncuesta&encuesta='+encuestaID+votos,
			type: 'POST',
			url: 'includes/funciones_parser.php',
			success: function(response) {
				if (response.resultado) {
					$.ajax({
						data: 'accion=getResultadoEncuesta&encuesta='+encuestaID,
						type: 'POST',
						url: 'includes/funciones_parser.php',
						success: function(response) {
							if (response.respuestas.length > 0) {
								encuestaBox.find('ol').remove();
								var salida = '<div id="graph">';
								
								$.each(response.respuestas, function(i, val){
									var porcentaje = Math.round(100 * parseInt(val.cantidad, 10) / parseInt(response.total, 10));
									salida += '	<div class="item"> \
													<strong>'+val.nombre+'</strong> \
													<div> \
														<span><strong>'+porcentaje+'%</strong> '+val.cantidad+' votos</span> \
            											<div class="bar"><img src="img/bar.gif" height="15" width="'+porcentaje+'%" /></div> \
													</div> \
												</div>';
								});
								
								salida += '</div>';
								
								encuestaBox.find('.encuesta_contenido').html(salida);
							}
						}
					});
				}
				cargandov2(false);
			}
		});
	});
});
