


$(document).ready(function(){

	$("a.star").mouseover(function() {
		$obj = $(this).context;
		$objParentId = $obj.parentNode.id;

		$classes = explodeList($obj.className," ");
		$id = explodeList($classes[1],"-");
		$id = $id[1];

		$star = explodeList($classes[2],"-");
		$star = $star[1];

		for ( $i=1; $i<=10; $i++ ){
			if ($i<=$star){
				if ($i % 2 == 1){
					$("#"+$objParentId+" a.star-"+$i).addClass("star-hover-1").removeClass("star-off-1").removeClass("star-on-1");
				} else {
					$("#"+$objParentId+" a.star-"+$i).addClass("star-hover-2").removeClass("star-off-2").removeClass("star-on-2");
				}
			} else {
				if ($i % 2 == 1){
					$("#"+$objParentId+" a.star-"+$i).addClass("star-off-1").removeClass("star-hover-1").removeClass("star-on-1");
				} else {
					$("#"+$objParentId+" a.star-"+$i).addClass("star-off-2").removeClass("star-hover-2").removeClass("star-on-2");
				}
				
			}
		}
	});
	
	$("a.star").mouseout(function() {

		$obj = $(this).context;
		$objParentId = $obj.parentNode.id;
		
		//USER RATE
		$userRate = $obj.parentNode.className;
		$userRate = explodeList($userRate," ");
		$userRate = explodeList($userRate[2],"-");
		$userRate = $userRate[2];
		
		//GET STAR
		$classes = explodeList($obj.className," ");
		$star = explodeList($classes[2],"-");
		$star = $star[1];
		
		for ( $i=1; $i<=10; $i++ ){
			if ($i<=$userRate){
				if ($i % 2 == 1){
					$("#"+$objParentId+" a.star-"+$i).addClass("star-on-1").removeClass("star-hover-1").removeClass("star-off-1");
				} else {
					$("#"+$objParentId+" a.star-"+$i).addClass("star-on-2").removeClass("star-hover-2").removeClass("star-off-2");
				}
			} else {
				if ($i % 2 == 1){
					$("#"+$objParentId+" a.star-"+$i).addClass("star-off-1").removeClass("star-hover-1").removeClass("star-on-1");
				} else {
					$("#"+$objParentId+" a.star-"+$i).addClass("star-off-2").removeClass("star-hover-2").removeClass("star-on-2");
				}
				
			}
		}
	});
	
	$("a.star").click(function() {
		$obj = $(this).context;
		$objParentId = $obj.parentNode.id;
		$id = explodeList($objParentId,"-");
		$id = $id[3];
		
		//FRASES OU AUTORES
		$ajaxFile = "ajax-frases-avaliar.php";
		$errorTitle1 = "frases";
		$errorTitle2 = "desta frase";
		$ratingType = $obj.parentNode.className;
		$ratingType = explodeList($ratingType," ");
		if ($ratingType[1] == "rating-authors") {
			$ajaxFile = "ajax-autores-avaliar.php";
			$errorTitle1 = "autores";
			$errorTitle2 = "deste autor";
		}

		$classes = explodeList($obj.className," ");
		$star = explodeList($classes[2],"-");
		$star = $star[1];

		$.ajax({
			type: "POST",
			url: $ajaxFile,
			data: "id="+$id+"&stars="+$star,
			beforeSend: function (XMLHttpRequest) {
				$("#more-"+$id).html("<div class=\"txtC f08em\"><img src=\"images/icon-busy-20x20.gif\" /> carregando...</div>");
			},	
			success: function(msg){
				$msg = explodeList(msg,"|");
				if ($msg[0] == 1){
					$("#more-"+$id).html($msg[1]);
				} else if ($msg[0] == 2){
					alert("Os dados para avaliação estão incompletos.");
				} else if ($msg[0] == 3){
					alert("Houve um erro na avaliação "+$errorTitle2+".");
				} else {
					$("#"+$objParentId).html($msg[1]);
					$val = $star/2;
					$many = ($val > 1)?"s":"";
					$("#more-"+$id).html($msg[2]);
				}
			}
		});
	});
	
	
});