/************************************************************************************************************
Ajax poller
Copyright (C) 2006  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com
	
************************************************************************************************************/	




var serverSideFile = 'ajax-poller-cast-vote-php.php';
var voteCenterImage = 'images/poll1.gif';

var graphMaxWidth = 180;	// It will actually be a little wider than this because of the rounded image at the left and right
var graphMinWidth = 1;	// Minimum size of graph
var pollScrollSpeed = 5;	// Lower = faster
var txt_totalVotes = 'Nombre total de votes : ';

var ajaxObjects = new Array();
var pollVotes = new Array();
var pollVoteCounted = new Array();
var totalVotes = new Array();
var poller_voted = new Array();		// To know if it a poller was voted or not

/* Preload images */
var preloadedImages = new Array();
preloadedImages[0] = new Image();
preloadedImages[0].src = voteCenterImage;


function showVoteResults(pollId,ajaxIndex)
{
	var xml = ajaxObjects[ajaxIndex].response;
	xml = xml.replace(/\n/gi,'');
	
	var reg = new RegExp("^.*?<pollerTitle>(.*?)<.*$","gi");
	var pollerTitle = xml.replace(reg,'$1');
	
	var parent = document.getElementById('poller_results' + pollId);

	var resultDiv = document.createElement('DIV');
	resultDiv.id = 'result_div' + pollId;
	parent.appendChild(resultDiv);

	var titleP = document.createElement('P');
	titleP.className='pollerTitle';
	titleP.innerHTML = pollerTitle;
	if (poller_voted["' + pollId + '"] == undefined) {
		titleP.innerHTML = '<a href="#ReturnToForm" onclick="returnToPollForm(' + pollId + ');" title="Aller voter">' + pollerTitle + '</a>';
	}
	resultDiv.appendChild(titleP);	

	var options = xml.split(/<option>/gi);
	
	pollVotes[pollId] = new Array();
	totalVotes[pollId] = 0;
	for(var no=1; no<options.length; no++){
		
		var elements = options[no].split(/</gi);
		var currentOptionId = false;
		var option_texte = '';
		for(var no2=0;no2<elements.length;no2++){
			if(elements[no2].substring(0,1)!='/'){
				var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1');
				var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1');
				if(key.indexOf('optionText')>=0){
					option_texte = value;
				}

				if(key.indexOf('optionId')>=0){
					currentOptionId = value/1;
				}
				
				if(key.indexOf('votes')>=0){
					var pOption = document.createElement('P');
					pOption.id ='number_voteTxt' + currentOptionId;
					pOption.className = 'result_pollerOption';
					pOption.innerHTML = option_texte;
					resultDiv.appendChild(pOption);					

					var voteDiv = document.createElement('DIV');
					voteDiv.className='result_pollGraph';
					voteDiv.id = 'result_voteTxt' + currentOptionId;
					resultDiv.appendChild(voteDiv);	
					
					pollVotes[pollId][currentOptionId] = value;					
					totalVotes[pollId] = totalVotes[pollId]/1 + value/1;
				}
			}
		}
	}
	
	var totalVoteP = document.createElement('P');
	totalVoteP.className = 'result_totalVotes';
	totalVoteP.innerHTML = txt_totalVotes + totalVotes[pollId];
	resultDiv.appendChild(totalVoteP);	
	setPercentageVotes(pollId);
	document.getElementById('poller_waitMessage' + pollId).style.display = 'none';
}

function setPercentageVotes(pollId)
{
	for(var prop in pollVotes[pollId]){
		var prc = Math.round( (pollVotes[pollId][prop] / totalVotes[pollId]) * 100);
		var txt = document.getElementById('number_voteTxt' + prop);
		txt.innerHTML += ' <small>(' + prc + '%, ' + pollVotes[pollId][prop] + ' votes)</small>';

		var gph = document.getElementById('result_voteTxt' + prop);
		gph.style.width = Math.max(graphMinWidth, Math.round(prc/100 * graphMaxWidth)) + 'px';
		pollVotes[pollId][prop] = prc;
	}	
}

function returnToPollForm(pollId)
{
	var obj = document.getElementById('poller_results' + pollId);
	var old = document.getElementById('result_div' + pollId);
	obj.removeChild(old);			// Empty poller_results
	document.getElementById('poller_question' + pollId).style.display='block';
}

function castMyVote(pollId, formObj)
{	
	var elements = formObj.elements['vote[' + pollId + ']'];
	var optionId = false;
	for(var no=0; no<elements.length; no++){
		if (elements[no].checked) optionId = elements[no].value;
	}
	if(optionId){
		poller_voted["' + pollId + '"] = true;
		document.getElementById('poller_waitMessage' + pollId).style.display = 'block';
		document.getElementById('poller_question' + pollId).style.display='none';
		var ajaxIndex = ajaxObjects.length;
		ajaxObjects[ajaxIndex] = new sack();
		ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?pollId=' + pollId + '&optionId=' + optionId;
		ajaxObjects[ajaxIndex].onCompletion = function(){ showVoteResults(pollId,ajaxIndex); };	// Specify function that will be executed after file has been found
		ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	
		doFondu("poller_results" + pollId, 0, 0, 100, 100, +10);
	}	
}	

function displayResultsWithoutVoting(pollId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?pollId=' + pollId;
	document.getElementById('poller_waitMessage' + pollId).style.display = 'block';
	document.getElementById('poller_question' + pollId).style.display = 'none';	
	ajaxObjects[ajaxIndex].onCompletion = function(){ showVoteResults(pollId,ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

function alreadyVoted(pollId)
{
	poller_voted["' + pollId + '"] = true;
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?pollId=' + pollId;
	ajaxObjects[ajaxIndex].onCompletion = function(){ showVoteResults(pollId,ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

