var Poll = new Object();

Poll.pollID = 0;
Poll.voteView = '';

Poll.vote = function(voteRadioName)
{
	var answerID = 0;
	var answerRadioButtons = document.getElementsByName(voteRadioName);
	for(var i = 0; i < answerRadioButtons.length; i++)
	{
		if(answerRadioButtons[i].checked)
		{
			answerID = answerRadioButtons[i].value;
			break;
		}
	}
	
	if(answerID == 0)
	{
		alert('Er is geen antwoord ingevoerd.');	
	} else {
		ajaxRequest(Poll.showResults, root + 'internal/services/pollVote.php', 'answerID, ' + answerID + ', number');	
	}
}

Poll.showVoteView = function()
{
	var answersContainer = document.getElementById('poll_' + Poll.pollID + '_radioContainer');
	answersContainer.innerHTML = Poll.voteView;
	
	var voteBtn = document.getElementById('poll_' + Poll.pollID + '_voteButton');
	voteBtn.style.display = 'block';
	
	var resultsBtn = document.getElementById('poll_' + Poll.pollID + '_resultsButton');
	resultsBtn.style.display = 'block';
}


Poll.showResults = function(pollID)
{
	Poll.pollID = pollID;
	ajaxRequest(Poll.renderResults, root + 'internal/services/pollGetVotes.php', 'poll_id, ' + pollID + ', number');
}

Poll.renderResults = function(result)
{
	//Parse the votes
	result = result.split(")")[0];
	
	var votes = new Object();
	var votesArray = result.split("[");

	for(var i = 1; i < votesArray.length; i++)
	{
		var answer = votesArray[i].split("]")[0];
		votes[answer] = votesArray[i].split("=>")[1];
	}
	
	var resultsView = document.createElement('div');
	

	//Count the votes
	var totalVotes = 0;
	for(var i in votes)
		totalVotes += parseInt(votes[i]);
	
	
	//Render the votes
	for(var i in votes)
	{
		resultsView.appendChild(Poll.createVoteResult(i, votes[i], totalVotes));
	}
	
	//Render the voteCount
	var voteCount = document.createElement('div');
	voteCount.setAttribute('class', 'VoteCount');
	voteCount.style.fontSize 	= '10px';
	voteCount.style.position 	= 'relative';
	voteCount.style.left		= '-10px';
	voteCount.style.top			= '20px';
	var voteCountText = document.createTextNode('Aantal stemmen: ' + totalVotes);
	voteCount.appendChild(voteCountText);
	resultsView.appendChild(voteCount);
	
	
	
	//Render the back button
	var backButton = document.createElement('a');
	backButton.setAttribute('onclick', 'Poll.showVoteView()');
	backButton.style.cursor = 'pointer';
	backButton.style.position = 'relative';
	backButton.style.fontSize = '10px';
	backButton.style.top = '20px';
	backButton.style.left = '-10px';
	var backButtonText  = document.createTextNode('Terug');
	backButton.appendChild(backButtonText);
	resultsView.appendChild(backButton);
	
	
	
	
	
	var answersContainer = document.getElementById('poll_' + Poll.pollID + '_radioContainer');
	Poll.voteView = answersContainer.innerHTML;
	answersContainer.innerHTML = '';
	answersContainer.appendChild(resultsView);
	
	var voteBtn = document.getElementById('poll_' + Poll.pollID + '_voteButton');
	voteBtn.style.display = 'none';
	
	var resultsBtn = document.getElementById('poll_' + Poll.pollID + '_resultsButton');
	resultsBtn.style.display = 'none';
}


