/*
 * $HeadURL:: http://ambraproject.org/svn/ambra/head/ambra/webapp/src/main/webapp/javasc#$
 * $Id: init_article_related.js 7770 2009-07-07 18:51:15Z josowski $
 *
 * Copyright (c) 2006-2010 by Public Library of Science
 * http://plos.org
 * http://ambraproject.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
dojo.require("dojo.fx");

dojo.addOnLoad(
  function() {
    var almService = new alm();
    var doi = dojo.byId('doi').value;
    var articleDate = dojo.byId('articlePubDate').value;

    if(almService.validateArticleDate(articleDate)) {
      if(almService.isArticle(doi)) {
        almService.getRelatedBlogs(doi, setRelatedBlogs, setRelatedBlogsError);
        almService.getSocialBookMarks(doi, setBookmarks, setBookmarksError);
        almService.getIDs(doi, setIDs, setIDsError);

        //Make calls to fill in data in the right hand column
        almService.getCites(doi, setCitesInRHC, setCitesInRHCError);
        almService.getCounter(doi, setChartDataInRHC, setChartDataInRHCError);
      } else {
        setNoDataMessageError('relatedBookmarks');
        dojo.fadeOut({ node:'relatedBookmarksSpinner', duration: 1000 }).play();

        setNoDataMessageError('relatedBlogPosts');
        dojo.fadeOut({ node:'relatedBlogPostsSpinner', duration: 1000 }).play();

        dojo.fadeOut({ node:'relatedArticlesSpinner', duration: 1000 }).play();

        setChartDataInRHCError(null);
        setCitesInRHCError(null);
      }
    } else {
      setDelayMessageError('relatedBookmarks');
      dojo.fadeOut({ node:'relatedBookmarksSpinner', duration: 1000 }).play();

      setDelayMessageError('relatedBlogPosts');
      dojo.fadeOut({ node:'relatedBlogPostsSpinner', duration: 1000 }).play();
      dojo.fadeOut({ node:'relatedArticlesSpinner', duration: 1000 }).play();
    }
  }
);

function setBookmarks(response, args) {
  dojo.byId('relatedBookmarks').style.display = 'none';
  dojo.fadeOut({ node:'relatedBookmarksSpinner', duration: 1000 }).play();

  var numRendered = 0;
  var doi = escape(dojo.byId('doi').value);

  if (response.article.source.length > 0) {
    var html = "<ul>";

    for(var a = 0; a < response.article.source.length; a++)
    {
      var url = response.article.source[a].public_url;

      //Only list links that HAVE DEFINED URLS
      if (url) {
        html = html + "<li><a href=\"" + url + "\">" + response.article.source[a].source + " (" + response.article.source[a].count + ")</a></li>";
        numRendered++;
      }
    }

    html = html + "</ul>"
  }

  if (numRendered == 0){
    html = "<ul><li>No related bookmarks found</li></ul>";
  }

  dojo.byId('relatedBookmarks').innerHTML = html;
  dojo.fx.wipeIn({ node:'relatedBookmarks', duration: 500 }).play();
}

function setBookmarksError(message) {
  dojo.byId('relatedBookmarks').style.display = 'none';
  dojo.fadeOut({ node:'relatedBookmarksSpinner', duration: 1000 }).play();

  setError('relatedBookmarks', message);
}

function setIDs(response) {
  dojo.fadeOut({ node:'relatedArticlesSpinner', duration: 1000 }).play();

  var pubMedID = 0;

  if (response.article.pub_med) {
    pubMedID = response.article.pub_med;
    dojo.byId('pubMedRelatedURL').href="http://www.ncbi.nlm.nih.gov/sites/entrez?Db=pubmed&DbFrom=pubmed&Cmd=Link&LinkName=pubmed_pubmed&LinkReadableName=Related%20Articles&IdsFromResult=" + pubMedID + "&ordinalpos=1&itool=EntrezSystem2.PEntrez.Pubmed.Pubmed_ResultsPanel.Pubmed_RVCitation";
    dojo.fx.wipeIn({ node:'pubMedRelatedLI', duration: 500 }).play();
  }

  //Now use these values to create HTML for the Right Hand Column.
  setIDsInRHC(response);
}

function setIDsError(message) {
  dojo.fadeOut({ node:'relatedArticlesSpinner', duration: 1000 }).play();
  setError('pubMedRelatedErr', message);
  setIDsInRHCError(message);
}

function setRelatedBlogs(response, args) {
  var html = "";
  var htmlSearch = "";

  dojo.byId('relatedBlogPosts').style.display = 'none';
  dojo.fadeOut({ node:'relatedBlogPostsSpinner', duration: 1000 }).play();

  var numBlogsRendered = 0;
  var doi = escape(dojo.byId('doi').value);

  if (response.article.source.length > 0) {
    html = "";

    // If there is at least one hit for a blog site, then create a link to those blogs.
    // else, if there are zero hits for a blog site, then create a "search for title" link instead.
    for (var a = 0; a < response.article.source.length; a++) {
      var url = response.article.source[a].public_url;

      //Only list links that HAVE DEFINED URLS
      if (url && response.article.source[a].count > 0) {
        if (numBlogsRendered > 0) {
          html = html + ", ";
        }
        html = html + "<a href=\"" + url + "\">" + response.article.source[a].source
            + " (" + response.article.source[a].count + ")</a>";
        numBlogsRendered++;
      } else if (response.article.source[a].search_url != null
          && response.article.source[a].search_url.length > 0) {
        htmlSearch = htmlSearch + ", <a href=\"" + response.article.source[a].search_url
            + dojo.byId('articleTitleUnformatted').value
            + "\">" + response.article.source[a].source + "</a>";
      }
    }
  }

  if (numBlogsRendered > 0) {
    html = html + "<br/>";
  }

  html = html + "Search for related blog posts on <a href=\"http://blogsearch.google.com/blogsearch?as_q=%22"
      + dojo.byId('articleTitleUnformatted').value + "%22\">Google Blogs</a>";

  if (htmlSearch.length > 0) {
    html = html + htmlSearch;
  }

  dojo.byId('relatedBlogPosts').innerHTML = html;
  dojo.fx.wipeIn({ node:'relatedBlogPosts', duration: 1000 }).play();
}

function setRelatedBlogsError(message) {
  dojo.byId('relatedBlogPosts').style.display = 'none';
  dojo.fadeOut({ node:'relatedBlogPostsSpinner', duration: 1000 }).play();
  
  html = "Search for related blog posts on <a href=\"http://blogsearch.google.com/blogsearch?as_q=%22"
      + dojo.byId('articleTitleUnformatted').value
      + "%22\">Google Blogs</a><br/><div id=\"inlineError\"><img src=\""
      + appContext + "/images/icon_error.gif\">&nbsp;" + message + "</div>";
  dojo.byId('relatedBlogPosts').innerHTML = html;
  dojo.fx.wipeIn({ node:'relatedBlogPosts', duration: 1000 }).play();
}
