var Rating = new Class({
	
	Implements		: Options,
	anchorElements	: null,
	anchors			: [],
	startEventIndex	: null,
	
	text			: ['Slecht','Behoorlijk','Goed','Zeer goed','Uitstekend'],
	voteStatus		: ['Stemmen', 'Bedankt voor je stem', 'Je hebt al gestemd'],
	
	textHoverID		: null,
	resultContainer	: null,
	placedVote		: false,
	
	
	options: {
		container	: 'rating',
		url			: '/shop/processRating.php',
		increment	: 0.5,
		multiplier	: 2,
		stars		: 5,
		productID	: 174916, 	/* dynamic */
		value		: 0, 		/* dynamic */
		authorised  : true		/* dynamic */
	},
	
	
	initialize: function( options ) {
		
		this.setOptions( options );		
		this.createStars();
		
		if ( this.options.authorised == true ) {
			this.initStarEvents();
		}
	
	},
	
	createStars: function() {
		
		var container 	= $( this.options.container );						
		var prefix 		= new Element('li', {'class':'prefix','html':'Beoordeling van klanten:'});
		prefix.inject(container);
		
		for ( var i = 0; i <= ( this.options.stars / this.options.increment ); i++ ) {
			
			if ( i > 0 ) {
				
				var cssClass 	= this.cssClass(i);				
				var a 			= new Element('a', { 'html': '&nbsp;', 'class': cssClass, 'href': '#' + i });
				var li 			= new Element('li');
							
				li.adopt(a).inject( container );
				this.anchors[ this.anchors.length ] = a;
				
			} 
			
		}
						
		var liResult = new Element('li', {'id':'result','html':'lala'});
		liResult.inject(container);
			
		
		this.anchorElements = container.getElements('a');
		if ( this.options.authorised == false ) {
			this.anchorElements.addClass('disabled');	
		}
		
		this.resultContainer = $('result');
		this.initMessage();
		
	},
	
	
	initMessage: function() {
		
		/* when user was authorised to place vote and vote was just placed */
		if ( this.options.authorised == true && this.placedVote == true ) {
			this.resultContainer.innerHTML = this.voteStatus[1];
		}
				
		/* when user is not	authorised, cq. allready voted */
		else if ( this.options.authorised == false ) {
			this.resultContainer.innerHTML = this.voteStatus[2];
		}
		
		/* when user is allowed to voted and no vote has been placed */
		else if ( this.options.authorised == true && this.placedVote == false ) {
			this.resultContainer.innerHTML = this.options.amount + ' ' + this.voteStatus[0];
		}
		
		
	},
	
	initStarEvents: function() {		
		
		var rating = this;		
			
		this.anchors.each(function(anchor, i) {
							
			anchor.addEvents({			
									
				'mouseover': function() {					
					if ( rating.options.authorised == true ) {
						rating.fillStars(i);
						rating.statusMessage(i);
					}
				},				
				
				'click': function(e) {
					 if ( rating.options.authorised == true ) {
						 rating.castVote(e);					
					 }
				}				
							
			});
			
		});
		
		
		$( this.options.container ).addEvent('mouseleave', function(e) {
			if ( rating.options.authorised == true ) {
				rating.initMessage();					
				rating.resetStars();
			}
		});
		
			
						
	},
	
	cssClass: function(i, current) {
		
		var cssClass = ( (i % 2) ? 'left' : 'right' );		
				
		if ( i <=  this.options.value ) {
			return cssClass + 'Active';	
		} 
		
		return cssClass;
		
	},
	
	statusMessage: function(i) {
				
		var star 	 		= (i + 1) / this.options.multiplier;
		var index 			= star.round() - 1;
		
		if ( index != this.textHoverID ) {
		
			this.textHoverID = index;					
			this.resultContainer.innerHTML	= this.text[ index ];
		
		}
		
	},
	
	fillStars: function(i) {
		
		var rating 	= this;
		
		this.anchors.each( function(el, index) {
			
				
			var cssClass = (index % 2) ? 'right' : 'left';	
				
			if ( index <= i ) {		
			
				if ( el.hasClass(cssClass) == true ) {										
					el.removeClass(cssClass);
					el.addClass(cssClass + 'Active');								
				} 
				
				
			} else {
								
				if ( el.hasClass(cssClass + 'Active') ) {					
					el.removeClass(cssClass + 'Active');
					el.addClass(cssClass);										
				}
				
						
			}
									
		});
		
	},
	
	
	resetStars: function() {
		this.fillStars( this.options.value - 1 );
	},
	
	
	castVote: function( e ) {
	
		var value = e.target.getAttribute('href').split('#')[1];
				
		
		var request = new Request({ 
							method: 'post',						
							url: this.options.url,
							data: { 'rating': value, 'productID': this.options.productID }
						}).send();
		
		/* if all went well, else, nothing */
		this.placedVote = true;
		this.initMessage();
				
		this.options.authorised = false;
		this.options.value = value;
		this.resetStars();
		
		this.anchorElements.addClass('disabled');
		this.anchorElements.removeEvents();
			
		
	}
	
		
});