$(function() {
	// At least jQuery 1.3.x required, else the mouseover/-out and click events are not unbind properly
	
	// CSS stylings
	$('#contentRating')
		.css({
			paddingTop: '15px'
		});
		
	$('#contentRating .horizontalLine')
		.css({
			display: 'block',
			height: '1px',
			lineHeight: '1px',
			overflow: 'hidden',
			backgroundColor: '#A4C397'
		});
		
	$('#contentRating p')
		.css({
			display: 'block',
			color: '#155005',
			fontFamily: 'Verdana, Arial, Helvetica, sansserif',
			fontSize: '11px',
			lineHeight: '25px',
			margin: '0',
			padding: '0'
		});
		
	$('#contentRating div p')
		.css({
			margin: '0',
			float: 'left'
		});
		
	$('#contentRating div p:first-child')
		.css({
			paddingRight: '15px'
		});	
		
	$('#ratingStatistics')
		.css({
			paddingLeft: '15px'
		});
		
	$('#contentRating img')
		.css({
			paddingTop: '5px',
			float: 'none'
		});
		
	$('.ratingBox')
		.css({
			display: 'block',
			width: '21px',
			height: '20px',
			float: 'left',
			padding: '1px',
			backgroundPosition: '50% 50%',
			backgroundRepeat: 'no-repeat'
		});
	
	
	// Set the default background image for each box
	$.each($('.ratingBox'), function(){
		this.thisRate = $(this).attr('id').substr('rate_'.length, $(this).attr('id').length);
		this.thisInitialImage = ( (this.thisRate <= Math.round(averageRating)) ? contentRatingRoot+'art/img_stern_gruen.gif' : contentRatingRoot+'art/img_stern_grau.gif' );
		
		// If user has voted this value or more
		if (userRating >= this.thisRate) {
			// this.thisInitialImage = contentRatingRoot+'art/img_stern_rot.gif';
		}
		
		$(this).css({ backgroundImage: 'url("'+ this.thisInitialImage +'")', cursor: 'pointer' });
	});
	
	// Events
	$('.ratingBox')
		.bind('mouseover', function() {
			// Change background image for current box and all previous ones
			$(this).prevAll('.ratingBox').andSelf().css('background-image', 'url("'+ contentRatingRoot +'art/img_stern_rot.gif")');
		})
		.bind('mouseout', function() {
			// Change background image for current box and all previous ones
			$.each($(this).prevAll('.ratingBox').andSelf(), function() {
				$(this).css('background-image', 'url("'+ this.thisInitialImage +'")');
			});
		})
		.bind('click', function() {
			// Only one vote allowed, so unbind the effent after click
			$('.ratingBox').unbind('mouseover mouseout click');
			
			// If user has not already rated this page
			if ( userRating == false ) {
				userRating = $('.ratingBox').index(this) + 1;
				
				$.ajax({
					type: 'POST',
					cache: false,
					url: contentRatingRoot+'performRating.asp',
					data: { rating: userRating, path: path, querystring: querystring },
					dataType: 'json',
					success: function(data) {
						// Renew the boxes background images
						$('.ratingBox').css('background-image', 'url("'+ contentRatingRoot +'art/img_stern_grau.gif")');
						$('.ratingBox:lt('+ Math.round(data.averageRating) +')').css('background-image', 'url("'+ contentRatingRoot +'art/img_stern_gruen.gif")');
						
						// Renew the statistics text
						$('#ratingStatistics').html('<strong>'+ data.averageRating.replace(/\./, ',') +'<\/strong> ('+ data.numberOfRatings +((data.numberOfRatings != 1) ? ' Bewertungen' : ' Bewertung') +')');
					}
				}); // end $.ajax
			} // end if ( userRating == false )
			
			// Show a confirmation message
			$('#ratingMessageToUser').css('color', '#FF0000').html('Vielen Dank f&uuml;r Ihre Bewertung.');
		}); // end $('.ratingBox')
		
		// Unbind mouseover/-out and click events for the boxes and show confirmation message if user already rated this page
		if (userRating != false) {
			$('.ratingBox').unbind('mouseover mouseout click');
			$('#ratingMessageToUser').css('color', '#FF0000').html('Vielen Dank f&uuml;r Ihre Bewertung.');
		}
		
});