

var charPause = 40;        // msecs between adding a new character to the message
var messagePause = 2000;   // pause between messages
var msg = -1;              // the current message in messages
var message = ''           // set to current message
var idx = 1;               // the index of the last character shown
var icon = null;           // the icon currently displaying


messageTotal = 2;

messages = {
         0:{ticker:"2012 Calendar now available--click here", title:"2012 Calendar now available--click here", text:"The 2012 calendar is now available at £7.99 plus £2 p&p.Go to accessories to order it", icon:"specialIcon"},
         1:{ticker:"NEW BOOK ON PETER --- click here", title:"NEW BOOK ON PETER--- click here", text:"A new book about Peter and his work will be available from 3/3/12.It will contain about 11,000 words and over 200 images of Peter's work thoughout his career.The book will be about 12\" by 10\" in size with 280 pages.More details to follow ", icon:"specialIcon"}}

// Called from onLoad event
function startTicker()
{
   //If there are newsitems then run the ticker - otherwise do nothing
   if (messageTotal > 0)
   {
      ticker = $("newsTicker");
      ticker.setStyle({display:'block'});
      newsFrame = $("frame");
      Event.observe(ticker, 'click', showItem);
      Event.observe(newsFrame, 'click', hideItem);
      showMessages();
   } else {
       $("newsTicker").setStyle({display:'none', border:'none'});
   }
}

function showMessages()
{
   msg += 1;
   if (msg == messageTotal) {
      msg = 0;
   }
   idx = 1;
   message = messages[msg].ticker;
   t = setTimeout(typeIt, charPause);
}

function typeIt()
{
   //type the current character of the current message
   var data = message.substr(0,idx) + suffix();
   ticker.innerHTML = data;
   idx += 1; //next character
   //if (idx == 5) return;
   if (idx > message.length) {
      t = setTimeout(showMessages, messagePause);
   } else {
      t = setTimeout(typeIt,charPause);
   }
}

function suffix()
{
   if( idx == message.length || (idx % 2) == 1) {
     return '';
   }
   else {
      return '<img id="newscursor" src="images/block_cursor.jpg">';
   }
}


function showItem()
{
   hideItem();
   //popukate template for news item
   tmpl = new Template('<div id="newstitle">#{title}</div><div id="newsparagraph">#{text}</div>')
   data = messages[msg];
   html = tmpl.evaluate(data);
   newsFrame.setStyle({display:'block'});
   icon = $( messages[msg].icon );
   $('currentIcon').src = icon.src;
   //icon.setStyle({display:'block',position:'absolute', left:'127px', top:'145px'})
   $("newsDetail").innerHTML = html;
}

function hideItem() {
   if (icon != null) {
      icon.setStyle({display:'none'}); 
   }
   newsFrame.setStyle({display:'none'});
}




