
var starWidth = 0;
var ja_votou = false;

function processReqChange()
{	
	var erroAJAX=0;
	if (xmlhttp.readyState == 1) {
		if(idreq)
			document.getElementById('xmlLoading').innerHTML = '<img src="http://icelular.com.br/images/ajax-loader.gif" />';
			document.getElementById('xmlLoading').className='texto_voto';
	}

    // apenas quando o estado for "completado"
    if (xmlhttp.readyState == 4) {
		
        // apenas se o servidor retornar "OK"
        if (xmlhttp.status ==200) {

        // procura pela div id="pagina" e insere o conteudo
        // retornado nela, como texto HTML
			var resultado = xmlhttp.responseText;
			resultado = resultado.replace(/\+/g," ");
			resultado = unescape(resultado);
            //document.getElementById('pagina').innerHTML = resultado;
			if(resultado!=''){
				dados=resultado.split("|");
				if(dados[0]=='OK'){
					document.getElementById('xmlLoading').innerHTML='Obrigado pelo seu voto!';
					//document.getElementById('rating').innerHTML = dados[3]/2;//rating
					document.getElementById('vote').innerHTML = dados[2];//votos
					starWidth=(dados[3]*20);//resultado
					document.getElementById('xmlLoading').className='result_ok';
				}else
				if(dados[0]=='JA_VOTOU'){						
					document.getElementById('xmlLoading').innerHTML='Você já votou '+(dados[1]=='w'?'nesse Wallpaper':(dados[1]=='d'?'nesse Download':(dados[1]=='i'?'nesse Aparelho':(dados[1]=='e'?'nessa Dica':(dados[1]=='m'?'nessa Matéria':(dados[1]=='v'?'nesse Vídeo':'nessa Oferta'))))))+'!';
					document.getElementById('xmlLoading').className='result_votou';
				}else
				if(dados[0]=='ERRO'){						
					document.getElementById('xmlLoading').innerHTML=dados[1];
					document.getElementById('xmlLoading').className='result_erro';
				}
			}else{
				erroAJAX=1;
			}
        }else{
            erroAJAX=1;
        }
		//document.getElementById('resultAJAX').visibility="none";
		if(erroAJAX==1){
			document.getElementById('xmlLoading').innerHTML='Falha na votação. Tente mais tarde!';            
			document.getElementById('xmlLoading').className='result_erro';
		}
    }
	ja_votou=false;//resposta do clique
}

function grava_voto(tipo,id,voto){
	url="/votos_ajax.php?tipo="+tipo+"&id="+id+"&voto="+voto;
	objetoXML();

	if (xmlhttp==null){
		alert ("Este browser não suporta HTTP Request");
		return false;
	}
	idreq=id;

	xmlhttp.onreadystatechange = processReqChange;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

function Voting(tipo,rate,id)
{
	if(!ja_votou){
	  ja_votou=true;//bloqueia clique do voto
	  grava_voto(tipo,id,rate);
	}
}

function Unset_Event(prefix)
{
	for (i=1; i<=5; i++){
		id = prefix + i;
		document.getElementById(id).onclick = Return_False;
	}
}

function Return_False()
{
	return false;
}

var alertTimerId = 0;

function Star_Visibility()
{
	var starBar_el = document.getElementById('starbar');
	
	if (starBar_el.style.visibility == 'hidden')
	{
		starBar_el.style.width = '0%';
		starBar_el.style.visibility = 'visible';
		alertTimerId = setTimeout("Fade_In()",5);
	}
	else
	{
		starBar_el.style.visibility = 'hidden';
		clearTimeout (alertTimerId);
	}
}

function Fade_In()
{
	var i;
	var starBar_el = document.getElementById('starbar');
	
	i = starBar_el.style.width;
	i = parseInt(i.substring(0, i.length-1));
	i += starWidth/10;
	starBar_el.style.width = i + '%';
	
	if (i>=starWidth)
	{
		clearTimeout (alertTimerId);
		return;
	}
	else
		alertTimerId = setTimeout("Fade_In()",5);
}


