var Voter = new Class({
	
	ajaxURL: 'http://' + window.location.hostname + '/ajax.php/vote/index',
	
	initialize: function(voteTrigger) {	
		
		if (voteTrigger) {
			
			voteTrigger.addEvent('click', function() {
				
				this.associationID = voteTrigger.className.match(/\d+/)[0];
				
				//get entered captcha
				this.captcha = $('votingCaptcha').value;
				
				var jsonRequest = new Request.JSON({url: this.ajaxURL, 
					onSuccess: function(data) {
					
						//check status
						if (data.status == 'success') {
							
							//disable voting
							voteTrigger.setAttribute('id', 'associationNoVote');
							voteTrigger.setAttribute('alt', 'Sie haben bereits für diesen Verein abgestimmt');
							voteTrigger.setAttribute('title', 'Sie haben bereits für diesen Verein abgestimmt');
							voteTrigger.src = voteTrigger.src.replace('vote.', 'vote_grey.');
							voteTrigger.removeEvent('click', function() {});
							
							//increase votes
							votes = $('associationVotes').innerHTML.toInt();
							newVotes = votes + 1;
							$('associationVotes').innerHTML = newVotes;
							
							//check if this was the first vote
							if (newVotes == 1) {
								$('associationNoVotes').setStyle('display', 'none');
								$('associationMin1Vote').setStyle('display', 'block');
							}
							
							//hide captcha-stuff
							$('captchaLine1').setStyle('display', 'none');
							$('captchaLine2').setStyle('display', 'none');
							
						} else if (data.status == 'wrongCaptcha') {				
							
							$('captchaImage').src = '/sfCaptchaGD/getImage?r='+Math.random();
							$('captchaMessage').addClass('error');
							$('votingCaptcha').value = '';
							$('votingCaptcha').addClass('error');
						
						} else {
							
							alert('Keine Stimmberechtigung!');
							
						}
						
					}.bind(this)
				}).get({'associationID': this.associationID, 'captcha': this.captcha});
				
			}.bind(this));	
		}
	
	}
});

window.addEvent('domready', function() {
	new Voter($('associationVote'));
});
