var cookieLife = 730;  // No. of days that the cookie is valid for (default is 730 - two years)

var today = new Date();
var expiry = new Date(today.getTime() + cookieLife * 24 * 60 * 60 * 1000); 
var expired = new Date(today.getTime() - cookieLife * 24 * 60 * 60 * 1000); 

var bikky = document.cookie;

function getCookie(name) { // use: getCookie("name");
	var index = bikky.indexOf(name + "=");
	if (index == -1) return null;
	index = bikky.indexOf("=", index) + 1;
	var endstr = bikky.indexOf(";", index);
	if (endstr == -1) endstr = bikky.length;
	return unescape(bikky.substring(index, endstr));
}

function setCookie(name, value) { // use: setCookie("name", value);
	if (value != null && value != "")
	  document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
	bikky = document.cookie; // update bikky
	return getCookie(name) != null; // return false if the cookie was refused
}

function deleteCookie(name) { // use: deleteCookie("name");
	document.cookie=name + "=null; expires=" + expired.toGMTString(); // delete cookie
	bikky = document.cookie; // update bikky
	document.forms[0].cookieContent.value = formatCookie(); // display cookie content
}

function makeCookie() { // make sure the cookie is set
	var form = document.forms[0];
	form.cookieContent.value = formatCookie(); // display cookie content
}
  
function formatCookie() {
	var retValue = "";
	with (bikky) {
		for (var i=0; i < length; i++) retValue += (charAt(i) != " ") ? charAt(i) : "\n";
	}
	return retValue;
}

function Ticker(name, id, shiftBy, interval)
{
  this.name     = name;
  this.id       = id;
  this.shiftBy  = shiftBy ? shiftBy : 1;
  this.interval = interval ? interval : 100;
  this.runId	= null;

  this.div = document.getElementById(id);

  // remove extra textnodes that may separate the child nodes
  // of the ticker div

  var node = this.div.firstChild;
  var next;

  while (node)
  {
    next = node.nextSibling;
    if (node.nodeType == 3)
      this.div.removeChild(node);
    node = next;
  }

  //end of extra textnodes removal
 
  this.left = 0;
  this.shiftLeftAt = this.div.firstChild.offsetWidth;
  this.div.style.height	= this.div.firstChild.offsetHeight;
  this.div.style.width = 2 * screen.availWidth;
  this.div.style.visibility = 'visible';
}

function startTicker()
{
  this.stop();
  
  this.left -= this.shiftBy;

  if (this.left <= -this.shiftLeftAt)
  {
    this.left = 0;
    this.div.appendChild(this.div.firstChild);
  
    this.shiftLeftAt = this.div.firstChild.offsetWidth;
  }

  this.div.style.left = (this.left + 'px');

  this.runId = setTimeout(this.name + '.start()', this.interval);
}

function stopTicker()
{
  if (this.runId)
    clearTimeout(this.runId);
    
  this.runId = null;
}

function changeTickerInterval(newinterval)
{

  if (typeof(newinterval) == 'string')
    newinterval =  parseInt('0' + newinterval, 10); 
	
  if (typeof(newinterval) == 'number' && newinterval > 0)
    this.interval = newinterval;
    
    this.stop();
    this.start();
}

/* Prototypes for Ticker */
Ticker.prototype.start = startTicker;
Ticker.prototype.stop = stopTicker;
Ticker.prototype.changeInterval = changeTickerInterval;


var ticker = null; /* ticker object */
var ticker_div = null;
var pause_button = null;
var stop_button = null;

function start_ticker ()
{
	ticker_div = document.getElementById('tickerID');
	pause_button = document.getElementById('pause');
	stop_button = document.getElementById('stop');
	pause_click();
}

function pause_click ()
{
	if (ticker.runId) {
		pause_button.src = "images/barclays_ticker_play.gif";
		ticker.stop();
		setCookie('ticker', 'pause');
	} else {
		pause_button.src = "images/barclays_ticker_pause.gif";
		ticker.start();
		setCookie('ticker', 'start');
	}
	ticker_div.style.visibility = 'visible';
	stop_button.style.visibility = 'visible';
	pause_button.style.marginBottom = '0px';

}

function stop_click ()
{
	pause_button.src = "images/barclays_ticker_play.gif";
	stop_button.style.visibility = 'hidden';
	pause_button.style.marginBottom = '0px';
	ticker_div.style.visibility = 'hidden';
	ticker.stop();
	setCookie('ticker', 'false');
}
