//Copyright © [Year(s)] Nokia Corporation and/or its subsidiary(-ies).

Fp.Ratings={ratingCount:0,ratingValue:0,userRating:0,init:function(){this.ratingCount=0;this.ratingValue=0;this.userRating=0;if(Fp.POI){if(Fp.POI.rating){this.ratingCount=Fp.POI.rating.ratingCount||0;this.ratingValue=Fp.POI.rating.rating||0;this.userRating=Fp.POI.rating.userRating||0;}this.setVisuals(Fp.POI);this.setupEvents();}},setVisuals:function(a){var c="";if(a&&a.name){c=a.name;if(a.address){var b=Fp.Utils.formatAddress(a.address,a.isPlace);if(b){c+=", "+b;}}}Fp.Marquee.addAnimatedString({element:Fp.byId("ratepage-address"),text:c});this.setOneVisuals({element:Fp.byId("connect-menu-rating"),rating:this.ratingValue,countRat:this.ratingCount,countEle:Fp.byId("connect-menu-rating-count")});this.setOneVisuals({element:Fp.byId("ratepage-smallRating"),rating:this.ratingValue,countRat:this.ratingCount,countEle:Fp.byId("ratepage-smallRating-count")});this.setOneVisuals({element:Fp.byId("ratepage-bigRating"),rating:this.userRating});},setupEvents:function(){var d=Fp.byId("ratepage-bigRating"),b=this,c,a;for(c=0,a=d.childNodes.length;c<a;c++){d.childNodes[c].onmousedown=function(){b.userRating=parseInt(this.getAttribute("name"),10);if(Fp.Lang.isRTL){b.userRating=6-b.userRating;}b.clearOneRating({element:this.parentNode});b.setOneVisuals({element:this.parentNode,rating:b.userRating});Fp.byId("ratingsSubmit").className="btnLeft btnCentered";};}new Fp.Utils.addTapEvent({element:Fp.byId("ratingsSubmit"),onEvent:function(){Fp.Ratings.submit();}});},setOneVisuals:function(f){if(f&&f.element&&f.rating!==undefined){this.clearOneRating({element:f.element});var c=f.element.childNodes,e=parseInt(f.rating,10),d=f.rating-e,b;if(Fp.Lang.isRTL){for(b=0;b<e;b++){c[(c.length-1-b)].className="full";}if(d>=0.5&&c[(c.length-1-e)]){c[(c.length-1-e)].className="half";}}else{for(b=0;b<e;b++){c[b].className="full";}if(d>=0.5&&c[e]){c[e].className="half";}}if(f.countEle&&f.countRat!==undefined){var a=Language[f.countEle.title];f.countEle.innerHTML=a.replace("%d",f.countRat);}}},clearOneRating:function(d){if(d&&d.element){var c=d.element.childNodes,b,a;for(var b=0,a=c.length;b<a;b++){c[b].className="";}}},clearUserRatings:function(){var a=Fp.byId("ratepage-bigRating");for(var c=0,b=a.childNodes.length;c<b;c++){var d=a.childNodes[c];if(d.nodeName=="LI"){d.className="";}}Fp.byId("ratingsSubmit").className="btnLeft btnCentered btnDisabled";},submit:function(){var a=Fp.Settings.ratingsURLPost.replace("{PLACE_ID}",Fp.POI.placeId);a+="?roverQuery="+Fp.Utils.encodeXOR("ncimUserId=RoverId-"+Fp.uuid,"a2e385df4309587a597");if(this.userRating>0){Fp.Popup.showLoader();var b={ratings:{rating:[{value:this.userRating,type:"OVERALL"}]}},c=new Fp.Ajax.Request({url:a,method:Fp.Ajax.Method.POST,onSuccess:this.onSendOk,onFailure:this.onSendFail});c.sendJSON(b);}},onSendOk:function(b,d){Fp.Popup.openInfo({headerText:Language["places.label73"],subText:"",timeout:true});var a=Fp.Settings.ratingsURL.replace("{PLACE_ID}",Fp.POI.placeId),c=new Fp.Ajax.Request({url:a,method:Fp.Ajax.Method.GET,onSuccess:function(e,f){var g=Fp.Ajax.JsonParse(f);if(g.averageRatings&&g.averageRatings.averageRating){Fp.POI.rating=Fp.Utils.getRatings(g);Fp.POI.rating.userRating=Fp.Ratings.userRating;Fp.Ratings.init();}}});c.send();},onSendFail:function(a,b){Fp.Popup.openChoice({headerText:Fp.Lang.getTranslationForToken("places.ratingfailed"),subText:Fp.Lang.getTranslationForToken("places.label68"),onOk:function(){Fp.Ratings.submit();},onCancel:function(){Fp.Pages.back();}});Fp.Ratings.clearUserRatings();}};