﻿// ----------------------------------------------------------------------
// Dynamically rotate the news content
// Dominic Winsor, Design Haus UK Ltd.
// 2007-09-27
// ----------------------------------------------------------------------
// 1: CSS Dependencies (put these in CSS)
//   .newsItemOff { display: none; }   /* hidden article */
//   .newsItemOn  { display: block; }  /* visible article */

// 2: HTML Dependencies (these should be in the html)
//   <div id="newsItem0" class="newsItemOff">This is an article</div>
//   <div id="newsItem1" class="newsItemOff">This is another article</div>
//     .. and so on

// 3: Script Dependencies:
//    The following is required (update if dynamically driven) to set the maximum number of articles
//   <script language="javascript" type="text/javascript">newsCount = 2;</script>

// 4: Getting started:
//   Call initRotateNews() on window.onload or some other event.
// ----------------------------------------------------------------------
var newsCount  = 0;
var newsIndex  = 0;
var lastItem   = 0;
var rotateTime = 4000; // milliseconds to display item for


// Starting point - call this onload
function initRotateNews()
{   
    // exit if no news
    if (newsCount==0) return false;
    
    // show the first article
    displayNews(newsIndex);
}

// show one specific news article from the list
function displayNews( articleID )
{
    // exit if no article, no newscount or articleID outside of range
    if (articleID==NaN || newsCount==NaN || articleID>newsCount) return;
    
    // get references to the current and previous articles
    var articlePrev = document.getElementById("newsItem"+lastItem);
    var articleCurr = document.getElementById("newsItem"+articleID);
    
    // turn off the previous article, turn on the current
    articlePrev.className = 'newsItemOff';
    articleCurr.className = 'newsItemOn';
    
    // update the last item for the next iteration
    lastItem = articleID;
           
    // move to next article, or wrap around to start if at end
    newsIndex = (newsIndex<newsCount) ? ++newsIndex : 0;
    
    // call self, after a brief delay
    window.setTimeout("displayNews(newsIndex)",rotateTime);
}