/*
  Sapo polls
 */

var
	pollLoaded = false;


// Show a poll

function ShowPoll ( id ) {

	var
		synd = new SAPO.Communication.Syndication(),
		widDiv = document.getElementById("poll_"+id);

	if ( widDiv == null )
		return;

	// Get poll via syndication

	pollSubId = synd.push('http://vota.sapo.pt/polljson.php?id='+id, {
		timeout:	10,

		onComplete:	function (poll,widDiv) {

			var
				voteEl = locChildWithClass(widDiv,'poll_vote'),
				titleEl = locChildWithClass(voteEl,'poll_title'),
				optsEl = locChildWithClass(voteEl,'poll_opts'),
				resultsEl = locChildWithClass(widDiv,'poll_results');

			if ( !voteEl || !titleEl || !optsEl )
				return;

			// Set details

			titleEl.innerHTML = poll.title;

			optsEl.innerHTML = "";
			for ( var x = 0 ; x < poll.answers.length ; x++ ) {
				var
					answer = poll.answers[x],
					optEl = document.createElement("LI");

				optEl.innerHTML = '<input type="radio" name="anof_'+id+'"id="opt_'+answer.id+'" value="'+answer.id+'"><label for="opt_'+answer.id+'">'+answer.answer+'</label>';
				optsEl.appendChild(optEl);
			}

			// Show vote form

			resultsEl.style.display = 'none';
			voteEl.style.display = 'block';


		},
		optOnComplete:	widDiv,

		onTimeout:	function (widDiv) { document.getElementById('pollerror').innerHTML = "Erro: O pedido excedeu o tempo."; },
		optOnTimeout:	widDiv
	} );
	synd.run(pollSubId);

}


// Show poll results

function ShowPollResults ( id ) {

	var
		synd,
		widDiv = document.getElementById("poll_"+id);


	// Get poll via syndication

	synd = new SAPO.Communication.Syndication();
	pollSubId = synd.push('http://vota.sapo.pt/polljson.php?id='+id, {
		timeout:	10,

		onComplete:	function (poll,widDiv) {

			_showPollResults(id,poll,widDiv);

		},
		optOnComplete:	widDiv,

		onTimeout:	function (widDiv) { document.getElementById('pollerror').innerHTML = "Erro: O pedido excedeu o tempo."; },
		optOnTimeout:	widDiv
	} );
	synd.run(pollSubId);

}


// Show poll result set on an element (widDiv)

function _showPollResults ( id, result, widDiv ) {

	var
		voteEl = locChildWithClass(widDiv,'poll_vote'),
		resultsEl = locChildWithClass(widDiv,'poll_results'),
		errorEl = locChildWithClass(resultsEl,'poll_error'),
		titleEl = locChildWithClass(resultsEl,'poll_title'),
		optsEl = locChildWithClass(resultsEl,'poll_opts');

	if ( !voteEl || !resultsEl || !errorEl || !titleEl || !optsEl )
		return;

	// There was an error ? Show it

	if ( result.error ) {
		errorEl.innerHTML = result.error;
		errorEl.style.display = 'block';
	}
	else {
		errorEl.style.display = 'none';
	}

	// Title

	titleEl.innerHTML = result.title;

	// Add options

	optsEl.innerHTML = "";
	for ( var x = 0 ; x < result.answers.length ; x++ ) {
		var
			answer = result.answers[x],
			optEl = document.createElement("LI"),
			perc = Math.round((answer.votes/result.total)*100);

		optEl.className = "poll_resbar";
		optEl.innerHTML = '<span class="bar" style="width:'+perc+'%">&nbsp;</span><span class="perc">&nbsp;'+answer.answer+' ('+perc+'%)</span>';
//		optEl.style.width = perc+'%';
		optsEl.appendChild(optEl);
	}

	// Hide vote form and show results

	voteEl.style.display = 'none';
	resultsEl.style.display = 'block';

}


function VoteForPoll ( id ) {

	var
		synd, inputz, answerId,
		widDiv = document.getElementById("poll_"+id);

	if ( !widDiv )
		return;

	// Find current answear id

	inputz = document.getElementsByTagName("INPUT");
	for ( var x = 0 ; x < inputz.length ; x++ ) {
		var
			input = inputz[x];
		if ( (input.name == "anof_"+id) && (input.checked) )
			answerId = input.value;
	}

	if ( answerId == null )
		return;

	// Subscribe to newsletter via syndication

	synd = new SAPO.Communication.Syndication();
	pollSubId = synd.push('http://vota.sapo.pt/polljson.php?op=vote&id='+id+'&answer='+answerId+'&jsonTag=result', {
		timeout:	10,

		onComplete:	function (result,widDiv) {

			_showPollResults(id,result,widDiv);

		},
		optOnComplete:	widDiv,

		onTimeout:	function (widDiv) { document.getElementById('pollerror').innerHTML = "Erro: O pedido excedeu o tempo."; },
		optOnTimeout:	widDiv
	} );
	synd.run(pollSubId);

}



