//NEWS TICKER//
//Settings
var pxShown = 1;
var pauseTime = 3000;

var currentItem, maskObj, animate, tickcontent, tickermask, startlink, stoplink;

var stopped = new Boolean();
stopped = 0;


function moveRight(){
   maskObj.style.left = parseInt(maskObj.style.left) + pxShown + 'px';
   maskObj.style.width = parseInt(maskObj.style.width) - pxShown + 'px';
   if (parseInt(maskObj.style.left) < tickcontent.offsetWidth)
   {
     animate = setTimeout(moveRight,5);
   }
   else
   {
      maskObj.style.display = 'none';
      animate = setTimeout(change,pauseTime);   
   }
}

function change(){
     stopped = 0;
     clearTimeout(animate);
     if (x < newsArray.length - 1)
     {
        x++;
     }
     else
     {
        x = 0
     }
     tickcontent.innerHTML = newsArray[x];
     startstoplink.className = 'stop'
     startstoplink.onclick = stopButton;
     startli.setAttribute('title', 'Pause ticker');
     startspan.innerHTML = 'Pause';
     
     maskObj.style.left = 0;
     maskObj.style.display = 'inline';
     maskObj.style.width = tickcontent.offsetWidth + 'px';
     moveRight();
}

function next(){
     if (x < newsArray.length - 1)
     {
        x++;
     }
     else
     {
        x = 0
    }
     stopped = 1;
     stop();
     tickcontent.innerHTML = newsArray[x];
}


function previous(){
     if (x == 0)
     {
        x = newsArray.length - 1;
     }
     else
     {
        x = x - 1;
    }
     stopped = 1;
     stop();
     tickcontent.innerHTML = newsArray[x];
}

function stop(){
   maskObj.style.display = 'none';
   maskObj.style.left = 0;
   startstoplink.className = '' 
   startstoplink.onclick = change;
   startli.setAttribute('title', 'Play ticker');
   startspan.innerHTML = 'Play';
   clearTimeout(animate);
}

function stopButton() {
      stopped = 1;
	  stop();
}

function pause(){
   clearTimeout(animate);
}

function mouseOver()
{
     stop();
}

function mouseOut(){
    if (stopped == 0)
  {
     setTimeout(change, 1000);
  }
}

function initNewsTicker(){
   maskObj = document.getElementById('tickermask');
   tickcontent = document.getElementById('output');

   displayTicker();
   
   tickcontent.style.width = 'auto'; 
   maskObj.style.left = '0px'; 
   x=0;
   tickcontent.innerHTML = newsArray[x];
   maskObj.style.width = tickcontent.offsetWidth + 'px';
   moveRight();
}

function displayTicker(){
   maskObj = document.getElementById('tickermask');
   tickcontent = document.getElementById('output');
   startstoplink = document.getElementById('newstickerstop');
   newsfeedslink = document.getElementById('NewsFeeds');
   newstickerdiv = document.getElementById('newsticker');
   previouslink = document.getElementById('newstickerpre');
   nextlink = document.getElementById('newstickernext');
   title = document.getElementById('newstickerTitle');
   startli = document.getElementById('listart');
   startspan = document.getElementById('spanstart');

   newstickerdiv.style.height = '24px';
   maskObj.style.display = 'inline';
   startstoplink.style.display = 'block';
   previouslink.style.display = 'block';
   nextlink.style.display = 'inline';
   title.style.display = 'inline';
   newsfeedslink.style.display = 'inline';
   
   startstoplink.onclick = stopButton;
   previouslink.onclick = previous;
   nextlink.onclick = next;
}
