// JavaScript Document

/*$(function() {
	$("#progressbar").progressbar({
		value: 0
	});
		$("#progressbar2").progressbar({
		value: 0
	});
		$("#progressbar3").progressbar({
		value: 0
	});
		$("#progressbar4").progressbar({
		value: 0
	});
		$("#progressbar5").progressbar({
		value: 0
	});
});


$(document).ready(function() {
  $("#progressbar > div").removeClass("ui-widget-header");
  $("#progressbar > div").addClass("ui-widget-header-azul");
  
  $("#progressbar2 > div").removeClass("ui-widget-header");
  $("#progressbar2 > div").addClass("ui-widget-header-verde");
  
  $("#progressbar3 > div").removeClass("ui-widget-header");
  $("#progressbar3 > div").addClass("ui-widget-header-laranja");

  $("#progressbar4 > div").removeClass("ui-widget-header");
  $("#progressbar4 > div").addClass("ui-widget-header-vermelho");
  
  $("#progressbar5 > div").removeClass("ui-widget-header");
  $("#progressbar5 > div").addClass("ui-widget-header-amarelo");
  
  crescebarra(50, "progressbar", 100);
  crescebarra(30, "progressbar2", 100);
  crescebarra(80, "progressbar3", 100);
  crescebarra(10, "progressbar4", 100);
  crescebarra(90, "progressbar5", 100);
});*/



function crescebarra(fim, barra, tempo) 
{
		//alert(barra + " - "+ fim);
		var value = $("#"+barra).progressbar('option', 'value');
		var novovalue = parseInt(value)+1;
		$("#"+barra).progressbar('option', 'value', novovalue);
		if(fim>=value)
		{
      	 	setTimeout(function() {crescebarra(fim, barra, tempo);}, tempo);
		}		
}

function Votar(inquerito)
{       
	var nomepoll = "inq"+inquerito;
	
	if($("[name="+nomepoll+"]:checked").length==0)
		return;
	
	var opcao = $("[name="+nomepoll+"]:checked").val();
	var contentor = "#cont_inq_"+inquerito;
	document.cookie = 'inq_'+inquerito;
	
	var dados = "inquerito="+inquerito+"&opcao="+opcao;
		
	$.ajax({
	url:"includes/votar.php",
	type:"post",
	data: dados,
	success: function(response, statusCode){
		var arrayDoPhp=eval(response);
		//posicao 0 tem respostas
		//posicao 1 tem valores
		var soma = 0;
        var maiorPerc=0;
		for(i=0; i<arrayDoPhp[1].length; i++)
		{
			soma = parseInt(soma) + parseInt(arrayDoPhp[1][i]);
		}
		var html ='<table width="100%">';
		for(i=0; i<arrayDoPhp[0].length; i++){
            var tamanho = Math.round(parseInt(arrayDoPhp[1][i])/soma*100);
            html=html+'<tr><td width="40%" class="txtmedio_333">'+arrayDoPhp[0][i]+'</td><td width="50%"><div id="progressbar_'+inquerito+'_'+i+'"></div></td><td width="10%" class="txtmedio_666" align="right">'+tamanho+'%</td></tr>';
            if(parseInt(arrayDoPhp[1][i])>parseInt(arrayDoPhp[1][maiorPerc]))
                maiorPerc=i;
        }
		 html=html+'<tr><td colspan="3" class="txtmedio_666" align="right">Total de votos: '+soma+'</td></tr></table>';
		$(contentor).html(html);
		$(contentor).ready(function() {						
			
            for(i=0; i<arrayDoPhp[1].length; i++)
			{
				var p = 'progressbar_'+inquerito+'_'+i;
                $("#"+p).progressbar({
						value: 0
				});	
                
                /*$("#"+p+" > div").removeClass("ui-widget-header");
                
                if(i==maiorPerc)
                    $("#"+p+" > div").addClass("ui-widget-header-azul");
                else
                    $("#"+p+" > div").addClass("ui-widget-header-amarelo");*/

				var tamanho = Math.round(parseInt(arrayDoPhp[1][i])/soma*100);
				crescebarra(tamanho, p, 100);
                
			}						
		});
	},
	error: function(XMLHttpRequest, textStatus, errorThrown){}
	});

	
}

function ChamarVotos(inquerito)
{       
	var nomepoll = "inq"+inquerito;
	var contentor = "#cont_inq_"+inquerito;
	var dados = "inquerito="+inquerito;
		
	$.ajax({
	url:"includes/votos.php",
	type:"post",
	data: dados,
	success: function(response, statusCode){
		var arrayDoPhp=eval(response);
		//posicao 0 tem respostas
		//posicao 1 tem valores
		var soma = 0;
        var maiorPerc=0;
		for(i=0; i<arrayDoPhp[1].length; i++)
		{
			soma = parseInt(soma) + parseInt(arrayDoPhp[1][i]);
		}
		var html ='<table width="100%">';
		for(i=0; i<arrayDoPhp[0].length; i++){
            var tamanho = Math.round(parseInt(arrayDoPhp[1][i])/soma*100);
            html=html+'<tr><td width="40%" class="txtmedio_333">'+arrayDoPhp[0][i]+'</td><td width="50%"><div id="progressbar_'+inquerito+'_'+i+'"></div></td><td width="10%" class="txtmedio_666" align="right">'+tamanho+'%</td></tr>';
            if(parseInt(arrayDoPhp[1][i])>parseInt(arrayDoPhp[1][maiorPerc]))
                maiorPerc=i;
        }
		 html=html+'<tr><td colspan="3" class="txtmedio_666" align="right">Total de votos: '+soma+'</td></tr></table>';
		$(contentor).html(html);
		$(contentor).ready(function() {						
			
            for(i=0; i<arrayDoPhp[1].length; i++)
			{
				var p = 'progressbar_'+inquerito+'_'+i;
                $("#"+p).progressbar({
						value: 0
				});	
                
                //$("#"+p+" > div").removeClass("ui-widget-header");
                
                //if(i==maiorPerc)
                    //$("#"+p+" > div").addClass("ui-widget-header-azul");
                //else
                    //$("#"+p+" > div").addClass("ui-widget-header-amarelo");

				var tamanho = Math.round(parseInt(arrayDoPhp[1][i])/soma*100);
				crescebarra(tamanho, p, 100);
                
			}						
		});
	},
	error: function(XMLHttpRequest, textStatus, errorThrown){}
	});

	
}