﻿jQuery.fn.rating=function(o){jQuery.ajaxSetup({cache:false});var r=this;var url=getUrlServicos();var p={Path:"/images/votos/",Imagem:{Nota:{N0:"img-estrela-ativa-0.gif",N1:"img-estrela-ativa-1.gif",N2:"img-estrela-ativa-2.gif",N3:"img-estrela-ativa-3.gif",N4:"img-estrela-ativa-4.gif",N5:"img-estrela-ativa-5.gif"},Estrela:{Ativa:"ativa.gif",Inativa:"inativa.gif",Hover:"hover.gif"}},Texto:{Nota:{N0:"Péssimo",N1:"Ruim",N2:"Razoável",N3:"Bom",N4:"Ótimo",N5:"Excelente"},Mensagem:{Default:"Avalie esta matéria",Obrigado:"Obrigado pela avaliação!"}},Related:"",ShowText:true,ShowCount:true,ReadOnly:true,Value:0,ID:0,Count:0,CriaImagem:function(value,status){var A=jQuery("<a href='' value='"+value.toString()+"'><img src='"+p.Path+(status=="ativa"?p.Imagem.Estrela.Ativa:p.Imagem.Estrela.Inativa)+"' border=0 /></a>");A.click(function(){p.Value=parseInt(jQuery(this).attr("value"));jQuery.getJSON(url+"Rating.svc/SetValue?conCodigo="+p.ID+"&value="+p.Value,{},function(json){p.ReadOnly=true;r.children().remove();if(p.ShowText){r.append("<span class='textoMensagem'>"+p.Texto.Mensagem.Obrigado+"</span>")}p.CreateRating();if(p.ShowCount){jQuery.getJSON(url+"Rating.svc/GetCount?conCodigo="+p.ID,{},function(json2){p.Count=parseInt(json2.d);r.append("<span class='textoCount'>"+p.Count+(p.Count!=1?" votos":" voto")+"</span>")})}});if(p.Related!=""){setTimeout(function(){var related=p.Related.split(",");for(j=0;j<related.length;j++){var x=jQuery("#"+related[j]);x.children().remove();x.append(r.clone())}},500)}return false});A.mouseover(function(){var m=this;var texto="";r.children("span.textoMensagem").remove();r.children("span.textoCount").remove();if(p.ShowText){switch(parseInt(value)){case 0:texto+=p.Texto.Nota.N0;break;case 1:texto+=p.Texto.Nota.N1;break;case 2:texto+=p.Texto.Nota.N2;break;case 3:texto+=p.Texto.Nota.N3;break;case 4:texto+=p.Texto.Nota.N4;break;case 5:texto+=p.Texto.Nota.N5;break}r.prepend("<span class='textoMensagem'>"+texto+"</span>")}r.children("span.imagemRating").children("a").children("img").each(function(){if(parseInt(jQuery(this).parent().attr("value"))<=parseInt(value)){jQuery(this).attr("src",p.Path+p.Imagem.Estrela.Hover)}});if(p.ShowCount){r.append("<span class='textoCount'>"+p.Count+(p.Count!=1?" votos":" voto")+"</span>")}});A.mouseout(function(){r.children().remove();if(p.ShowText){r.append("<span class='textoMensagem'>"+p.Texto.Mensagem.Default+"</span>")}r.append("<span class='imagemRating'></span>");for(i=0;i<p.Value;i++){r.children(".imagemRating").append(p.CriaImagem(parseInt(i)+1,"ativa"))}for(i=0;i<5-p.Value;i++){r.children(".imagemRating").append(p.CriaImagem(parseInt(i)+1+p.Value,"inativa"))}if(p.ShowCount){r.append("<span class='textoCount' >"+p.Count+(p.Count!=1?" votos":" voto")+"</span>")}});return A},CreateRating:function(){r.append("<span class='imagemRating'></span>");if(p.ReadOnly){var img=p.Path;switch(p.Value){case 0:img+=p.Imagem.Nota.N0;break;case 1:img+=p.Imagem.Nota.N1;break;case 2:img+=p.Imagem.Nota.N2;break;case 3:img+=p.Imagem.Nota.N3;break;case 4:img+=p.Imagem.Nota.N4;break;case 5:img+=p.Imagem.Nota.N5;break}r.children(".imagemRating").append("<img src='"+img+"' />")}else{for(i=0;i<p.Value;i++){r.children(".imagemRating").append(p.CriaImagem(parseInt(i)+1,"ativa"))}for(i=0;i<5-p.Value;i++){r.children(".imagemRating").append(p.CriaImagem(parseInt(i)+1+p.Value,"inativa"))}}},Init:function(){if(p.ShowText){r.append("<span class='textoMensagem'>"+p.Texto.Mensagem.Default+"</span>")}p.CreateRating();if(p.ShowCount){jQuery.getJSON(url+"Rating.svc/GetCount?conCodigo="+p.ID,{},function(json2){if(json2!=null){p.Count=parseInt(json2.d);r.append("<span class='textoCount' >"+p.Count+(p.Count!=1?" votos":" voto")+"</span>")}})}}};p.ID=parseInt(this.text());jQuery.extend(p,o);this.text("");if(p.ID!=0){jQuery.getJSON(url+"Rating.svc/GetValue?conCodigo="+p.ID,{},function(json){try{p.Value=parseInt(json.d)}catch(err){p.Value=0}p.Init()})}else{if(p.ID==0&&p.Value!=0){p.Init()}}};
