/*
Copyright by Arturas Piksrys(arturas@ring.lt) 2005
*/
function JokesVotingController(){
		
		this.server = new Ajax();
		var url = 'http://fun.ring.lt/jokespage/index.php';
		
		
		
	this.addVote = function(vote, joke_id){
		
		this.toggleVotingInputs(true);
		this.vote_span = document.getElementById("votes_" + joke_id); 
		this.rating_span = document.getElementById("rating_" + joke_id); 
		this.joke_div = document.getElementById("joke_" + joke_id); 
		this.content = this.joke_div.innerHTML;
		
		this.server.setServerUrl(url);
		
		this.server.addParams('processor', 'Jokes');
		this.server.addParams('do', 'addVote');
		this.server.addParams('vote', vote);
		this.server.addParams('joke_id', joke_id);
		
		this.beforeCall();
		
		this.server.call(this);
	}
	
	this.afterCall = function(response){
		
		if(response.responseText == "666") {
			this.showMsg("You've alreadey voted for this joke!", 2);
			return false;
		}
		
		this.showMsg("Thank you for your vote!", 2);
		
		var result = response.responseText.split(',');
		this.vote_span.innerHTML = result[1];
		this.rating_span.innerHTML = result[0];
	}
	
	
	this.beforeCall = function(){
		this.showMsg("Voting...", 0);
	}
	
	
	this.restoreText = function(){
		this.joke_div.style.height = null;
		this.joke_div.innerHTML = this.content;
	}
	
	this.toggleVotingInputs = function(state)
	{
		var inputs = document.getElementsByName("vote");
		for(var i=0; i < inputs.length; i++)
		{
			inputs[i].disable = state;
		}
	}
	
	this.showMsg = function(text, delayInSec){
		var self = this;
		
		this.joke_div.style.height = "75px";
		
		if(delayInSec > 0){
			this.jokeDivBox(text);
			window.setTimeout(function evals( e ) { self.restoreText(); }, delayInSec * 1000);
		}
		else{
			this.jokeDivBox(text);
		}
		
		
		
	}
	
	
	
	this.jokeDivBox = function(text){
		this.joke_div.innerHTML = '<div class="dialogSpace"><div class="dialogBox">'+ text +'</div></div>';
	}

}

function vote(vote, joke_id)
{
	var JVC = new JokesVotingController();
	JVC.addVote(vote, joke_id);
}


