// Voting Functions for bigmir2. 
// Author: Andrey Tserkus. (C) SputnikMedia.net
// http://www.bigmir.net

// Constants
VFR_OK = 0;
VFR_ERR_DATA = 1;
VFR_ERR_OPENPROXY = 2;
VFR_ERR_ALREADY = 3;

// Request
var 
	req = false;

function voteDoLoad(voteGroupID, voteID)
{
	var voteGroup = vData[voteGroupID];
	var vote = voteGroup.votes[voteID];
	// Get value
	var name = 'vote_answer_' + voteGroupID + '_' + voteID;
	var formID = 'vote_form_' + voteGroupID + '_' + voteID;
	var form = document.getElementById(formID);
	var e = form.elements;
	var k = e.length;
	var answerID = -1;
	for(var i = 0; i < k; i++)
	{
		if((e[i].name == name) && (e[i].checked))
			answerID = e[i].value;
	}
	if (answerID < 1)
		return false;
	// Send request
	if (!req) {
		req = new JSHttpRequest();
	}
	req.onreadystatechange = function() {
		var voteGroupID = req.responseJS.voteGroupID;
		var voteID = req.responseJS.voteID;
		var answerID = req.responseJS.answerID;
		var voteGroup = vData[voteGroupID];
		var vote = voteGroup.votes[voteID];
		if (req.readyState == 4) {
			var voting = document.getElementById(vote.container);
			if (req.responseJS.resultCode == VFR_OK) {
				for (var i = 0; i < vote.numAnswers; i++) {
					if (vote.answers[i].id == answerID) {
						vData[voteGroupID].votes[voteID].answers[i].voteCount++;
						vData[voteGroupID].votes[voteID].totalVotes++;
						break;
					}
				}
				vData[voteGroupID].votes[voteID].resultHTML = formVotedHTML(voteGroupID, voteID, false, '');
				exchangeVotingContent(voteGroupID, voteID, true);
			} else {
				var errorString = voteGroup.stError;
				switch (req.responseJS.resultCode) {
					case VFR_ERR_DATA:
						errorString = voteGroup.stDataError;
						break;
					case VFR_ERR_OPENPROXY:
						errorString = voteGroup.stOpenProxyError;
						break;
					case VFR_ERR_ALREADY:
						errorString = voteGroup.stAlreadyVoteError;
						break;
				}
				errorString = voteGroup.stVoteNo + ': ' + errorString;
				vData[voteGroupID].votes[voteID].resultHTML = formVotedHTML(voteGroupID, voteID, false, errorString);
				exchangeVotingContent(voteGroupID, voteID, true);
			}
		}
	}
	req.caching = true;
	req.open('GET', vote.jsRequestPath, true);
	req.send({voteGroupID: voteGroupID, voteID: voteID, answerID: answerID, rand: Math.random()});
	return false;
}

// Forms a result HTML for voting
function formVotedHTML(voteGroupID, voteID, addVoteLink, errorString) {
	var result = '';
	var voteGroup = vData[voteGroupID];
	var vote = voteGroup.votes[voteID];
	result = '<div id="answer">';
	result += '<h2 class="header"> ' + voteGroup.stQuestAnswers + ':</h2>';
	result += '<div class="ContentBorder" id="darkgrey">';
	if (errorString != '') {
		result += '<p class="errorElem">' + errorString + '</p>';
	}
	result += '<p id="black">' + vote.title + '</p>';
	var width = 0;
	var percent = 0;
	for (var i = 0; i < vote.numAnswers; i++) {
		if (vote.totalVotes > 0) {
			width = Math.round(vote.answers[i].voteCount * 250 / vote.totalVotes);
			percent = Math.round(vote.answers[i].voteCount * 100 / vote.totalVotes);
		}
		result += vote.answers[i].text + ' <div class="progressBar" id="progress' + i + '" style="width:' + width + 'px;">';
		result += '<span>' + percent + '</span></div>	<div class="filledLine"><spacer></div>';
	}
	result += '<b id="black">' + voteGroup.stTotalAnswered + ' ' + vote.totalVotes + '&nbsp;' + voteGroup.stPpl + '</b> <div class="filledLine"><spacer></div> ';
	result += '<span class="floatRight"><a href="' + vote.archivePath + '" class="medium">' + voteGroup.stArchive + '</a></span>';
	if (addVoteLink)
		result += '<a href="#" onclick="return exchangeVotingContent(' + voteGroupID + ', ' + voteID + ', 0);" title="' + voteGroup.stThisWindow + '" class="floatLeft">' + voteGroup.stAnswerIt + '</a>';
	else 
		result += '&nbsp;';
	result += '<br clear="all"></div>';
	return result;
}

// Excahnges normal html and result html in voting
function exchangeVotingContent(voteGroupID, voteID, showResult) {
	var voteGroup = vData[voteGroupID];
	var vote = voteGroup.votes[voteID];
	var voting = document.getElementById(vote.container);
	if (!vote.resultHTML) {
		vote.resultHTML = formVotedHTML(voteGroupID, voteID, true, '');
	}
	if (!vote.normalHTML)
		vote.normalHTML = voting.innerHTML;
	if (showResult) {
		voting.innerHTML = vote.resultHTML;
	} else {
		voting.innerHTML = vote.normalHTML;
	}
	return false;
}