(function ($) {

/**
 * Rating
 * @constructor jQuery
 */
$.widget('ui.Rating', $.extend({}, $.ui.Component.prototype, {
    type: 'Rating',
    init: function () {
        var self = this;
        //return;
        var ratingBlockWidth = 95;
        var tempText = '';

        self.element.find('#' + self.element.attr('id') + ' .voteEnabled').live('mouseover', function (e) {
        	var currentPosition = parseInt(((e.pageX - getLeftOffset(this) + 1) / ratingBlockWidth) * 100);
        	tempText = $(this).parent().find('.text').html();
        	if($(this).hasClass('notAccepted')){
        		$(this).addClass('greyStar');
        		$(this).parent().find('.text').html($(this).parent().find('.text').attr('votedText'));
        	}else{
        		$(this).addClass('redStar');
        		$(this).find('.fill').width(validateRating(currentPosition) + '%');
        		var currentPosition = parseInt(e.pageX - getLeftOffset(this) + 1);
	        	if(currentPosition > 0 && currentPosition <= 19){
	        		$(this).parent().find('.text').html('Ужасно');
	        	}else if(currentPosition > 19 && currentPosition <= 38){
	        		$(this).parent().find('.text').html('Плохо');
	        	}else if(currentPosition > 38 && currentPosition <= 57){
	        		$(this).parent().find('.text').html('Так себе');
	        	}else if(currentPosition > 57 && currentPosition <= 76){
	        		$(this).parent().find('.text').html('Хорошо');
	        	}else{
	        		$(this).parent().find('.text').html('Отлично!');
	        	}
        	}
        });
        
        self.element.find('#' + self.element.attr('id') + ' .voteEnabled').live('mousemove', function (e) {
        	var currentPosition = parseInt(((e.pageX - getLeftOffset(this) + 1) / ratingBlockWidth) * 100);
        	if($(this).hasClass('notAccepted')){
        		$(this).addClass('greyStar');
        	}else{
        		$(this).addClass('redStar');
        		$(this).find('.fill').width(validateRating(currentPosition) + '%');
        		var currentPosition = parseInt(e.pageX - getLeftOffset(this) + 1);
	        	if(currentPosition > 0 && currentPosition <= 19){
	        		$(this).parent().find('.text').html('Ужасно');
	        	}else if(currentPosition > 19 && currentPosition <= 38){
	        		$(this).parent().find('.text').html('Плохо');
	        	}else if(currentPosition > 38 && currentPosition <= 57){
	        		$(this).parent().find('.text').html('Так себе');
	        	}else if(currentPosition > 57 && currentPosition <= 76){
	        		$(this).parent().find('.text').html('Хорошо');
	        	}else{
	        		$(this).parent().find('.text').html('Отлично!');
	        	}
        	}
        });
        
        self.element.find('#' + self.element.attr('id') + ' .voteEnabled').live('mouseout', function (e) {
        	if($(this).hasClass('notAccepted')){
        		$(this).removeClass('greyStar');
        	}else{
        		$(this).removeClass('redStar');
        		$(this).find('.fill').width($(this).attr('ratingValue') + '%');
        	}
        	$(this).parent().find('.text').html(tempText);
        });
        
        self.element.find('#' + self.element.attr('id') + ' .voteEnabled').live('click', function (e) {
        	if(!$(this).hasClass('notAccepted')){
	        	var currentPosition = parseInt(((e.pageX - getLeftOffset(this) + 1) / ratingBlockWidth) * 100);
	        	var objectId = $(this).attr('objectId') || 0;
	        	var type = $(this).attr('type') || '';
	            if (currentPosition > 0 && currentPosition <= 100) {
	            	self.reload({
		                rating: 	validateRating(currentPosition),
		                objectId:	objectId,
	                    type:       type
		            });
	            }
	            return false;
        	}
		});
        
        
        
        function validateRating(position) {

        	if (position > 0 && position <= 20) {
        		return 20;
        	} else if (position > 20 && position <= 40) {
        		return 40;
        	} else if (position > 40 && position <= 60) {
        		return 60;
        	} else if (position > 60 && position <= 80) {
        		return 80;
        	} else if (position > 80 && position <= 100) {
        		return 100;
        	} else {
        		return 100;
        	}
        }
        
        function getLeftOffset(element) {
			var left = element.offsetLeft;
			for (var parent = element.offsetParent; parent; parent = parent.offsetParent) {
				left += parent.offsetLeft - parent.scrollLeft;
			}
			return left;	
        }
    }
}));

})(jQuery);