var newsimg = [];
var newsalt = [];
var newstitle = [];
var newsdetails = [];
var i = 0;
var t;
var x = 0;
var pause = false;

var displayTime=20000; //display time (in ms)
var summaryLines=4; //Number of summary headlines to show

$(document).ready(function(){   
   $(".showitem").each(function(){
      newsimg.push($(this).find("img").attr("src"));
      newsalt.push($(this).find("img").attr("alt"));
      newstitle.push($(this).find(".newstitle").html());
      newsdetails.push($(this).find(".newsdetails").html());
    });  
  //check the number of news headlines isn't more than the total number of news items  
  if (summaryLines > newstitle.length - 1) {
    summaryLines = newstitle.length - 1;
  }
  
  addFirstItem();
  $(".news").prepend(getSummary(1));
  
  $(".news").mouseover(function(){
    pause = true;
    $(this).css("cursor","default"); 
  }).mouseout(function(){
    pause = false;
  });
  newsScroller();
});

function newsScroller() {
  if (pause == false) {
    if (x>0) {displayTime=4000;};
    setNews(x);
    if (x == newstitle.length-1) {
      x = 0;
    }
    else {
      x++;
    }
  }
  
  t=setTimeout("newsScroller()", displayTime);   
};

function addFirstItem() {
  var news;
  
  //generate html for the first news item
  news = "<div class=\"showitem\"> \r";
  news = news + "<div class=\"newsimg\"> \r <img src=\"http://library.ncin.org.uk/img/news_default.bmp\" alt=\"NCIN News\"/> \r </div> \r";
  news = news + "<p class=\"newstitle\">"+newstitle[1]+"</p> \r";
  news = news + "<p class=\"newsdetails\">"+newsdetails[1]+"</p> \r";
  news = news + "</div>";
  
  //add html before existing items
  $(".newsshow").prepend(news);
};

function setNews(item) {
  $(".showitem:first").find("img").attr("src",newsimg[item]);
  $(".showitem:first").find("img").attr("alt",newsalt[item]);
  $(".showitem:first").find(".newstitle").html(newstitle[item]);
  $(".showitem:first").find(".newsdetails").html(newsdetails[item]);
  $(".newssummary").replaceWith(getSummary(item+1));
  $("#headlines a").click(function(){
      setNews($(this).attr("id"));
  });
}

function getSummary(startItem) {
  var summary;
  var item = 0;
  
  //generate html for the news summaries
  summary = "<div class=\"newssummary\"><span id=\"headlines\">";
  while (item < summaryLines){
    if (newstitle[rollOver(item + startItem)].length > 90) {
      summary=summary + "<a href=\"#\" id=\""+ rollOver(item + startItem) +"\">" + newstitle[rollOver(item + startItem)].substring(0,90) + "...</a><br />\r";
    }
    else {
      summary=summary + "<a href=\"#\" id=\""+ rollOver(item + startItem) +"\">" +(newstitle[rollOver(item + startItem)]) + "</a><br />\r";
    }
    item++;  
  }
  summary = summary + "</span><a href=\"/about/news.shtml\">More NCIN news...<\a><\div>";
  
  return(summary);
};

function rollOver(x) {
  if (x >= newstitle.length) {
    x = x - newstitle.length;
  }
  return x;
};