var currentPage=1;var lastPage=1;var pageSize=5;var ownerName;var commentUrl;var commentTitle;var items;var writeComment="Scrivi il tuo commento...";var currentVote=0;var parentID=-1;$(document).ready(function(){ownerName=$("#commentOwner").val();commentUrl=$("#commentUrl").val();commentTitle=$("#commentSubject").val();loadFeedback();loadComments(1,pageSize);loadCommentForm();});$(window).scroll(function(){var inner_wrap=$(".pagerLink");if(inner_wrap.length>0&&(inner_wrap.height()-$(window).height()<=$(window).scrollTop()+140)){loadComments($(".pagerLink").attr("rel"),pageSize);}});function loadCommentForm(){$("#newCommentSubmit").click(submitComment);$("#commentBody").css({overflow:"auto",display:"block",height:"16px"});$("#commentBody").text(writeComment);$("#commentBody").bind("focus",function(){$("#commentBody").animate({height:"100px"},100);if($("#commentBody").val()==writeComment){$("#commentBody").text("");}}).bind("blur",function(){if($("#commentBody").val()==""){$("#commentBody").text(writeComment);$("#commentBody").animate({height:"16px"},100);}});}function submitComment(){$("#newCommentSubmit").unbind();if($("#commentBody").val()==writeComment||$("#commentBody").val()==""){return;}var comment_form=$("#newComment");$.ajax({type:"POST",url:"/my/comments/comments.aspx/NewComment",data:"{'url':'"+encodeURIComponent(commentUrl)+"','subject':'"+encodeURIComponent(commentTitle.replace(/\'/g,"&#39;"))+"','body': '"+encodeURIComponent($("#commentBody").val().replace("\n","[br]").replace(/\'/g,"&#39;"))+"', 'parentID': '"+encodeURIComponent(parentID)+"', 'pageSize': '"+pageSize+"', 'pageIndex': '"+(lastPage-1)+"'}",contentType:"application/json; charset=UTF-8",timeout:4000,error:function(request,error,ex){currentPage=lastPage;$("#newComment_container").fadeIn(300);alert("Impossibile inviare il commento. Riprova tra un attimo.");},beforeSend:function(){$("#newComment_container").fadeOut(300);},success:function(msg){currentPage=lastPage;$("#commentContainer").html("");loadComments(1,9990);loadCommentForm();$("#newComment_container").fadeIn(300);}});}function replyComment(pID){parentID=pID;$("#commentBody").focus();$("html,body").animate({scrollTop:$("#commentBody").offset().top},{duration:"slow",easing:"swing"});return false;}function loadFeedback(){if(commentUrl==undefined){return;}$.ajax({type:"POST",url:"/my/comments/comments.aspx/GetFeedbackVotes",data:"{'url':'"+encodeURIComponent(commentUrl)+"'}",contentType:"application/json; charset=UTF-8",dataType:"json",success:function(msg){showFeedback(msg);},error:function(request,error,exc){}});}function showFeedback(msg){currentVote=msg.d.CurrentVote;$("#like_counter_img").attr("src",(currentVote==1)?"http://gui.aspitalia.com/images/common/like_s.png":"http://gui.aspitalia.com/images/common/like.png");$("#like_counter_img").attr("title",(currentVote==1)?"ti piace, clicca per rimuovere la preferenza":"mi piace");$("#dislike_counter_img").attr("src",(currentVote==-1)?"http://gui.aspitalia.com/images/common/unlike_s.png":"http://gui.aspitalia.com/images/common/unlike.png");$("#dislike_counter_img").attr("title",(currentVote==-1)?"non ti piace, clicca per rimuovere la preferenza":"non mi piace");$("#like_counter").text(msg.d.PositiveVotes);$("#dislike_counter").text(msg.d.NegativeVotes);}function loadComments(page,ps){if(commentUrl==undefined){return;}$(".commentPager").html(" ");commentsLoadingProgress();$.ajax({type:"POST",url:"/my/comments/comments.aspx/GetComments",data:"{'url':'"+encodeURIComponent(commentUrl)+"', 'pageSize': '"+ps+"', 'pageIndex': '"+(page-1)+"', 'owner': '"+encodeURIComponent(ownerName.replace(/\'/g,"&#39;"))+"'}",contentType:"application/json; charset=UTF-8",dataType:"json",success:function(msg){$("#commentListLoading").hide();if(msg.d[0]==0){$(".paging").hide();}else{currentPage=parseInt(page);lastPage=Math.ceil(msg.d[0]/pageSize);applyTemplate(msg.d[1]);if(msg.d[0]<=pageSize){$(".totalComments").hide();}$(".totalComments_inner").text(msg.d[0]+" commenti");updatePaging();}},error:function(request,error,exc){}});}function commentsLoadingProgress(){$(".paging").unbind();$("#commentListLoading").height(25);$("#commentListLoading").addClass("loading");$("#commentListLoading").show();}function applyTemplate(posts){$("<div>").setTemplateURL("/my/comments/CommentTemplate.aspx?v=2",null,{filter_data:false}).processTemplate(posts).appendTo("#commentContainer");}function updatePaging(){if(lastPage>1&&currentPage<lastPage){$(".commentPager").html('<a class="pagerLink submitButton" href="#" rel="'+(currentPage+1)+'">Carica ancora...</a>');$(".pagerLink").click(navigatePager);}else{$(".commentPager").html(" ");}}function navigatePager(evt){evt.preventDefault();loadComments($(this).attr("rel"),pageSize);}function content_like(){content_vote(1);}function content_dislike(){content_vote(-1);}function content_vote(vote){if(currentVote!=vote){content_addVote(vote);currentVote=vote;}else{content_removeVote(vote);currentVote=0;}}function content_removeVote(vote){if(commentUrl==undefined){return;}if(vote==1){$("#like_counter").text("...");}if(vote==-1){$("#dislike_counter").text("...");}$.ajax({type:"POST",url:"/my/comments/comments.aspx/RemoveFeedback",data:"{'url':'"+encodeURIComponent(commentUrl)+"'}",contentType:"application/json; charset=UTF-8",dataType:"json",success:function(msg){showFeedback(msg);},error:function(request,error,exc){}});}function content_addVote(vote){if(commentUrl==undefined){return;}if(vote==1){$("#like_counter").text("...");}if(vote==-1){$("#dislike_counter").text("...");}$.ajax({type:"POST",url:"/my/comments/comments.aspx/AddFeedback",data:"{'url':'"+encodeURIComponent(commentUrl)+"', 'vote': '"+vote+"'}",contentType:"application/json; charset=UTF-8",dataType:"json",success:function(msg){showFeedback(msg);},error:function(request,error,exc){}});}function favoriteClick(url){if(url==null){url=commentUrl;}if($("#favoriteButton").attr("src")=="http://gui.aspitalia.com/images/common/unfavorite_big.png"){unfavorite(url);}else{favorite(url);}}function unfavorite(url){$.ajax({type:"POST",url:"/my/favorites.aspx/RemoveFavorite",data:"{'url':'"+encodeURIComponent(url)+"'}",contentType:"application/json; charset=UTF-8",dataType:"json",success:function(msg){updateFavoriteList();},error:function(request,error,exc){}});return false;}function favorite(url){$.ajax({type:"POST",url:"/my/favorites.aspx/AddFavorite",data:"{'url':'"+encodeURIComponent(url)+"'}",contentType:"application/json; charset=UTF-8",dataType:"json",success:function(msg){updateFavoriteList();},error:function(request,error,exc){}});return false;}function isFavorite(url){$.ajax({type:"POST",url:"/my/favorites.aspx/IsFavorite",data:"{'url':'"+encodeURIComponent(url)+"'}",contentType:"application/json; charset=UTF-8",dataType:"json",success:function(msg){if(msg==null){return;}if(msg.d=="0"){src="http://gui.aspitalia.com/images/common/favorite_big.png";}else{src="http://gui.aspitalia.com/images/common/unfavorite_big.png";}$("#favoriteButton").attr("src",src);updateFavoriteButtonText(src);},error:function(request,error,exc){}});}function updateFavoriteList(){if($("#FavoritesContainer").length){$("#FavoritesContainerLoading").height(25);$("#FavoritesContainerLoading").addClass("loading");$("#FavoritesContainerLoading").show();$("#FavoritesContainer").empty();$.ajax({type:"POST",url:"/my/favorites.aspx/GetAll",data:"{}",contentType:"application/json; charset=UTF-8",dataType:"json",success:function(msg){var items=msg.d;if(items.length==0){$("#FavoritesContainer").empty().html('<p class="warning margin auto centered">Non hai ancora aggiunto contenuti preferiti. Naviga tra i contenuti ed usa l\'icona <img src="http://gui.aspitalia.com/images/common/favorite.png" style="width:13px;height:13px;" title="Clicca per aggiungere ai tuoi preferiti" /> per aggiungerli subito a questa sezione!');}else{var html="<ul>";for(i=0;i<items.length;i++){html+='<li><a href="#" onclick="return unfavorite(\''+items[i].Url+'\');" title="rimuovi dai tuoipreferiti"><img src="http://gui.aspitalia.com/images/common/unfavorite.png" style="width:13px;height:13px;" title="Clicca per rimuovere dai tuoi preferiti" /></a> <a href="'+items[i].Url+'">'+items[i].Subject+"</a></li>";}$("#FavoritesContainer").empty().html(html+"</ul>");}$("#FavoritesContainerLoading").hide();},error:function(request,error,exc){}});}else{var src=$("#favoriteButton").attr("src");if(src=="http://gui.aspitalia.com/images/common/unfavorite_big.png"){src="http://gui.aspitalia.com/images/common/favorite_big.png";}else{src="http://gui.aspitalia.com/images/common/unfavorite_big.png";}$("#favoriteButton").attr("src",src);updateFavoriteButtonText(src);}}function updateFavoriteButtonText(src){if(src=="http://gui.aspitalia.com/images/common/unfavorite_big.png"){$("#favoriteButton").attr("title","è tra i tuoi contenuti preferiti. fai click per rimuoverlo.");}else{$("#favoriteButton").attr("title","aggiungi ai tuoi preferiti");}}
