
var tm;if(!tm)tm={};if(!tm.siteLife)tm.siteLife={};tm.siteLife.recommendations={};function display(){var doLog=true;var uniqueId=0;var getUniqueId=function(){return'sitelife-'+(++uniqueId);}
this.getUniqueId=getUniqueId;var displayCommentCount=function(article,elementId,showComments,countPrefix,isR2_5Display,displayForKyte){_displayLogger('debug','displayCommentCount()',article);isR2_5Display=typeof(isR2_5Display)!='undefined'?isR2_5Display:false;if(isR2_5Display){displayCommentCountR25(article,elementId,showComments,displayForKyte);return;}
if(article==null){_displayLogger('error','article is not registered with SiteLife: cannot populate '+elementId);return;}else{var rootElement=document.getElementById(elementId);var commentLabel=document.createElement('span');commentLabel.setAttribute('class','header');commentLabel.setAttribute('className','header');if(parseInt(article.Comments.NumberOfComments)==0){commentLabel.appendChild(document.createTextNode('Be the first to comment'));}else{commentLabel.appendChild(document.createTextNode(countPrefix+' ('+article.Comments.NumberOfComments+')'));}
if(showComments){var ul1=document.createElement('ul');var ul2=document.createElement('ul');ul1.appendChild(commentLabel);var postCommentLinkSpan=document.createElement('span');postCommentLinkSpan.setAttribute('class','add-comment-link');postCommentLinkSpan.setAttribute('className','add-comment-link');var postCommentLink=document.createElement('a');postCommentLink.setAttribute('href','#commentsiframe');postCommentLink.setAttribute('onclick','window.location.hash=this.href.split(\'#\')[1];return false;');postCommentLink.appendChild(document.createTextNode('Add a comment'));postCommentLinkSpan.appendChild(postCommentLink);ul2.appendChild(postCommentLinkSpan);rootElement.appendChild(ul1);rootElement.appendChild(ul2);}else{rootElement.appendChild(commentLabel);}}}
this.displayCommentCount=displayCommentCount;var displayCommentCountR25=function(article,elementId,showComments,displayForKyte){if(article==null){_displayLogger('error','article is not registered with SiteLife: cannot populate '+elementId);return;}else{var commentHTML='';var rootElement=$('#'+elementId+':last');var commentLabel=$('<span class="header"></span>');var numberOfComments=parseInt(article.Comments.NumberOfComments);switch(numberOfComments){case 0:commentHTML+=!displayForKyte?'Add a comment':'';break;case 1:commentHTML+=(!displayForKyte?'Comment (':'(')+
article.Comments.NumberOfComments+')';break;default:commentHTML+=(!displayForKyte?'Comments (':'(')+
article.Comments.NumberOfComments+')';}
if(displayForKyte){commentLabel=commentHTML;}else{commentLabel=$('<span class="header">'+commentHTML+'</span>')[0];}
if(showComments){var ul1=document.createElement('ul');var ul2=document.createElement('ul');ul1.appendChild(commentLabel);var postCommentLinkSpan=document.createElement('span');postCommentLinkSpan.setAttribute('class','add-comment-link');postCommentLinkSpan.setAttribute('className','add-comment-link');var postCommentLink=document.createElement('a');postCommentLink.setAttribute('href','#commentsiframe');postCommentLink.setAttribute('onclick','window.location.hash=this.href.split(\'#\')[1];return false;');postCommentLink.appendChild(document.createTextNode('Add a comment'));postCommentLinkSpan.appendChild(postCommentLink);ul2.appendChild(postCommentLinkSpan);rootElement.append(ul1);rootElement.append(ul2);}else{rootElement.append(commentLabel);}}}
var displayCommentRecommendCount=function(article,elementId){_displayLogger('debug','displayCommentRecommendCount()',article);var htmlToPopulate='';if(article==null){var numOfComments=0;var numOfRecommends=0;}
else{var numOfComments=article.Comments.NumberOfComments;if(numOfComments==1){var commentTerm='comment';}
else{var commentTerm='comments';}
var numOfRecommends=article.Recommendations.NumberOfRecommendations;htmlToPopulate='<ul class="sitelife-counts"><li class="comment"><a href="'+article.PageUrl+'">';if(numOfComments==0){htmlToPopulate=htmlToPopulate+'Leave your comment';}
else{htmlToPopulate=htmlToPopulate+numOfComments+' '+commentTerm;}
htmlToPopulate=htmlToPopulate+'</a></li>';htmlToPopulate=htmlToPopulate+'<li class="recommend"><a href="'+article.PageUrl+'">';if(numOfRecommends==0){htmlToPopulate=htmlToPopulate+'Recommend';}
else{htmlToPopulate=htmlToPopulate+'Recommends ('+numOfRecommends+')';}
htmlToPopulate=htmlToPopulate+'</a></li></ul>';document.getElementById(elementId).innerHTML=htmlToPopulate;}}
this.displayCommentRecommendCount=displayCommentRecommendCount;var displayCommentRecommendCountR2=function(article,elementId,fullSize,showRecommend){_displayLogger('debug','displayCommentRecommendCountR2()',article);var htmlToPopulate='';showRecommend=typeof(showRecommend)!='undefined'?showRecommend:true;if(article!=null){var numOfComments=parseInt(article.Comments.NumberOfComments);var numOfRecommends=parseInt(article.Recommendations.NumberOfRecommendations);var commentTerm='';var recommendTerm='';switch(numOfComments){case 0:fullSize?commentTerm=commentTerm='Add a comment':commentTerm='';break;case 1:fullSize?commentTerm=commentTerm='Comment ('+numOfComments+')':commentTerm='('+numOfComments+')';break;default:fullSize?commentTerm=commentTerm='Comments ('+numOfComments+')':commentTerm='('+numOfComments+')';}
if(showRecommend){switch(numOfRecommends){case 0:recommendTerm='Recommend';break;default:fullSize?recommendTerm='Recommended ('+numOfRecommends+')':recommendTerm='('+numOfRecommends+')';}
htmlToPopulate='<span class="sitelife-counts"><span class="comment"><a href="'+article.PageUrl+'#sitelife-comments-bottom">'
+commentTerm+'</a></span><span class="recommend"><a href="'+article.PageUrl+'">'
+recommendTerm+'</a></span></span>';}else{htmlToPopulate='<span class="sitelife-counts"><span class="comment"><a href="'+
article.PageUrl+'#sitelife-comments-bottom">'
+commentTerm+'</a></span>';}
document.getElementById(elementId).innerHTML=htmlToPopulate;}}
this.displayCommentRecommendCountR2=displayCommentRecommendCountR2;var displayCommentCountNoRecommend=function(article,elementId){_displayLogger('debug','displayCommentRecommendCountR25()',article);if(article!=null){var numOfComments=article.Comments.NumberOfComments;if(numOfComments==1){var commentTerm='comment';}
else{var commentTerm='comments';}
var numOfRecommends=article.Recommendations.NumberOfRecommendations;var a1=document.createElement('a');a1.setAttribute('href',article.PageUrl);if(numOfComments==0){a1.appendChild(document.createTextNode('Add a comment'));}else if(numOfComments==1){a1.appendChild(document.createTextNode(article.Comments.NumberOfComments+' comment'));}
else{a1.appendChild(document.createTextNode(article.Comments.NumberOfComments+' comments'));}
document.getElementById(elementId).appendChild(a1);}}
this.displayCommentCountNoRecommend=displayCommentCountNoRecommend;var mostPopularTabs=function(mostPopularData,articleCount,useTeaser,displayVersion){var tabs={};_displayLogger('debug',mostPopularData);var headlineHandler=null;var teaserHandler=null;try{var mostCommented=mostPopularData.mostCommented;headlineHandler=function(article){return article.PageTitle+' ('+article.Comments.NumberOfComments+')';};teaserHandler=useTeaser?function(article){return'teaser';}:null;tabs.mostCommentedTab=_createListTab(mostCommented,articleCount,headlineHandler,teaserHandler,displayVersion);}catch(e){_displayLogger('error','problem with mostCommented data',e);throw e;}
try{headlineHandler=function(article){return article.PageTitle+' ('+article.Recommendations.NumberOfRecommendations+')';};var mostRecommended=mostPopularData.mostRecommended;tabs.mostRecommendedTab=_createListTab(mostRecommended,articleCount,headlineHandler,teaserHandler,displayVersion);}catch(e){_displayLogger('error','problem with mostRecommended data',e);throw e;}
_displayLogger('debug','tabs created',tabs);return tabs;}
this.mostPopularTabs=mostPopularTabs;var _createListTab=function(articleList,articleCount,headlineDisplay,teaserDisplay,displayVersion){var box=document.createElement('div');var list=document.createElement('ol');if(displayVersion=='V1'){box.setAttribute('class','Box');box.setAttribute('className','Box');var boxChild=document.createElement('div');box.appendChild(boxChild);boxChild.setAttribute('class','containerobj');boxChild.setAttribute('className','containerobj');boxChild.appendChild(list);}
else{box.setAttribute('class','mpa-pnl');box.setAttribute('className','mpa-pnl');list.setAttribute('class','mpa-l');list.setAttribute('className','mpa-l');box.appendChild(list);}
var len=Math.min(articleList.length,articleCount);for(var i=0;i<len;i++){var article=articleList[i];var li=document.createElement('li');var a1=document.createElement('a');list.appendChild(li);if(displayVersion=='V1'){var h4=document.createElement('h4');var a2=document.createElement('a');li.appendChild(h4);h4.appendChild(a1);}
else{a1.setAttribute('class','mpai-l');a1.setAttribute('className','mpai-l');if(i==len-1){li.setAttribute('class','last');li.setAttribute('className','last');}
li.appendChild(a1);}
a1.setAttribute('href',article.PageUrl);var linkText='';if(displayVersion=='V2'){linkText=linkText+(i+1)+'. ';}
linkText=linkText+_entityToString(headlineDisplay(article));a1.appendChild(document.createTextNode(linkText));if(displayVersion=='V1'&&teaserDisplay!=null){var a2=document.createElement('a');a2.setAttribute('href',article.PageUrl);a2.appendChild(document.createTextNode(_entityToString(teaserDisplay(article))));li.appendChild(a2);}}
return box;}
var _entityToString=function(stg){return stg.replace(/\&\#(\d+);/g,function(y,x){return String.fromCharCode(x);});}
var _displayLogger=function(){};if(doLog&&tm.siteLife.logger!=null){_displayLogger=function(level){tm.siteLife.logger.log(level,'tm.siteLife.display',arguments);}
_displayLogger('info','log enabled');}
var _updateALink=function(link,username,removeParams){if(link.href.match(/community\/profile/)){if(!removeParams){link.href=link.href.replace(/community\/profile/,'community/profile/'+username);}else{link.href=link.href.replace(/community\/profile.*/,'community/profile/'+username+'/');}
if(tm.siteLife.logger){tm.siteLife.logger.log('info','updateProfileLinks',['','updated link'+'-> '+link.innerHTML]);}}}
var _updateProfileHeaderLinks=function(){var loginLinks=$('.Persona_LoginStatus')[0].getElementsByTagName('A');var userName=loginLinks[0].innerHTML.replace(/<[^>]*>/g,'');if(!userName.match(/log.?in/i)){for(var l=0;l<loginLinks.length;l++){_updateALink(loginLinks[l],userName);}}}
var _updateContainerLinks=function(cName,userName){var container=$(cName);if(container.length){var personaMainLinks=container[0].getElementsByTagName('A');for(var l=0;l<personaMainLinks.length;l++){_updateALink(personaMainLinks[l],userName);}}}
var _updateOtherUserLinks=function(containerClassName){var friendsListLinks=$(containerClassName);if(friendsListLinks.length==1){friendsListLinks=friendsListLinks[0].getElementsByTagName('A');for(var l=0;l<friendsListLinks.length;l++){_updateALink(friendsListLinks[l],friendsListLinks[0].innerHTML.trim(),true);}}}
var _updatePublicMessagesLinks=function(containerClassName){var publicMessageTable=$(containerClassName);if(publicMessageTable.length){var publicMessageRows=publicMessageTable[0].getElementsByTagName('TR');for(var r=0;r<publicMessageRows.length;r++){var imgN=0;if(publicMessageRows[r].getElementsByTagName('IMG')[0].alt=='Delete Item'){imgN++;}
userName=publicMessageRows[r].getElementsByTagName('IMG')[imgN].alt.replace(/ .*/,'');var links=publicMessageRows[r].getElementsByTagName('A');for(var l=0;l<links.length;l++){_updateALink(links[l],userName,true);}}}}
var updateProfileLinks=function(){_updateProfileHeaderLinks();var userName=$('.PersonaHeader_Title')[0].innerHTML.replace(/'.*/,'').trim();_updateContainerLinks('.PersonaHeader_Container',userName);_updateContainerLinks('.PersonaTabs_Container',userName);_updateContainerLinks('.Persona_2ColumnTable',userName);_updateOtherUserLinks('.Friends_List');_updateOtherUserLinks('.Reactions_Wrapper');_updatePublicMessagesLinks('.MessagePreview_Table');_updatePublicMessagesLinks('.Messages_MessageTable');if(tm.siteLife.logger){tm.siteLife.logger.log('info','updatedProfileLinks',['','updated all profile links']);}}
this.updateProfileLinks=updateProfileLinks;function populateCount(responses){var countResponse=responses[0];if(countResponse.ResponseStatus.StatusCode==PluckSDK.ResponseStatusCode.OK){tm.siteLife.recommendations[countResponse.Article.ArticleKey.Key]=countResponse.Article.Recommendations.NumberOfRecommendations;}}
var pluck4RecommendationCount=function(articleId){var countRequest=new PluckSDK.ArticleRequest();countRequest.ArticleKey=new PluckSDK.ExternalResourceKey();countRequest.ArticleKey.Key=articleId;tm.siteLife.recommendations[articleId]=0;PluckSDK.SendRequests([countRequest],populateCount);}
this.pluck4RecommendationCount=pluck4RecommendationCount;}
tm.siteLife.display=new display();
