$(function(){
	
	$('#content input[type=radio].star').rating();
	//$('.rating-cancel').hide();

	var err = false;

	$('#content .star').mousemove(function(){

		var votacao = $(this).find('a').html();
		var title = $(this).find('a').attr('title');
		
		$('#content input[type=radio].star').each(function(){

			if ( $(this).attr('disabled') ){
				err = true;
			}	
		});

		if ( ! err )
			$('#teste').html(title);
		
		//$('#teste').html(title);

	}).click(function(){

		/*$('input[type=radio].star').each(function(){
			$(this).attr("disabled","disabled");
		});
		
		$(this).parent().find('div').each(function(){

			if ( $(this).attr('class') != 'rating-cancel' ){
				
				$(this).removeClass('star-rating-live');
				//$(this).removeClass('star-rating-on');
				$(this).addClass('star-rating-readonly');
				//alert($(this).attr('class'));
			}
					
			
		});
		return false;*/
		
		var div = $(this).parent().parent();
		var id_div = div.attr('id');
		var id_video = $(this).parent().parent().attr('id').split("-")[1];
		var ranking = $(this).find('a').html();
		var title = $(this).find('a').attr('title');
		
		$('#content input[type=radio].star').each(function(){

			if ( $(this).attr('disabled') ){
				err = true;
			}
			
		});
		
		var autenticado = 0;
		
		if ( ! err ){
			
			$.ajax({

				type: 'POST',
				url: 'ranking_acao.php',
				data: 'id_video='+id_video+'&ranking='+ranking+'&texto='+title,
	
				beforeSend: function(){
					if ( $('.ranking-resposta') ){
						$('.ranking-resposta').remove();
					}
					$('<div class="ranking-resposta"><span>Classificando...</span></div>').insertAfter('#'+id_div);
				},	
				success: function(data) {
					$('.ranking-resposta').empty();
					$('.ranking-resposta').html(data);
					
					$('#content input[type=radio].star').each(function(){
						$(this).attr("disabled","disabled");
					});
					$('#'+id_div).addClass('video-classificado');
					return false;
				},
	            error: function(xml){
	                $('.ranking-resposta').html('Erro!');
	            }

	           
			}); //close $.ajax(*/
			
		}else{
			
			/*$.post('ranking_verificar.php',
			{id_video:id_video},
			function(data){
				if ( data == 1 ){
					autenticado = 1;
					alert(data);	
				}
			},"html");*/
			
			if ( $(this).parent().parent().attr('class') == 'video-classificado' ){
				if ( $('.ranking-resposta') ){
					$('.ranking-resposta').remove();
				}
				$('<div class="ranking-resposta">Você já classificou este vídeo!</div>').insertAfter('#'+id_div);
			}
		}
			
	});
	
	if ( $('#mod-video input[type=radio].star-rel') ){
		$('#mod-video input[type=radio].star-rel').rating();
	}	
	
});
