/* ------------------------------------------------------------------------
	Rating Functions
------------------------------------------------------------------------- */

	var voted = false;

	ratings = {
		init : function(){
			// Loop through all the star links, and associate the ajax functions
			$('ul.star-rating a:not(.voted)').click(function(){
				$caller = $(this);

				$.ajax({
					type: "POST",
					url: $caller.attr('href'),
					dataType: "json",
					success:function(responseText){
						responseText = eval(responseText);
						if(responseText){
							$('p.rating span:first').text(responseText['rating_count']);
							$('ul.star-rating li.current-rating').width((eval(responseText['average_rating']) * 20) + '%');

							if(eval($('p.rating span:first').text() > 1))
								$('p.rating span.plural').text('s');

							// to disable to voting
							voted = true;
							$('ul.star-rating a').addClass('voted').trigger('blur');

							ratings.displaySuccess($caller);
						}else{
							ratings.displayError($caller);
						}
					}
				});
				return false;
			});

			$('ul.star-rating a:not(.voted)').click(function(){ return false; });

			tooltips.init();
		},
		displayError : function(caller){
			if(settings.lang == 'fr'){
				errorMessage = "<span style='color:#FE0000;'>Vous avez déjà voté</span>";
			}else{
				errorMessage = "<span style='color:#FE0000;'>You already voted</span>";
			}
			
			// Hide the current tooltip if there's one
			$('div.tooltip').remove();
			
			// Build the tooltip
			$(caller).parent().parent().parent().prepend(tooltips.build(errorMessage));
			
			// Position the toolip
			$('div.tooltip').css({
				'top': -25,
				'left': ($(caller).position().left + $(caller).width()) - 19
			});
		},
		displaySuccess : function(caller){
			if(settings.lang == 'fr'){
				errorMessage = "Votre note a été prise en compte";
			}else{
				errorMessage = "This includes your vote";
			}
			
			// Hide the current tooltip if there's one
			$('div.tooltip').remove();
			
			// Build the tooltip
			$(caller).parent().parent().parent().prepend(tooltips.build(errorMessage));
			
			// Position the toolip
			$('div.tooltip').css({
				'top': -25,
				'left': ($(caller).position().left + $(caller).width()) - 19
			});
		}
	}
	
	tooltips = {
		init : function(){
			$('ul.star-rating a').hover(
				function(){
					if(!voted){
						tooltips.hover(this);
					}else{
						ratings.displayError($(this));
					}
				},
				function(){
					tooltips.out(this);
				}
			);
		},
		hover : function(caller){
			// Build the tooltip
			$(caller).parent().parent().parent().prepend(tooltips.build($(caller).attr('rel')));
			
			// Position the toolip
			$('div.tooltip').css({
				'left':($(caller).position().left + $(caller).width()) - 19,
				'opacity': 0
			}).animate({'opacity':1,'top':-25});
		},
		out : function(){
			$('div.tooltip').remove();
		},
		build : function(content){
			tooltip = "<div class='tooltip'><div class='inside'>"+content+"</div></div>";
			return tooltip;
		}
	}
	
	reviews = {
		init : function(rating){
			$('.prettyPopin ul.star-rating a').hover(
				function(){
					tooltips.hover(this);
				},
				function(){
					tooltips.out(this);
				}
			).click(function(){
				if(!rating) voted = true;
				$('.prettyPopin ul.star-rating li.current-rating').width(($(this).parent().data('rating') * 20) + '%');
				$('#ReviewRating').val($(this).parent().data('rating'));
				return false;
			});

			$('.remaining').each(function(){
				$(this).data('maxChar',eval($(this).find('span.max').text()));
				
				$('#' + $(this).attr('rel')).keyup(function(){
					$maxCharContainer = $('p[rel='+$(this).attr('id')+'] span.counter');
					
					charCount = $maxCharContainer.parent().data('maxChar') - $(this).val().length;
					if(charCount < 0){
						charCount = charCount * -1;
						$('p[rel='+$(this).attr('id')+'].toomany').removeClass('hide');
						$('p[rel='+$(this).attr('id')+'].remaining').addClass('hide');
					}else{
						$('p[rel='+$(this).attr('id')+'].toomany').addClass('hide');
						$('p[rel='+$(this).attr('id')+'].remaining').removeClass('hide');
					}
					
					if(charCount <= 1){
						$('p[rel='+$(this).attr('id')+'] span.plural').hide();
					}else{
						$('p[rel='+$(this).attr('id')+'] span.plural').show();
					}
					$maxCharContainer.text(charCount);
				});
			});
			
			$('input,textarea').trigger('keyup');

			// Associate the date to the lis
			$('.prettyPopin ul.star-rating li').each(function(i){
				$(this).data('rating',i);
			});
			
			// If the user already rated, trigger the rating
			$('.prettyPopin ul.star-rating li:eq('+rating+') a').trigger('click');
		},
		setPaging : function(){
			$('a[rel=reviewPaging]').click(function(){
				
			});
		},
		setRating : function(language_key){
			// Make sure the user voted.
			if(!voted) return;
			 
			$.get('/' + settings.lang + '/ratings/get_stats/' + language_key,function(responseText){
				
				responseText = eval('('+responseText+')');
				if(responseText){
					$('p.rating span:first').text(responseText['rating_count']);
					$('ul.star-rating li.current-rating').width((eval(responseText['average_rating']) * 20) + '%');
				
					if(eval($('p.rating span:first').text() > 1))
						$('p.rating span.plural').text('s');
					
					// to disable to voting
					$('ul.star-rating a').addClass('voted').trigger('blur');
					
					ratings.displaySuccess($('ul.star-rating a:first'));
				}else{
					ratings.displayError($('ul.star-rating a:first'));
				}
			});
		}
	}