
 /**
  * This file populates the news section of the website
  */

  // Globals
  var startNo = 0;
  var showNo  = 3;
  var newsBox = null;

  // Add load event
  addEvent(window, "load", news_init);

  /*
  This functions loads the news XML
  */
  function news_init()
  {
      newsBox = document.createElement("DIV");
      newsBox.style.display  = "none";
      newsBox.style.width    = "412px";
      newsBox.style.height   = "290px";
      newsBox.style.backgroundColor = "#FFFFB7";
      newsBox.style.border   = "2px solid #A8A800";
      newsBox.style.position = "absolute";
      newsBox.style.top      = "220px";
      newsBox.style.left     =  "18px";
      newsBox.style.padding  = "10px 20px 20px 20px";
      newsBox.style.fontSize = "11px";
      newsBox.style.overflow = "auto";

      var newsBoxContent = document.createElement("DIV");
      newsBoxContent.id  = "newsBoxContent";
      newsBox.appendChild(newsBoxContent);

      var button      = document.createElement("INPUT");
      button.type     = "button";
      button.value    = "Close";
      button.position = "absolute";
      button.bottom   = "10px";
      button.right    = "10px";
      newsBox.appendChild(button);
      addEvent(button, "click", hideItem);

      document.getElementById("contentInner").appendChild(newsBox);

      //var filename = "forum/rss.php?f=1&t=1&s=" + startNo + "&c=" + showNo;
      var filename = "feed.asp";

      // Show loading animation
      document.getElementById("loadingAni").style.display = "";
      document.getElementById("ajaxNews").style.display = "none";

      // Create XML object
      var xml = camsoftXML.XMLHttpRequest();
      xml.open("GET", filename, true);
      xml.onreadystatechange = function (){ build_news(xml) };
      xml.send(null);
  }

  /*
  This functions constructs the HTML for the news
  section based on the XML data.
  */
  function build_news(xml)
  {
      if(xml.readyState == 4)
      {
          // Get news items
          var items = xml.responseXML.getElementsByTagName("item");

          // Get ajaxNews DIV
          var ajaxNewsDIV = document.getElementById("ajaxNews");

          // Remove existing content
          ajaxNewsDIV.innerHTML = "";

          var q = 0;

          // Loop for each RSS item
          for(var i = startNo, j = items.length; i < j; i++)
          {
              if(!items[i] || q == showNo) break;

              // Get data
              var title       = items[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
              var description = items[i].getElementsByTagName("description")[0].childNodes[0].nodeValue.replace(/(<([^>]+)>)/ig,"");
              var desc        = items[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
              var link        = items[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;

              // Create title link
              var titleLink = document.createElement("A");
              titleLink.className = "newsLink";
              titleLink.innerHTML = title.substr(0, 26) + ((title.length > 26)? "..." : "");
              titleLink.title     = title;
              titleLink.href      = "javascript:void(0);";
              addEvent(titleLink, "click", showItem(title, desc));

              // Append Link to ajaxNewsDIV
              ajaxNewsDIV.appendChild(titleLink);

              // Create descText
              var descText = document.createElement("DIV");
              descText.className = "newsText";
              descText.innerHTML =  description.substr(0, 85) + ((description.length > 47)? "..." : "");

              // Append descText to ajaxNewsDIV
              ajaxNewsDIV.appendChild(descText);

              q++;
          }

          // Hide loading animation
          document.getElementById("loadingAni").style.display = "none";
          ajaxNewsDIV.style.display = "";
      }
  }

  function hideItem()
  {
      newsBox.style.display = "none";
      document.getElementById("newsBoxContent").innerHTML = "";
  }

  function showItem(title, desc)
  {
      return function()
      {
          document.getElementById("newsBoxContent").innerHTML = "<h1>" + title + "</h1>" + desc + "<br />&nbsp;";
          newsBox.scrollTop = "0";
          newsBox.style.display = "";
      }
  }

  function prevNews()
  {
      if(startNo > 0)
      {
          startNo = startNo - showNo;
          news_init();
      }
  }

  function nextNews()
  {
      startNo = startNo + showNo;
      news_init();
  }
