/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Fang | http://www.webdeveloper.com/forum/showthread.php?t=191419
Licensed under: Public Domain

Modified by Chris Hartman with the addition
of moving forward and backward in the article rotation.
 */

//Get an index of everything in a list with the ID of "news"
function postNews() {
  aLI=document.getElementById('news').getElementsByTagName('li');
  for(var i=0; i<aLI.length; i++)  {
    aLI[i].style.display="none";
    aLI[i].getElementsByTagName('p')[0];
  }
  rotate(aLI.length-1);
};

 //Set some variables for prep
var aLI=[];
var timerRUN=null;
var Speed=6000; // change as required

//Rotate one article forward and
//loop to item "0" after the last item in the index.
function rotate(idx) {
  aLI[idx].style.display="none";
  idx=(idx<aLI.length-1)? ++idx : 0;
  aLI[idx].style.display="block";
  timerRUN=setTimeout('rotate('+idx+')', Speed);
}

//Rotate one article backward and loop to the last item
//once you try to access an item less than 0
//After that, execute rotate(idx) to continue looping forward
//in the list.
function rotateReverse(idx) {
  aLI[idx].style.display="none";
  idx=(idx==0)? aLI.length-1 : --idx;
  aLI[idx].style.display="block";
  timerRUN=setTimeout('rotate('+idx+')', Speed);
}

//Determines which item is currently in view.
function inView() {
  var newsItem=0;
  for(var i=0; i<aLI.length; i++) {
    if(aLI[i].style.display=="block") {newsItem= i;}
  }
  return newsItem;
}

//Executed by an OnClick event that cancels the timer
//rotates the article forward manually
//and resets the timer.
function nextView() {
clearTimeout(timerRUN);
rotate(inView());
}

//Executed by an OnClick event that cancels the timer
//rotates the article backward manually
//and resets the timer.
function lastView() {
clearTimeout(timerRUN);
rotateReverse(inView());
}


// Multiple onload function created by: Simon Willison
// http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  postNews();
});