

  // Callback function for recommendDiscussionPost
  //
  // 1/12/2008 CB
  IncrementCounter = function(response){
//    console.log('in response');
//    console.log('message is ' + response.Messages[0].Message);
      // if successful increment the counter
      if (response.Responses[0]){
//        console.log('response is there: ' + response.Responses[0]);
        var post = response.Responses[0].ForumPost;
//        console.log('post is ' + post);
        var key = response.Responses[0].ForumPost.ForumPostKey.Key;
//        console.log('key is ' + key);
        var counterId = 'postRecommendationCount' + key;
        var counter = document.getElementById(counterId);
        var count = parseInt(counter.innerHTML);
        count += 1;
        counter.innerHTML = count;
        // hide the recommend link and show the recommended
        var recommend = document.getElementById('postRecommend' + key)
        , recommended = document.getElementById('postRecommended' + key);
        recommend.style.display = 'none';
        recommended.style.display = '';
      }
    }

// function to add a recommend link to a discussion post. Creates a "vote" for both the post and the overall discussion 
// (via an Article so we can use Discovery). Relies in this being in the template override for the showDiscussionPost
// widget
//
// 27/11/2008 CB
//
// <div class="slBmjRecommendWidget">
//   <a href="#" onclick="javascript:gSiteLife.recommendDiscussionPost('${post.ForumPostKey.Key}', '${post.ForumDiscussionKey.Key}', '${posts[0].PostTitle}'); return false;" class="SiteLife_Recommend" id="postRecommend${post.ForumPostKey.Key}" {if post.CurrentUserHasRecommended == "True"} style="display: none;" {/if} >Recommend</a> 
//   <span id="postRecommended${post.ForumPostKey.Key}" {if post.CurrentUserHasRecommended == "False"} class="SiteLife_Recommended" style="display: none;" {/if} > 
//    Recommended
//  </span>
//  (<span id="postRecommendationCount${post.ForumPostKey.Key}" >${post.NumberOfRecommendations}</span>)
// </div>
  SiteLifeProxy.prototype.recommendDiscussionPost = function(postKey, discussionKey, discussionTitle){
    var request = new RequestBatch()
    , forumPostKey = new ForumPostKey(postKey)
    , recommendAction = new RecommendAction(forumPostKey)
    , cleanedDiscussionKey = discussionKey.replace('@D', '&amp;D')
    , articleKey = new ArticleKey(cleanedDiscussionKey)
    , updateArticleAction = new UpdateArticleAction(articleKey, encodeURI(window.location), discussionTitle, new Section('recommendations'), [])
    , recommendArticleAction = new RecommendAction(articleKey)
    ;
    request.AddToRequest(recommendAction);
    request.AddToRequest(forumPostKey);
    request.AddToRequest(updateArticleAction);
    request.AddToRequest(recommendArticleAction);
    request.AddToRequest(articleKey);
    request.BeginRequest(serverUrl, IncrementCounter);
    return false;
  }
  
  // function to display the most recommended [count] group forum discussions. Relies on a div with id 'Summary_Recommended_Discussions'
  // being present, and discussions having been recommended by the recommendDiscussionPost function above.
  //
  // CB 27/11/2008
  //
  SiteLifeProxy.prototype.SummaryDiscussionsRecommended = function(count){

    var requestBatch = new RequestBatch();
    var searchSections = new Array();   
    var searchCategories = new Array();   
    var activityDisco = new Activity("Recommended");   
    var contentType = new ContentType("Article");   
    var limitToContributorsDisco = new Array();   
    requestBatch.AddToRequest(new DiscoverContentAction(searchSections,searchCategories,limitToContributorsDisco,activityDisco,contentType,15,count));   
    requestBatch.BeginRequest(serverUrl, function(responseBatch){
      if (responseBatch.Messages[0].Message == 'ok'){
        var response = responseBatch.Responses[0]
        , articles = response.DiscoverContentAction.DiscoveredContent;
        if (articles.length > 0){
          for (i = 0; i < articles.length; i++){
            var article = articles[i]
            , requestBatch2 = new RequestBatch()
            , forumDiscussionKeyString = article.ArticleKey.Key.replace('&amp;D', '@D')
            , forumDiscussionKey = new DiscussionKey(forumDiscussionKeyString);
            requestBatch2.AddToRequest(forumDiscussionKey);
            requestBatch2.AddToRequest(new ArticleKey(article.ArticleKey.Key));
            requestBatch2.BeginRequest(serverUrl, function(responseBatch2){
              if (responseBatch2.Messages[0].Message == 'ok'){
                var discussion, article;
                for (j = 0; j < responseBatch2.Responses.length; j++)
                {
                  var response = responseBatch2.Responses[j];
                  if (response.ForumDiscussion){ discussion = response.ForumDiscussion }
                  else {article = response.Article}
                }
                var doc = document
                , container = doc.getElementById('Summary_Recommended_Discussions')
                , result = container.appendChild(doc.createElement('div'))
                , avatar = result.appendChild(doc.createElement('div'))
                , avatarLink = avatar.appendChild(doc.createElement('a'))
                , avatarImg = avatarLink.appendChild(doc.createElement('img'))
                , resultTitle = result.appendChild(doc.createElement('div'))
                , titleLink = resultTitle.appendChild(doc.createElement('a'))
                , author = result.appendChild(doc.createElement('div'))
                , resultDate = result.appendChild(doc.createElement('div'));
                result.setAttribute('id', 'Summary_Recommended_Discussion_ ' + discussion.ForumDiscussionKey.Key);
                if (container.childNodes.length % 2 != 0) { result.className = 'Summary_DiscAltColor'; }

                // build the avatar
                avatarImg.setAttribute('src', discussion.LiteUser.ImageUrl);
                avatarLink.setAttribute('href', discussion.LiteUser.PersonaUrl);
                avatar.className = 'Summary_Discussions_Recommended_Avatar';
                
                // build the title
                titleLink.setAttribute('href', decodeURI(article.PageUrl));
                titleLink.innerHTML = discussion.DiscussionTitle;
                resultTitle.className = 'Summary_Discussions_Recommended_Title';
                
                // build the author
                author.innerHTML = 'By <a href="' + discussion.LiteUser.PersonaUrl + '">' + discussion.LiteUser.DisplayName + '</a>';
                author.className = 'Summary_Discussions_Recommended_Author';
                
                // build the date
                resultDate.innerHTML = RequestBatch.convertDate(discussion.FirstPostDate, true);
                resultDate.className = 'Summary_Discussions_Recommended_Date';
                
              }
            });
          }
        }
      }
    });
  }
 