// News rotation script

var lastNum = 0;
var articleCount = 0;
var nImage = new Array();
var nHeadline = new Array();
var nTeaser = new Array();
var nLink = new Array();
var nTarget = new Array();
var imgPath = "http://amtelecom.ca/newsrotate/";

function switchNews(nNum) {
  if (nNum == 'p') {
    // Increment next value or reset count and start at 1
    if (lastNum > 0) {
      nNum = lastNum - 1;
    } else {
      nNum = ((lastNum - 1) + articleCount);
    }
    lastNum = nNum;
    displayNews("boxNewsStory",nImage[nNum],nHeadline[nNum],nTeaser[nNum],nLink[nNum],nTarget[nNum]);
    swButton(nNum);
  } else if (nNum == 'n') {
    // Increment next value or reset count and start at 1
    if (lastNum < (articleCount-1)) {
      nNum = lastNum + 1;
    } else {
      nNum = ((lastNum + 1) - articleCount);
    }
    lastNum = nNum;
    displayNews("boxNewsStory",nImage[nNum],nHeadline[nNum],nTeaser[nNum],nLink[nNum],nTarget[nNum]);
    swButton(nNum);
  } else if ((nNum == 0) || (nNum == 1) || (nNum == 2) || (nNum == 3) || (nNum == 4) || (nNum == 5) || (nNum == 6)) {
    nNum = (!nNum) ? 1 : nNum+1;
    nNum = nNum - 1;
    lastNum = nNum;
    displayNews("boxNewsStory",nImage[nNum],nHeadline[nNum],nTeaser[nNum],nLink[nNum],nTarget[nNum]);
    swButton(nNum);
  } else {
    nNum = (!nNum) ? 0 : 0;
    displayNews("boxNewsStory",nImage[nNum],nHeadline[nNum],nTeaser[nNum],nLink[nNum],nTarget[nNum]);
    swButton(nNum);
  }
}

function setCount(count) {
  articleCount = count;
}

function swButton(bNum) {
  bNum = bNum + 1;

  document.newsbp.src = imgPath + "topstory_button_left.gif";
  if (document.newsb0) {
    document.newsb0.src = (bNum == 1) ? imgPath + "topstory_button_on_1.gif" : imgPath + "topstory_button_off_1.gif";
  }
  if (document.newsb1) {
    document.newsb1.src = (bNum == 2) ? imgPath + "topstory_button_on_2.gif" : imgPath + "topstory_button_off_2.gif";
  }
  if (document.newsb2) {
    document.newsb2.src = (bNum == 3) ? imgPath + "topstory_button_on_3.gif" : imgPath + "topstory_button_off_3.gif";
  }
  if (document.newsb3) {
    document.newsb3.src = (bNum == 4) ? imgPath + "topstory_button_on_4.gif" : imgPath + "topstory_button_off_4.gif";
  }
  if (document.newsb4) {
    document.newsb4.src = (bNum == 5) ? imgPath + "topstory_button_on_5.gif" : imgPath + "topstory_button_off_5.gif";
  }
  if (document.newsb5) {
    document.newsb5.src = (bNum == 6) ? imgPath + "topstory_button_on_6.gif" : imgPath + "topstory_button_off_6.gif";
  }
  if (document.newsb6) {
    document.newsb6.src = (bNum == 7) ? imgPath + "topstory_button_on_7.gif" : imgPath + "topstory_button_off_7.gif";
  }
  
  document.newsbn.src = imgPath + "topstory_button_right.gif";
}


function displayNews(nID,nI,nH,nT,nL,nTar) {
	
  var linkTarget = (nTar == '1') ? '_blank' : '_self';
	
  var divContent = document.getElementById(nID);
  var divHTML = '';
  divHTML += '<TABLE width="590" cellpadding=3 cellspacing=0 border=0>';
  divHTML += '<TR>';
  divHTML += '<TD valign="top" width="300" rowspan=2><img src="' + nI + '" alt="" width="300" height="199" style="border: 1px solid #000"></TD>';
  divHTML += '<TD colspan=2 valign=top width="#"><H1 style="margin-top: 0px;">' + nH + '</H1>';
  divHTML += nT + ' <a href="' + nL + '" target="' + linkTarget + '">...more</a></TD>';  
  divHTML += '</TR>';
  divHTML += '<TR><TD valign=bottom>';
  divHTML += '<A href="javascript: switchNews(\'p\'); stop();"><img src="newsrotate/topstory_button_left.gif" alt="Previous" name="newsbp" width="20" height="20" border="0"></a> <a href="javascript: startstop();"><img src="newsrotate/topstory_button_pause.gif" alt="Play / Pause" width="16" height="20" name="playbtn" border="0"></a> <a href="javascript: switchNews(\'n\'); stop();"><img src="newsrotate/topstory_button_right.gif" alt="Next" name="newsbn" width="20" height="20" border="0"></a> &nbsp;';

  divHTML += ' <a href="javascript: switchNews(0); stop();"><img width="20" height="20" title="Go Green with Online Billing" src="cleardot.gif" border="0" alt="Go Green with Online Billing" name="newsb0"></img></a>';  
  divHTML += ' <a href="javascript: switchNews(1); stop();"><img width="20" height="20" title="Aylmer & Simcoe will be 100% Digital Cable" src="cleardot.gif" border="0" alt="Aylmer & Simcoe will be 100% Digital Cable" name="newsb1"></img></a>';
  divHTML += ' <a href="javascript: switchNews(2); stop();"><img width="20" height="20" title="Email Fraud Prevention Information" src="cleardot.gif" border="0" alt="Email Fraud Prevention Information" name="newsb2"></img></a>';
  divHTML += ' <a href="javascript: switchNews(3); stop();"><img width="20" height="20" title="Forest – EastLink Digital Cable is here!" src="cleardot.gif" border="0" alt="Forest – EastLink Digital Cable is here!" name="newsb3"></img></a>';
  divHTML += ' <a href="javascript: switchNews(4); stop();"><img width="20" height="20" title="Simcoe - EastLink Telephone is now available!" src="cleardot.gif" border="0" alt="Simcoe - EastLink Telephone is now available!" name="newsb4"></img></a>';
  
  divHTML += '</TD></tr>';
  divHTML += '</table>';
  // Draw content to layer
  divContent.innerHTML = divHTML;
} // End function


// Auto rotate controls    

var state=1;
var timeDelayRun = 500;
var timeDelayRotate = 5000;
var a;
var b;
var c;

// YOU ARE HERE ---- try to clear every time out via a function
function clearAll () {
  window.clearTimeout(a);
  window.clearTimeout(b);
  window.clearTimeout(c);
} // end function

function startstop() {
  if (state == 0)  {
    state = 1;
    document.images.playbtn.src = imgPath + "topstory_button_pause.gif";
    a = window.setTimeout("switchNews('n');rotater();", timeDelayRun);
  } else {
    state = 0;
    document.images.playbtn.src = imgPath + "topstory_button_play.gif";
    clearAll();
  }
} // end function

function stop() {
  state = 0;
  document.images.playbtn.src = imgPath + "topstory_button_play.gif";
  clearAll();
} // end function

function rotater() {
  if (state == 1)  {
    document.images.playbtn.src = imgPath + "topstory_button_pause.gif";
    b = window.setTimeout("switchNews('n');", timeDelayRotate);
    c = window.setTimeout("rotater();", timeDelayRotate);
  } else {
    document.images.playbtn.src = imgPath + "topstory_button_play.gif";
    clearAll();
  }
} // end function
