function hidePoll(id) {
	var poll = $("#poll" + id);
	poll.hide();
}

function renderCounter(id, optionId, barImageWidth, ratioPercentage) {
	var pollOptionImage = $("#polloption" + id + "_" + optionId + "_image");
	var pollOptionValue = $("#polloption" + id + "_" + optionId + "_value");

	pollOptionImage.attr("width", Math.round(barImageWidth));
	var roundedPercentage = Math.round(ratioPercentage);
	pollOptionImage.attr("alt", roundedPercentage + "%");
	pollOptionValue.text(roundedPercentage + "%");
}

function renderCounterStep(data) {
	var step = 0.04;
	var nextStep = false;
	for(i = 0; i < data.length; i++) {
		renderCounter(data[i][5], data[i][4], data[i][0], data[i][1]);
		if(data[i][0] < data[i][2]) {
			data[i][0] = data[i][0] + data[i][2] * step;
			if (data[i][0] > data[i][2]) data[i][0] = data[i][2]
			data[i][1] = data[i][1] + data[i][3] * step;
			if (data[i][1] > data[i][3]) data[i][1] = data[i][3]
			nextStep = true;
		}

	}

	if(nextStep) {
		var callbackProxy = function() {
			renderCounterStep(data)
		};
		setTimeout(callbackProxy, 10);
	}
}

function renderPoll(id, width) {
	resetCounters(id);

	var poll = $("#poll" + id);
	var callbackProxy = function(dataFromServer) {
		renderPollOptions(id, dataFromServer);
	};
	poll.show();
	ajaxPoll.getFrontendPoll(id, width, callbackProxy);
}

function resetCounters(id) {
	var pollOptionImage = $("#poll" + id + " img");
	var pollOptionValue = $("#poll" + id + " img span");
	if(pollOptionImage.size() == 0) {
		return false
	}
	else {
		pollOptionImage.attr("width", "0");
		pollOptionImage.attr("alt", "0%");
		pollOptionValue.text("0%");
		return true;
	}
}

function renderPollOptions(id, dataFromServer) {
	var data = new Array();

	$("#poll" + id + "_votes").text(dataFromServer.votes);
	if (dataFromServer.canVote) {
		//$("#poll" + id + " .result").show();
		//$("#poll" + id + " .votetable").show();
		//$("#poll" + id + " .no-votetable").hide();

	var options = dataFromServer.options;
		for(i = 0; i < options.length; i++) {
			var pollData = new Array();
			data[i] = pollData;
			pollData[0] = 0;
			pollData[1] = 0;
			pollData[2] = options[i].barImageWidth;
			pollData[3] = options[i].ratioPercentage;
			pollData[4] = options[i].id;
			pollData[5] = id;
			$("#polloption" + id + "_" + options[i].id + "_votes").text(options[i].votes);
		}
		renderCounterStep(data);

	} else {
	
	//$("#poll" + id + " .result span").show();

	var options = dataFromServer.options;
		for(i = 0; i < options.length; i++) {
			var pollData = new Array();
			data[i] = pollData;
			pollData[0] = 0;
			pollData[1] = 0;
			pollData[2] = options[i].barImageWidth;
			pollData[3] = options[i].ratioPercentage;
			pollData[4] = options[i].id;
			pollData[5] = id;
			$("#polloption" + id + "_" + options[i].id + "_votes").text(options[i].votes);
			$("#poll" + id + " .votetable" + id + "_" + options[i].id).replaceWith("<span>" + $("#poll" + id + " .votetable" + id + "_" + options[i].id).text() + "</span>");
		}
		renderCounterStep(data);
	}
}

function ajaxVote(pollId, voteId, width) {
	var callbackProxy = function(dataFromServer) {
		resetCounters(pollId);
		var globalShowProxy = function() {
			var showProxy = function(dataFromServer) {
				$("#poll" + pollId + " .result span").show();
				$("#poll" + pollId + " .result").fadeIn("slow");
				renderPollOptions(pollId, dataFromServer);
			};
			ajaxPoll.getFrontendPoll(pollId, width, showProxy);
		}
		$("#poll" + pollId).queue(globalShowProxy);
		$("#poll" + pollId + " .votetable").fadeOut("fast");
	};

	ajaxPoll.vote(pollId, voteId, callbackProxy);
	return false;
}