var headerId = rand(5);
var itemId = rand(getChosenArray().length-1);

/*
 * This function starts the timer and displays the next fact
 */
function pepFactScroller() {
    	displayAPepFact();
    	
    	// repeat cycle every 5 seconds
    	timer = setTimeout('pepFactScroller()', 5000);
}

/* 
 * This function stop the timer and display's the next fact
 */
function nextPepFact() {
   	if (timer != null) {
   		clearTimeout(timer);
   		timer = null;
   	}
    	
   	displayAPepFact();
}


function getElement(elementName) {
    var element;
    var elementsArray;

    //Mozilla
    elementsArray = window.document.getElementsByName(elementName);

    if (elementsArray != null) {
      if (elementsArray.length != null) {
        if (elementsArray.length == 0) {
          //No such element!
          //return null;
        } else if (elementsArray.length == 1) {
          element = elementsArray[0];
        } else {
          element = elementsArray;
        }
      }
    }

    //IE Only
    if (element == null) {
        if (window.document) {
            if (window.document.all != null) {
              element = window.document.all[elementName];
            }
        }
    }

    if (element == null) {
        element = window.document.getElementById(elementName);
    }

    return element;
}

function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n) );
}


function roundRobin(c, max) {
	if (c < max-1) {
		return ++c;
	} 
	return 0;
}

function getChosenArray() {
	var chosenArray;
	
	switch (headerId) {
		case 0: 	chosenArray = aBusiness;
							break;
		case 1: 	chosenArray = aPepPeople;
							break;
		case 2: 	chosenArray = aService;
							break;
		case 3: 	chosenArray = aPepFacts;
							break;
		case 4: 	chosenArray = aRuralAreas;
							break;																					
		case 5: 	chosenArray = aPepClothing;
							break;
		default:  headerId = 0;
							chosenArray = aBusiness;
							break
	}
	return chosenArray;
}


function getAFact() {
  var pepFact = "";
  var title = "PEP Fact ";
  

  var nextLink;
  
  if (itemId == 0) {
 		headerId = roundRobin(headerId,6); 	
  }
   
  pepFact = "<b>" + title + "</b><br>" + getChosenArray()[itemId] + '.';
  
  // If you want to see the ID of the fact
  // pepFact += "<br>" + id;
  nextLink = "<br><div align=right>" +
    	     "<a href='#' onclick='nextPepFact();'>" +
    	     "<font color=#ff0000>...More &gt;</font>" +
    	     "</a>&nbsp;&nbsp;" +
    	     "</div>";
  
  pepFact = "<div class='pepNuggetScroller'>" + pepFact + nextLink + "</div>";
  
   
  itemId = roundRobin(itemId,getChosenArray().length);
   
  return pepFact;
}
