/**
 * @author dan.rades
 * todo: documentare
 */
(function($) {                                         
$.fn.nextRating = function(o) {  
	var emptyFunction = function(){};
	 
    o = $.extend({				
		type : 'radio',
		baseUrl : '/?cms_service=rate/rating_new&service=1',
		useCaptcha : false,
		size : 5,
		captchaPath : '/cms/images/captcha.jpg.php',
		beforeRate : emptyFunction,
		afterRate : emptyFunction
		
    }, o || {})
	
	
	
	var rate = function(el) {
		$.get(el.url, {}, function(data) {
			if (o.useCaptcha) 
			{
				captchaValue.val('');
			}
			if (data.code == 0) {
				var randNr = randNum = Math.floor(Math.random() * 30000);
				var errMsg = o.captchaErrorMessage || data.message;
				if (o.useCaptcha) 
				{
					$('.ratingCaptchaImage').eq(0).attr('src', o.captchaPath + '?rnd=' + randNr);
					$('.ratingCaptchaError').eq(0).text(errMsg);
				}				
				el.captchaValue.focus();
				return false;
			}
			else if (data.code == 1){
				$('.ratingGrade').text(data.rate);
				$('.ratingVotersTxt').text(data.votes);		
			}

			$('.ratingMessage').text(data.message).show();
		
			el.ratingItems.css('cursor','auto');
			el.ratingItems.unbind();
			if (o.useCaptcha) {
				el.captchaContainer.hide();
			}
			return true;
			
		}, 'json');
	};	
	
	var colorGrade = function(el) {
		el.ratingItems.each(function(i) {
			var item = $(this);		
			if (i < el.ratingNet) {
				item.addClass('selected');
			}
			if (i == el.ratingNet && el.ratingNet > 0) {
				if(el.ratingZecimale > 0)
				{
					if (el.ratingZecimale > 0.9) {
						item.addClass('selected');					
					}
					else if (el.ratingZecimale > 0.7) {
						item.addClass('selected75');
					}
					else if (el.ratingZecimale > 0.4) {
						item.addClass('selected50');
					}	
					else {
						item.addClass('selected25');
					}
				}
			}
		});		
	};	
	
	return this.each(function(i) {
	
		var _self = $(this);
		var rateValue = 0;
		var timeout = 0;
		
		_self.ratingBrut = $('input.ratingTotal', _self).eq(0).val();
		_self.ratingNet = Math.floor(_self.ratingBrut);
		_self.ratingZecimale = Math.abs(_self.ratingBrut - _self.ratingNet);
		
		_self.url = o.baseUrl + '&item=' + $('input.ratingItemId', _self).eq(0).val()				
		_self.ratingItems = $('.ratingItem', _self);
		_self.ratingItemsContainer = $('.ratingItemsContainer',_self);
		
		if (o.useCaptcha) 
		{
			_self.captchaContainer = $('.ratingCaptchaContainer', _self);
			_self.captchaValue = $('input.ratingCaptchaValue', _self);
			
			$('input.ratingSubmit', _self).click(function() {				
				var captchaValue = _self.captchaValue.val() || 0;
				_self.url = _self.url + '&captcha=' + captchaValue;
				if (captchaValue == 0) {
					alert('Introduceti codul din imagine');
				}
				rate(_self);
			});
			
			$('input.ratingCancel', _self).click(function() {
				_self.captchaContainer.hide();
			});			
		}
		
		if (o.type == 'image') {
			var unhover = true;
			colorGrade(_self);
			_self.ratingItemsContainer.bind('mouseenter', function() {
				unhover = false;
				_self.ratingItems.removeClass('selected selected75 selected50 selected25');
			});
			_self.ratingItemsContainer.bind('mouseleave', function() {
				unhover = true;
				colorGrade(_self);			
			});
		}
		
		_self.ratingItems.each(function(i) {
			var item = $(this);
			
			if (o.type == 'image') {				
				item.bind('mouseenter', function() {
					_self.ratingItems.removeClass('hover');
					for (p = 0; p <= i; p++) {
						_self.ratingItems.eq(p).addClass('hover');
					}
				});
				
				item.bind('mouseleave', function() {
					setTimeout(function() {
						if (unhover) {
							_self.ratingItems.removeClass('hover');
							unhover = false;
						}
						}, 300);
				});				
			}
			
			item.bind('click', function() {								
					switch (o.type) {		
						case 'radio':
							rateValue = item.val();
							break;	
						case 'link':
							rateValue = item.attr('title');
							break;				
						case 'image':
							rateValue = item.attr('title');	
							_self.ratingItemsContainer.unbind('mouseleave');	
							unhover = false;				
							break;							
						default:							
							break;
					}					
		
					_self.url += '&rate=' + rateValue;
					
					if (o.useCaptcha) {
						_self.captchaContainer.show();
						_self.captchaValue.focus();
						return;
					}
					else {					
						rate(_self);
					}
			});			
		});			
																									
	});
	

};



})(jQuery);

