/*
 * $Id$
 * $URL$
 * 
 * Some useful display methods for sitelife data
 */

// Namespacing

var tm;if(!tm)tm={};if(!tm.siteLife)tm.siteLife={};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){_displayLogger('debug','displayCommentCount()',article);isR2_5Display=typeof(isR2_5Display)!='undefined'?isR2_5Display:false;if(isR2_5Display){displayCommentCountR25(article,elementId,showComments);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){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');var numberOfComments=parseInt(article.Comments.NumberOfComments);rootElement.innerHTML='';commentLabel.setAttribute('class','header');commentLabel.setAttribute('className','header');switch(numberOfComments){case 0:commentLabel.appendChild(document.createTextNode('Add a comment'));break;case 1:commentLabel.appendChild(document.createTextNode('Comment ('+article.Comments.NumberOfComments+')'));break;default:commentLabel.appendChild(document.createTextNode('Comments ('+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);}}}
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){_displayLogger('debug','displayCommentRecommendCountR2()',article);var htmlToPopulate='';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+')';}
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>';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){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);}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);}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){var box=document.createElement('div');box.setAttribute('class','Box');box.setAttribute('className','Box');var boxChild=document.createElement('div');box.appendChild(boxChild);boxChild.setAttribute('class','containerobj');boxChild.setAttribute('className','containerobj');var list=document.createElement('ol');boxChild.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 h4=document.createElement('h4');var a1=document.createElement('a');var a2=document.createElement('a');list.appendChild(li);li.appendChild(h4);h4.appendChild(a1);a1.setAttribute('href',article.PageUrl);a1.appendChild(document.createTextNode(_entityToString(headlineDisplay(article))));if(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;}
tm.siteLife.display=new display();