Poll.createVoteResult = function(answer, votes, totalVotes)
{
	var percentage = Math.round( (parseInt(votes) / parseInt(totalVotes)) * 100);
	var voteResult = document.createElement('div')
	voteResult.setAttribute('class', 'VoteResult');
	voteResult.style.fontSize 	='10px';
	voteResult.style.position 	= 'relative';
	voteResult.style.left 		= '-10px';
	voteResult.style.top		= '20px';

	voteResultAnswer = document.createElement('div');
	voteResultAnswer.setAttribute('class', 'VoteResultAnswer');
	voteResultAnswerText = document.createTextNode(answer);
	voteResultAnswer.appendChild(voteResultAnswerText);
	voteResultAnswer.style.borderTop = '1px solid #ccc';
	voteResultAnswer.style.paddingRight = '30px';
	voteResultAnswer.style.width = '135px';
	voteResult.appendChild(voteResultAnswer);
	
	voteResultPercentage = document.createElement('div');
	voteResultPercentage.setAttribute('class', 'VoteResultPercentage');
	voteResultPercentageText = document.createTextNode(percentage + '%');
	voteResultPercentage.appendChild(voteResultPercentageText);
	voteResultPercentage.style.position = 'absolute';
	voteResultPercentage.style.right 	= '-13px';
	voteResultPercentage.style.top		= '0px';
	voteResult.appendChild(voteResultPercentage);
	
	
	var voteResultBar = document.createElement('div');
	voteResultBar.setAttribute('class', 'VoteResultBar');
	var barWidth = ((160 / 100) * percentage < 2) ? '2px' : (150 / 100) * percentage + 'px';
	
	voteResultBar.style.width 			= barWidth;
	voteResultBar.style.height 			= '7px';
	voteResultBar.style.backgroundColor = '#34738A';
	voteResultBar.style.marginTop	 	= '1px';
	voteResultBar.style.marginBottom 	= '4px';
	voteResult.appendChild(voteResultBar);
		
	return voteResult;
}


/*****************\
 **  A D M I N  **
\*****************/ 

Poll.nrOfAnswers 	= 0;
Poll.defaultValue 	= 'Vul het keuze antwoord in.';

Poll.addAnswer = function(value)
{	
	if(value == '' || value == 'undefined' || value == undefined || value == null)
		value = Poll.defaultValue;
		
	var answersView  = document.getElementById('answersView');
	
	var myLabel = Poll.createLabel('poll_answer_label_' + Poll.nrOfAnswers, 'AnswerLabel', 'Antwoord: ', 'answer[' + Poll.nrOfAnswers + ']');
	var myInput = Poll.createInput('answer[' + Poll.nrOfAnswers + ']', 		'AnswerInput', 'answer[' + Poll.nrOfAnswers + ']', 	value);	
	var deleteBtn = Poll.createDeleteButton(Poll.nrOfAnswers);
	
	myLabel.appendChild(myInput);
	
	var answerContainer = document.createElement('div');
	answerContainer.setAttribute('id', 'answer_' + Poll.nrOfAnswers);
	answerContainer.appendChild(myLabel);
	answerContainer.appendChild(deleteBtn);

	answersView.appendChild(answerContainer);
	
	Poll.nrOfAnswers++;
}

Poll.createLabel = function(labelID, labelClass, labelText, forID)
{
	var myLabel = document.createElement('label');
	myLabel.setAttribute('for', forID);
	myLabel.setAttribute('class', labelClass);
	myLabel.setAttribute('id', labelID);
	
	var labelText = document.createTextNode(labelText);
	myLabel.appendChild(labelText);
	
	return myLabel;
}


Poll.createInput = function(inputID, inputClass, inputName, inputValue)
{
	var myInput = document.createElement('input');
	myInput.setAttribute('class', inputClass);
	myInput.setAttribute('id', inputID);
	myInput.setAttribute('name', inputName);
	myInput.setAttribute('value', inputValue);
	
	return myInput;
}

Poll.createDeleteButton = function(answerID)
{
	var deleteBtn = document.createElement('img');
	deleteBtn.setAttribute('class', 'AnswerDeleteBtn');
	deleteBtn.setAttribute('id', 'answer_delte_button_' + answerID);
	deleteBtn.setAttribute('src', 'website/images/cancel_small_over.png');
	deleteBtn.onclick = function () { Poll.deleteAnswer(answerID); }
	deleteBtn.style.cursor = 'pointer';
	return deleteBtn;
}

Poll.deleteAnswer = function(answerID)
{
	var answersView  = document.getElementById('answersView');
	answersView.removeChild(document.getElementById('answer_' + answerID));
}
