var PlusMinusVoting = {

	url: 'services/PlusMinusVote.php',
	
	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);
				
				try
				{
					$('voting-div').update(_da['html']);
					$('voting-msg').update(_da['message']);
				}
				catch (error) {}
				//alert(transport.responseText);
			},
			onFailure: function() {alert("Chyba !");}
		});
	}	
};

var RatingVoting = {

	url: 'services/RatingVote.php',
	
	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 clanky
				try
				{
					$('voting-div').update(_da['html']);
					$('voting-msg').update(_da['message']);
					RatingVoting.loadEvents();
				}
				catch (error) {}
				
				// pre diskusiu
				try
				{
					$('discussion-voting'+resultId).update(_da['html']);
					$('discussion-voting-msg'+resultId).update(_da['message']);
					DiscussionVoting.loadEvents();
				}
				catch (error) {}
				
				// updatovanie volnych bodov
				try
				{
					$$('.tx_users_today_available').each( function(el) { el.update(_da['available_votes']); });					
				}
				catch(error) {}
				
				//alert(transport.responseText);
			},
			onFailure: function() {alert("Chyba !");}
		});
	},
	
	loadEvents: function()
	{
		$$('.plus').each( function(el) {
			el.observe('mouseover', function(event) {
				el = Event.element(event);
				if (el.hasClassName('plus'))
				{	
					el.addClassName('on');
					el.removeClassName('off');
				}			
				
				el.up().previousSiblings().each(function(e) {
					if (e.down().hasClassName('plus'))
					{
						e.down().addClassName('on');
						e.down().removeClassName('off');
					}
				});
			});
			
			el.observe('mouseout', function(event) {
				el = Event.element(event);
				if (el.hasClassName('plus'))
				{	
					el.removeClassName('on');
					el.addClassName('off');
				}			
				el.up().previousSiblings().each(function(e) {
					if (e.down().hasClassName('plus'))
					{
						e.down().removeClassName('on');
						e.down().addClassName('off');
					}
				});
			});
		});
		
		$$('.minus').each( function(el) {
			el.observe('mouseover', function(event) {
				el = Event.element(event);
				if (el.hasClassName('minus'))
				{	
					el.addClassName('on');
					el.removeClassName('off');
				}			
				
				el.up().previousSiblings().each(function(e) {
					if (e.down().hasClassName('minus'))
					{
						e.down().addClassName('on');
						e.down().removeClassName('off');
					}
				});
			});
			
			el.observe('mouseout', function(event) {
				el = Event.element(event);
				if (el.hasClassName('minus'))
				{	
					el.removeClassName('on');
					el.addClassName('off');
				}			
				el.up().previousSiblings().each(function(e) {
					if (e.down().hasClassName('minus'))
					{
						e.down().removeClassName('on');
						e.down().addClassName('off');
					}
				});
			});
		});		
	}
};

var UserVoting = Object.clone(RatingVoting);
UserVoting.url = 'services/UserVote.php';

var DiscussionVoting = Object.clone(RatingVoting);
DiscussionVoting.url = 'services/DiscussionVote.php';

DiscussionVoting.loadEvents = function()
{
	// diskusia
	$$('.post-vote-plus').each( function(el) {
		
		el.observe('mouseover', function(event) {
			el = Event.element(event);
			if (el.hasClassName('post-vote-enabled'))
			{	
				el.addClassName('on');
			}			
			el.previousSiblings().each(function(e) {
				if (e.hasClassName('post-vote-plus') && e.hasClassName('post-vote-enabled'))
				{
					e.addClassName('on');
				}
			});
		});

		el.observe('mouseout', function(event) {
			el = Event.element(event);
			if (el.hasClassName('post-vote-enabled'))
			{	
				el.removeClassName('on');
			}			
			el.previousSiblings().each(function(e) {
				if (e.hasClassName('post-vote-plus') && e.hasClassName('post-vote-enabled'))
				{
					e.removeClassName('on');
				}
			});
		});		
	});
	
	$$('.post-vote-minus').each( function(el) {		
		el.observe('mouseover', function(event) {
			el = Event.element(event);
			if (el.hasClassName('post-vote-enabled'))
			{	
				el.addClassName('on');
			}			
			el.previousSiblings().each(function(e) {
				if (e.hasClassName('post-vote-minus') && e.hasClassName('post-vote-enabled'))
				{
					e.addClassName('on');
				}
			});
		});

		el.observe('mouseout', function(event) {
			el = Event.element(event);
			if (el.hasClassName('post-vote-enabled'))
			{	
				el.removeClassName('on');
			}
			el.previousSiblings().each(function(e) {
				if (e.hasClassName('post-vote-minus') && e.hasClassName('post-vote-enabled'))
				{
					e.removeClassName('on');
				}
			});
		});		
	});
};

Event.observe(window, 'load', function() {
	
	RatingVoting.loadEvents();	
	
	DiscussionVoting.loadEvents();
		
});



