rate = {
		
	currentIndex : 0,
		
	setRateBox : function(rate, sender){
		
		var superParent = sender.parents('.widget:eq(0)');
		var parent = sender.parent();
		var imageSize = superParent.find('.imgSizeInput').val();
		
		if(superParent.find('.canRateInput').val() == 0)
			return;
	
		var range = parent.find('.rateItem').size();		
				
		var multiplier = superParent.find('.multiplierInput').val();
		
		if(multiplier == 0)
			multiplier = (rate > (range / 2) ? 2 : 1);
		
		parent.find('.rateItem .leftPart').css('background-position', '0px 0px');		
		parent.find('.rateItem .rightPart').css('background-position', '-' + (imageSize / 2) + 'px 0px');
		
		var leftCss  = '0px -' + (multiplier * imageSize) + 'px';
		var rightCss = '-' + (imageSize / 2) + 'px -' + (multiplier * imageSize) +'px'; 
		
		parent.find('.rateItem:lt(' + Math.floor(rate) + ')').find('.leftPart').css('background-position', leftCss);
		parent.find('.rateItem:lt(' + Math.floor(rate) + ')').find('.rightPart').css('background-position', rightCss);
				
		if(Math.round(rate) !== rate){
			var diff = rate - Math.floor(rate);
			
			if(diff > 0.25)		
				superParent.find('.rateItem:eq(' + (Math.floor(rate)) + ')').find('.leftPart').css('background-position', leftCss);
			
			if(diff >= 0.75)			
				superParent.find('.rateItem:eq(' + (Math.floor(rate)) + ')').find('.rightPart').css('background-position', rightCss);
			
		}
			
		rate.currentIndex = rate;
		
	},
	
	bindEvents : function(){
		
		$('.widget .rateBox .rateItem').mouseover(function(){	
			
			var superParent = $(this).parents('.widget:eq(0)');
			if(superParent.find('.canRateInput').val() == 0)
				return;
			
			var index = superParent.find('.rateItem').index($(this)) + 1;
		
			$(this).parent().css('cursor', 'pointer');
			
			if(rate.currentIndex != index){				
				rate.setRateBox(index, $(this));				
			}
				
			
				
		}).click(function(){
			
			var superParent = $(this).parents('.widget:eq(0)');
			
			if(superParent.find('.canRateInput').val() == 0)
				return false;
			
			var index = superParent.find('.rateItem').index($(this)) + 1;			
			var widgetId = superParent.find('.widgetId').val();			
			
			superParent.find('.rateBox').remove();			
			superParent.find('.ajaxLayer').show();
			
			superParent.load('/rate/widget?rate=' + index + '&widgetId=' + widgetId, {} , function(){
				rate.bindEvents();
			});
			
		});
		
		$('.widget .rateBox').bind("mouseleave", function(){	
			
			var superParent = $(this).parents('.widget:eq(0)');
			rate.setRateBox(superParent.find('.rateInput').val(), $(this).find('.rateItem:first'));
			
			$(this).css('cursor', 'default');
			
		});
		
	}

	
		
};

$(function(){
 
	rate.bindEvents();
	
	
});	