/**************************************************************************
 * API pro hlasovani
 */

function v(r)
{
	alert(r);
	return r;
}
function d(o)
{
	var s = '';
	for(var i in o) s += i + ': ' + o[i] + '\n';
	return s;
}


var VoteAPI = {
        voteAction:   new I4.Ajax.Message(I4.Ajax.JSONResponse, '/smallapp/voter/vote/').withPOSTParams('content_id', 'points')
        ,
        pointsAction: new I4.Ajax.Message(I4.Ajax.JSONResponse, '/smallapp/voter/points/').withPOSTParams('content_id')
};


function setDisplay(id, disp)
{
	document.getElementById(id).style.display = disp;
}


function Voter(contentId)
{

	this.vote = function vote(points)
	{
		this.showMessage(VoteAPI.voteAction(contentId, points));
		this.getPoints();
	}

	this.getPoints = function getPoints()
	{
		this.showPoints(VoteAPI.pointsAction(contentId));
	}

	this.showPoints = function showPoints(response)
	{
		if(response.points.celkem)
			with(document.getElementById('celkem' + contentId))
				replaceChild(document.createTextNode(response.points.celkem), firstChild);
		if(response.points.pocet)
			with(document.getElementById('pocet'  + contentId))
				replaceChild(document.createTextNode(response.points.pocet),  firstChild);
		if(response.points.prumer)
			with(document.getElementById('prumer' + contentId))
				replaceChild(document.createTextNode(response.points.prumer), firstChild);
	}

	this.getPoints();

}



Voter.prototype.showMessage = function showMessage(response)
{
	var messageElement = document.createElement('div');
	messageElement.setAttribute('class', 'voter-message');
	messageElement.appendChild(document.createTextNode(response.message));
	var voter = document.getElementById('voter'+response.uuid);
	voter.parentNode.replaceChild(messageElement, voter);
}


