var VotingHandler = new Class({
	
	ajaxURL: 'http://' + window.location.hostname + ':' + window.location.port + '/ajax.php/vote/index',
	associationId: 0,
	activeStars: 0,
	hoverStars: false,
	votingIsEnabled: false,
	stars: false,
	
	initialize: function(stars, associationId) {
		
		this.stars = stars;
	
		//check if voting is enabled
		if ($('captchaContainer')) {
			this.votingIsEnabled = true;
			this.hoverStars = true;
			//reset form fields
			$('votingRating').setProperty('value', 0);
			$('votingCaptcha').setProperty('value', '');
			//get association identifier
			this.associationId = associationId;
		}
	
		stars.each(function(star) {
			//hover stars
			star.addEvent('mouseenter', function(event) {
				if (this.hoverStars && this.votingIsEnabled) {
					activeStar = $(event.target);
					number = activeStar.getProperty('id').match(/\d+/)[0].toInt();
					numberStars = 5;
					while (numberStars > 0) {
						starToCheck = $('votingStar'+numberStars);
						imageSource = starToCheck.get('src').toString();
						if (numberStars <= number) {
							if (imageSource.lastIndexOf('_hover') == -1) {
								imageSource = imageSource.replace('.', '_hover.');
							}
						} else {
							imageSource = imageSource.replace('_hover.', '.');
						}
						starToCheck.set('src', imageSource);
						numberStars--;
					}				
				}
			}.bind(this));
			//set rating value
			star.addEvent('click', function(event) {
				
				if (this.votingIsEnabled) {
					event.stop();
					
					clickedStar = $(event.target);
					rating = clickedStar.getProperty('id').match(/\d+/)[0].toInt();				
					
					$('votingRating').setProperty('value', rating);		
					this.activeStars = rating;
					
					height = $('captchaContainer').getStyle('height').toInt();
					if (height == 0) {
						$('captchaContainer').tween('height', '110');
					}
				}
				
			}.bind(this));
		}.bind(this));
		
		//vote
		if (this.votingIsEnabled) {
			$('btnVotingSubmit').addEvent('click', function(event) {
				
				event.stop();
				
				rating = $('votingRating').getProperty('value');
				
				//get entered captcha
				captcha = $('votingCaptcha').value;
				
				var jsonRequest = new Request.JSON({url: this.ajaxURL, 
					onSuccess: function(data) {
					
						//check status
						if (data.status == 'success') {
							
							this.hoverStars = false;
							$('votingStarContainer').removeClass('votingEnabled');
							
							this.votingIsEnabled = false;
							
							height = $('captchaContainer').getStyle('height').toInt();
							if (height > 0) {
								$('captchaContainer').tween('height', '0');
							}
							
							if ($('stoererPreVote')) {
								$('stoererPreVote').setStyle('display', 'none');
							}
							if ($('stoererAfterVote')) {
								$('stoererAfterVote').setStyle('display', 'block');
							}
							
							//reset possible error-styles
							$('votingCaptcha').setStyle('border', 'solid 1px #8994A0');
							$('captchaError').setStyle('display', 'none');
							
						} else if (data.status == 'wrongCaptcha') {				
							
							$('votingCaptcha').setStyle('border', 'solid 1px #ff0000');
							$('votingCaptcha').setProperty('value', '');
							$('captchaError').setStyle('display', 'block');
							$('captchaImage').setProperty('src', '/captcha?r='+Math.random());
						
						} else {							
							console.log(data.status);							
						}
						
					}.bind(this)
				}).get({'associationID': this.associationId, 'rating': rating, 'captcha': captcha});	
				
			}.bind(this));
		
			//unhover stars			
			$('votingStarContainer').addEvent('mouseleave', function(event) {
				numberStars = 5;
				while (numberStars > 0) {
					starToCheck = $('votingStar'+numberStars);
					imageSource = starToCheck.get('src').toString();
					if (numberStars <= this.activeStars) {
						if (imageSource.lastIndexOf('_hover') == -1) {
							imageSource = imageSource.replace('.', '_hover.');
						}
					} else {
						imageSource = imageSource.replace('_hover.', '.');
					}
					starToCheck.set('src', imageSource);
					numberStars--;
				}
			}.bind(this));
		}
		
	}
});
