votesRefresher = null;
voteEnabled = true;
voteGlobalId = null;
if (!voteBase) {
	var voteBase = 'plman/';
}

function vote(id) {
	datos = 'action=vote&id=' + id;
	makeHttpRequest(voteBase + 'js.vote', '_vote', false, datos);
}

function _vote(data) {
	clearInterval(votesRefresher);
	refreshVotes();
	votesRefresher = setInterval('refreshVotes();', 10000);
}

function refreshVotes() {
	makeHttpRequest(voteBase + 'js.vote?action=list', '_refreshVotes');	
}

function _refreshVotes(data) {
	lines = data.split("\n");
	
	if (lines[0] == 'Error') {
		return false;	
	}
	
	var container = document.getElementById('temasVoto');
	container.innerHTML = '';
	
	if (lines[0] == 'canVote') {
		voteEnabled = true;
	}
	else if (lines[0] == 'noVote'){
		container.innerHTML = "Vota por tu tema favorito en el espacio de 100% interactivo, y si es el mas votado, escuchalo!!";
		return true;
	}
	else {
		voteEnabled = false;	
	}
	
	var table = document.createElement('table');
	
	for (i=1; i <lines.length; i++) {
		tema = lines[i].split('<!>');
		
		infoTema = tema[1].split('-');
		if (infoTema.length >= 2) {			
			nombreTrim = infoTema[1].replace(/^\s+|\s+$|.mp3/gi,"");
			actorTrim = infoTema[0].replace(/^\s+|\s+$|.mp3/gi,"");
		}
		else {
			nombreTrim = infoTema[0].replace(/^\s+|\s+$|.mp3/gi,"");
			actorTrim = '';
		}
		
		nombreTrim = nombreTrim.replace(/\w+/g,
			function(a){
        		return a.charAt(0).toUpperCase() + a.slice(1).toLowerCase();
			}
		);

		actorTrim = actorTrim.replace(/\w+/g,
			function(a){
        		return a.charAt(0).toUpperCase() + a.slice(1).toLowerCase();
			}
		);
		
		//div = document.createElement('div');
		//div.className = 'cuadroTema';

		//span = document.createElement('span');
		//span.idVoto = tema[0];
		//span.style.cssFloat = 'right';
		//span.style.styleFloat = 'right';
		
		//span.className = 'porcentajeNumero';
		myRow = table.insertRow(-1);
		myCell = myRow.insertCell(-1);
		
		if (voteEnabled) {
			var option = document.createElement('input');
			option.setAttribute('type', 'radio');
			option.name = 'voter100i';
			option.idVoto = tema[0];
			option.onclick = function() {
				voteGlobalId = this.idVoto;
			}
			myCell.appendChild(option);
		}
		else {
			myCell.innerHTML = Math.floor(parseInt(tema[2], 10)).toString() + ' %';
			myCell.style.cursor = 'default';
		}
		
//		div.appendChild(span);

		myCell = myRow.insertCell(-1);
		
		//span = document.createElement('div');
		//span.className = 'nombreTema';
		myCell.innerHTML = actorTrim + ' - ' + nombreTrim;
		//div.appendChild(span);

//		span = document.createElement('div');
//		span.className = 'autorTema';
//		span.innerHTML = 'de &ldquo;' + actorTrim + '&rdquo;';
//		div.appendChild(span);


//		container.appendChild();
	}
	container.appendChild(table);
	var div = document.createElement('div');
	div.style.textAlign = 'center';
	
	var boton = document.createElement('input');
	boton.setAttribute('type', 'button');
	boton.value = 'Votar';
	boton.className = 'button';
	boton.onclick = function() {
		vote(voteGlobalId);
	}
	
	div.appendChild(boton);
	container.appendChild(div);
}
