var PlusMinusVoting = Class.create({

	url: 'services/PlusMinusVote.abc',
	
	plus: function()
	{
		this.vote(1);
	},
	
	minus: function()
	{
		this.vote(-1);
	},
	
	vote: function(voteValue)
	{
		$('voteForm').voteValue.value=voteValue;
		
		new Ajax.Request(this.url,{
			method: 'post',
			parameters: $('voteForm').serialize(true),
			onSuccess: function(transport)
			{
				eval(transport.responseText);
				
				$('voting-div').update(_da['html']);
				$('voting-msg').update(_da['message']);
				//alert(transport.responseText);
			}
		});
	}	
});

var RatingVoting = {

	url: 'services/RatingVote.php',
	
	/**
	 * Prida hlas do hlasovania s id <i>resultId</i> s hodnotou <i>voteValue</i> 
	 *
	 */
	vote: function(resultId, voteValue)
	{
		$('voteForm'+resultId).voteValue.value=voteValue;
		
		new Ajax.Request(this.url,{
			method: 'post',
			parameters: $('voteForm'+resultId).serialize(true),
			onSuccess: function(transport)
			{
				eval(transport.responseText);				
				// pre diskusiu
				try
				{
					$('post-vote-'+resultId).update(_da['html']);
					DiscussionVoting.loadEvents();
				}
				catch (error) {}
			},
			onFailure: function() {alert("Chyba !");}
		});
	},
	
	loadEvents: function()
	{
		$$('.lminus a.vote').each( function(el) {
			el.observe('mouseover', function(event) {
				el = Event.element(event);
				var highlight = 20*(1+el.nextSiblings().size())
				el.up('.post-vote').down('div.minus').down('span').setStyle({
					'width' : highlight+'px'
				});
			});
			el.observe('mouseout', function(event) {
				el = Event.element(event);
				el.up('.post-vote').down('div.minus').down('span').setStyle({
					'width' : '0px'
				});
			});
		});
		
		$$('.lplus a.vote').each( function(el) {
			el.observe('mouseover', function(event) {
				el = Event.element(event);
				var highlight = 20*(1+el.previousSiblings().size())
				el.up('.post-vote').down('div.plus').down('span').setStyle({
					'width' : highlight+'px'
				});
			});
			el.observe('mouseout', function(event) {
				el = Event.element(event);
				el.up('.post-vote').down('div.plus').down('span').setStyle({
					'width' : '0px'
				});
			});
		});

	}
};

Event.observe(window, 'load', function() {

	RatingVoting.loadEvents();	
	
});
