
$(function(){
	
	$('#scale li').click(function(){
		
			if(!is_logged())
				return;
				
				
		var parent_id=$("#scale").attr('class'); 
		var valuation=$(this).html();
		
		if(valuation=='X') valuation=0;
		/*
		//NO FUNCIONA EL JODIDO SELECTOR!!!
		else{
			$('.'+parent_id+' .options').each(function(){
				if($(this).children('#watchlist-3')){
					var $this=$(this).children('#watchlist-3');
					if($this.html()=='Quiero verla') $this.html('Quiero volver a ver');
				}
			});
		}
		*/
		$.ajax({
			type:'post',
			url:'/plugins/valuation/action.php',
			data:{child_id:parent_id, valuation:valuation, type:'film'},
			success:function(msg){
				// $('#scale ul').hide();
				 $('#scale').delay(250).fadeOut();
				if(valuation==0){
						$('#x_delete').css('display', 'none');
						valuation='?';
						$('#'+parent_id).attr('title','');
				}else $('#'+parent_id).attr('title', 'Tu puntuacion es '+valuation);
				
				$('.rate .user').each(function(){
					if($(this).attr('id')==parent_id){
						$(this).children('span#number').html(valuation);
					}
				});
				
				$('.rate .community').each(function(){
					if($(this).attr('id')==parent_id){
						if($(this).html()=='-') $(this).html(valuation);
					}
				});
			}
		});
	});
	
	
	$('.rate').click(
		function(e)
		{						
			$('#x_delete').css('display', 'none');
			$(this).append($('#scale').css("display","block"));
			$('#scale').attr('class', $(this).attr('id'));
			if($(this).attr('title')!='') $('#x_delete').css('display', 'block');
		}
	);
	
	$(document).bind('click', function(e) {
	           var clicked=$(e.target); // get the element clicked                 
	           if( clicked.is('.rate') || clicked.parents().is('.rate')) {
	                
	               // click safe!
	           } else {
	               // outside click
	               $('#scale').hide();
	               $('#review').hide();
				   $('#x_delete').css('display', 'none');
	           }
	       });
	
	
});
