/* ------------------------------------------------------------------------------------------- 
--	ENCUESTA CBVALLADOLID.ES		-------------------------------------------------------------
--	Realizacion: digival.es 		-------------------------------------------------------------
--	joseignacio.marcos@digival.es -------------------------------------------------------------
------------------------------------------------------------------------------------------- */

var encuesta = new Class({

	initialize: function() {		
		
		this.contEncuesta			=	$('resultadosEnc'); //en el css se le debe de aplicar un ancho
		this.formulario 			=	$('encuestaForm'); 
		this.barraPorcentaje		=	$$('.barraPorcentaje');
		this.anchosBarras 		=	[];
		
	},
	
	start: function(){
			
		if(this.formulario){ //Si hay formulario cargo el evento del onsubmit para llamar al XMLHttpRequest 
		
			this.formulario.addEvent('submit',function(e){
				e = new Event(e).stop();			
				this.efectoContEncuesta = new Fx.Tween(this.contEncuesta	, {duration: 300});	//preparo el efecto del contenedor	
				this.formulario.set('send', {onComplete: function(reponseText, responseXML){ this.respuestaXML = responseXML; this.vaciarCaja(); }.bind(this) });
				this.formulario.send();	
			}.bind(this));
		
		}else{ //si no hay formulario es que estamos viendo los resultados de las encuestas con lo que hago el efecto de las barras

			this.barraPorcentaje.each(function(el, i){
				// el numero del porcentaje * el ancho de la encuesta dividido entre 100
				this.anchosBarras[i] = (el.get('text').toFloat() * this.contEncuesta.getFirst().getStyle('width').toInt())/100;
				el.set('tween', {duration: 1000});
				el.tween('width', [0, this.anchosBarras[i]]);
			}.bind(this));
		
		}
	}, 
	
	//oculto el contenedor, lo vacio y proceso el XML
	vaciarCaja: function(){
		
		this.efectoContEncuesta.start('opacity', [1,0]).chain(function(){
			
			this.contEncuesta.empty();
			this.procesaXML();
			
		}.bind(this));
		
	}, 
		
	procesaXML: function(){
		
		var totalVotos = this.respuestaXML.getElementsByTagName('totalVotos')[0].firstChild.nodeValue;
	  	var votos =  [];
		var porcentaje =  [];
		var respuestas =  [];
		
		for(var i=0; i<this.respuestaXML.getElementsByTagName('respuesta').length; i++){
			
			respuestas[i] 	= this.respuestaXML.getElementsByTagName('titrespuesta')[i].firstChild.nodeValue;
			votos[i] 		= this.respuestaXML.getElementsByTagName('votos')[i].firstChild.nodeValue;
			porcentaje[i] 	= this.respuestaXML.getElementsByTagName('porcentaje')[i].firstChild.nodeValue;
	
			this.contEncuesta.adopt(new Element('p',{'class': 'sinMargen'}).set('html','<strong>'+respuestas[i]+'</strong> - <em>'+votos[i]+' votos</em>'));
			this.contEncuesta.adopt(new Element('div',{'class': 'contPorcentaje','id':'votos'+i}));
			
			var ancho = (porcentaje[i]*this.contEncuesta.getFirst().getStyle('width').toInt())/100;
			$('votos'+i).adopt(new Element('p',{'styles':{'width':'0px'}, 'class': 'barraPorcentaje','id':'intvotos'+i}).set('html','<strong>'+porcentaje[i]+'%</strong>'));
					
			$('intvotos'+i).set('tween', {duration: 1000});
			$('intvotos'+i).tween('width', [0,ancho]);
		}
		
		this.contEncuesta.adopt(new Element('p').set('html', '<em>N&uacute;mero total de votos: '+totalVotos+'</em>'));
		this.contEncuesta.adopt(new Element('p').set('html','<a href="encuestas.php?seccion=5" title="ver todas las encuestas" class="botonA boton150 centrado"><strong>Ver todas las encuestas</strong></a>'));
		
		this.efectoContEncuesta.start('opacity', [0,1]);

	}
	
});

function InitEncuesta(){
	
	var enc = new encuesta().start();
}
/* -----------------------------------------------------------------------------
LOAD ---------------------------------------------------------------------------
----------------------------------------------------------------------------- */ 
window.addEvent('domready', InitEncuesta);