function LoadComments(){var r=$(".hfItemID").val(),t="DESC";$(".sortOptions a[class='active']").html()=="Oldest to newest"&&(t="ASC");var u={appId:kservAppId,contentId:r,sortOrder:t,pageSize:commentPageSize,pageIndex:commentPageIndex},f=jQuery.param(u),e=kservApi+"comments/get",n=0;$(".comments .items .spinner").show(),$(".comments .items .loadMore").hide(),$.ajax({type:"GET",dataType:"json",cache:!1,data:f,url:e,error:function(n,t,i){$(".messages").html("<div class='message error'>Error connecting to commenting service<br /><br />"+i+"<\/div>").show()},success:function(t){var e,r,f,u;if(t!=null)if(n=t.totalCount,$(".comments .totalComments").html(n),$(".article .commentcounts .bubble").html(n),n==1?$(".comments .totalComments").append(" comment"):$(".comments .totalComments").append(" comments"),n>0){for(e=t.comments,r="",i=0;i<e.length;i++){f=e[i],u=f.username,u==null&&(u="Guest");var o=f.createdDate,s=f.comment,h="item";commentPageIndex=="0"&&i==0&&(h="item first"),r+="<div class='"+h+"'>",r+="<div class='neckline'>",o!=null&&(r+="<div class='createdDate'>Added "+commentPrettyDate(o)+"<\/div>"),r+="<div style='clear:both;'><\/div><\/div>",u!=null&&(r+="<div class='namelocal'>",r+=u,r+="<\/div>"),s!=null&&(r+="<div class='comment'>"+s+"<\/div>"),$(".comments .commentForm textarea").length>0&&(r+="<div class='leaveComment'>",r+="<a href='javascript:leaveComment();'>Add a comment<\/a>",r+="<\/div>"),r+="<\/div>"}$(".comments .items .data .resultSet").append(r)}else $(".comments .items").hide(),$(".comments .noComments").show()},complete:function(){var t,i;$(".comments .items .spinner").hide(),t=$(".comments .items .data .resultSet .item").length,parseInt(n)>parseInt(t)?(i=parseInt(n)-parseInt(t),$(".comments .items .data .loadMore a").html("Load More ("+i+")"),$(".comments .items .data .loadMore").show()):$(".comments .items .data .loadMore").hide()}})}function SetCommentFormBehavior(){$(".comments .commentForm textarea").length>0&&($(".comments .commentForm textarea").focus(function(){$(this).val()=="Type your comment here..."&&($(this).addClass("active").attr("style","height:50px").val(""),$(".comments .commentForm .button").slideDown("fast")),$(".messages").length>0&&$(".messages").slideUp("fast")}),$(".comments .commentForm textarea").blur(function(){$(this).val()==""&&($(this).removeClass("active").removeAttr("style").val("Type your comment here..."),$(".comments .commentForm .button").slideUp("fast"))}),$(".comments .commentForm textarea").keyup(function(){var t=$(".comments .commentForm textarea").val().split("\n").join("<br />."),i,n;$(".comments .commentForm .clone").html(t),i=$(".comments .commentForm textarea").height(),n=$(".comments .commentForm .clone").height(),n>i&&$(".comments .commentForm textarea").removeAttr("style").attr("style","height:"+n+"px"),t.length>0?$(".comments .commentForm .button").hasClass("disabled")&&$(".comments .commentForm .button").removeClass("disabled"):$(".comments .commentForm .button").hasClass("disabled")||$(".comments .commentForm .button").addClass("disabled")}))}function addComment(){if(!$(".comments .commentForm .button").hasClass("disabled")){$(".comments .commentForm .button").addClass("disabled"),$(".comments .commentForm .button a").html("Adding comment...  Please wait.").addClass("processing");var t=$(".hfItemID").val(),i=$(".comments .commentForm textarea").val(),r=$(".hfCVI").val(),u=$(".hfEmail").val(),f=$(".hfCustomerName").val(),e={appId:kservAppId,contentId:t,authenticationId:r,email:u,username:f,comment:i},n="",o=kservApi+"comments/add";$.get("/sitefiles/resources/commentstoken.ashx",function(t){$.ajax({type:"POST",dataType:"json",contentType:"application/json",cache:!1,headers:{"KSERV-ApplicationId":kservAppId,"KSERV-Token":t},data:JSON.stringify(e),url:o,error:function(n,t,i){$(".messages").html("<div class='message error'>Error connecting to commenting service.<br />"+i+"<\/div>").show(),$(".comments .commentForm .button a").html("Post as "+$(".hfCustomerName").val()),$(".comments .commentForm .button").hide(),$(".comments .commentForm textarea").removeClass("active").removeAttr("style").val("Type your comment here...")},success:function(t){t!=null?t.success?n=t.commentId:($(".messages").html("<div class='message error'>Error connecting to commenting service.<\/div>").show(),$(".comments .commentForm .button a").html("Post as "+$(".hfCustomerName").val()),$(".comments .commentForm .button").hide(),$(".comments .commentForm textarea").removeClass("active").removeAttr("style").val("Type your comment here...")):($(".messages").html("<div class='message error'>Error connecting to commenting service.<br />Data returned null.<\/div>").show(),$(".comments .commentForm .button a").html("Post as "+$(".hfCustomerName").val()),$(".comments .commentForm .button").hide(),$(".comments .commentForm textarea").removeClass("active").removeAttr("style").val("Type your comment here..."))},complete:function(){if(n!=""){var t="Thank you for taking the time to comment on this article.  You comment has been submitted and will appear shortly.";$(".messages").html("<div class='message success'>"+t+"<\/div>").show(),$(".comments .commentForm .button a").html("Post as "+$(".hfCustomerName").val()),$(".comments .commentForm .button").hide(),$(".comments .commentForm textarea").removeClass("active").removeAttr("style").val("Type your comment here..."),setTimeout(function(){$(".messages").fadeOut("slow")},1e4)}}})})}}function leaveComment(){$("html, body").animate({scrollTop:$(".comments .commentForm").offset().top-100},{duration:"fast",easing:"swing"})}function commentPrettyDate(n){var o=[[60,"seconds",1],[120,"1 minute ago","1 minute from now"],[3600,"minutes",60],[7200,"1 hour ago","1 hour from now"],[86400,"hours",3600],[172800,"yesterday","tomorrow"],[604800,"days",86400],[1209600,"last week","next week"],[2419200,"weeks",604800],[4838400,"last month","next month"],[29030400,"months",2419200],[58060800,"last year","next year"],[290304e4,"years",29030400],[580608e4,"last century","next century"],[580608e5,"centuries",290304e4]],t=(""+n).replace(/-/g,"/").replace(/[TZ]/g," ").replace(/^\s\s*/,"").replace(/\s\s*$/,""),e,i;t.substr(t.length-4,1)=="."&&(t=t.substr(0,t.length-4));var r=(new Date-new Date(t))/1e3,u="ago",f=1;for(r<0&&(r=Math.abs(r),u="from now",f=2),e=0;i=o[e++];)if(r<i[0])return typeof i[2]=="string"?i[f]:Math.floor(r/i[2])+" "+i[1]+" "+u;return t}var commentPageSize="20",commentPageIndex="0";$(document).ready(function(){$(".comments").length>0&&LoadComments(),$(".comments .items .data .loadMore a").click(function(){commentPageIndex=parseInt(commentPageIndex)+1,LoadComments()}),$(".comments .sortOptions a").click(function(){$(this).hasClass("active")||($(".comments .sortOptions a").removeClass("active"),$(this).addClass("active"),$(".comments .items .data .resultSet").html(""),commentPageIndex="0",LoadComments())}),SetCommentFormBehavior(),$(".comments .commentForm .button a").click(function(){addComment()}),$(".commentcounts").click(function(){$("html, body").animate({scrollTop:$(".comments").offset().top-100},{duration:"fast",easing:"swing"})}),$(".comments").length>0&&$(".commentcounts").show()})