//******************************************************************************
// Name: news_article_selector.js
//
// Description:
//
// Provides functions for displaying the articles on the Company News page.
//
// Requires /js/misc_funcs.js
//
// Revision History:
//
// Version 1 by tc on 23/02/2010
// File created.
//
// Copyright 2010 I.T. Dev Ltd. All Rights Reserved.
//
//******************************************************************************

//******************************************************************************
// Initialise news articles. Adds styles and attributes that should only be
// applied if JavaScript is enabled.
function init_articles()
{
  // Get news articles
  var articles = getElementsByClass("article");

  // For each article on the page...
  var index;
  for (index in articles)
  {
    var article = articles[index];

    var heading = get_heading(article);
    heading.style.cursor = "pointer";

    heading.onmouseover = function()
    {
      var title = get_title(this);
      title.style.color = "#249";
      title.style.textDecoration = "underline";
    };
    heading.onmouseout = function()
    {
      var title = get_title(this);
      title.style.color = "#38a";
      title.style.textDecoration = "none";
    };
  }

  return true;
}

//******************************************************************************
// Toggles the visibility of the selected news article.
function toggle_article(article_id)
{
  // Get article element
  var article = document.getElementById(article_id);

  // Toggle the visibility
  if (get_content(article).style.display == 'none')
  {
    // Hide all articles except the one being viewed
    hide_all_articles();
    display_article(article);

    // To stop screen jumping to article, temporarily remove article's ID
    article.id = "";

    // Add bookmark to displayed URL
    window.location.hash = article_id;

    // Restore article's ID
    article.id = article_id;
  }
  else
  {
    // Hide article again
    hide_article(article);

    // Ideally we would remove the bookmark, but we cannot do this without the
    // window returning to the top of the page
    /* window.location.hash = ""; */
  }

  // Return false to stop the screen jumping to the event's target element
  return false;
}

//******************************************************************************
// Hides all the news articles.
function hide_all_articles()
{
  // Get news articles
  var articles = getElementsByClass("article");

  // For each article on the page...
  var index;
  for (index in articles)
  {
    hide_article(articles[index]);
  }

  return true;
}

//******************************************************************************
// Returns the icon element for the article.
function get_heading(article)
{
  var elements = article.getElementsByTagName("h2");
  return elements[0];
}

//******************************************************************************
// Returns the title element in the article's heading.
function get_title(heading)
{
  var elements = heading.getElementsByTagName("span");
  return elements[0];
}

//******************************************************************************
// Returns the content element for the article.
function get_content(article)
{
  var elements = getElementsByClass("article_content", article, "div");
  return elements[0];
}

//******************************************************************************
// Displays the specified news article.
function display_article(article)
{
  // Change icon
  get_heading(article).style.backgroundImage = "url('/Company/News/collapse.jpg')";

  // Show article content
  get_content(article).style.display = 'inline';

  return true;
}

//******************************************************************************
// Hides the specified news article.
function hide_article(article)
{
  // Change icon
  get_heading(article).style.backgroundImage = "url('/Company/News/expand.jpg')";

  // Hide article content
  get_content(article).style.display = 'none';

  return true;
}

