var timer = 0;
var interval = 600;

function init()
{
    headline = new Array;
    orientation = "horizontal"
    tickerHTMLWidth = "354"
    tickerHTMLHeight = "29"
    stopScroll = 1 
    tickerHTMLDivider = "&nbsp;&nbsp;&nbsp;&nbsp;"
}

function getHeadlines()
{
	//alert("in getHeadlines");
	var myAjax = new XHR.Request( '/entertainment/tv/microsites/B/bb7_celebrity/SSI/xml/breakingnews.xml?c4rand=' + Math.random(), 
					{ method: 'GET',
				    	  onComplete: updateTicker,
					  onError: handleError
                                        } );
	myAjax.send();
}

function handleError() 
{
	alert('handleError');
}

function updateTicker(request) {
	//alert("in updateTicker - timer is " + timer);
	init();

	// Get XML DOM
	var xmlResponse = request.getResponseXML();
	var aElements = xmlResponse.getElementsByTagName("text");

	for (var i=0; i<aElements.length;i++) {
		var a = aElements[i];
	//	alert('node value: ' + a.firstChild.nodeValue);
	//	alert('href: ' +  a.getAttribute("href"));
		headline[i] = new Array (a.firstChild.nodeValue, 
						"");
	}
        // alert("headline.length " + headline.length);

	stopTicker();

	buildTicker();
	runTicker();	
        updater = setTimeout ('getHeadlines()', interval*1000); 
}

// Build the tickerHTML and place it on the page
function buildTicker()
{
    tickerHTML='<div id="div1" style="position:relative; left:0; z-index:1;">';
    tickerHTML+='<table border="0" id="tickerElements" border="0" cellpadding="0" cellspacing="0"';
    tickerHTML+='><tr>';
    y=0;
    while (y<4)
    {
      for (x=0; x<(headline.length); x++)
      {
        tickerHTML+='<td ';
        if(orientation.toLowerCase()=="horizontal"){tickerHTML+='nowrap';} if(stopScroll==1){tickerHTML+=' onMouseOver="stopTicker();" onMouseOut="setWidth()"';}
        tickerHTML+='><p>';
       tickerHTML+=headline[x][0];        
        tickerHTML+='</p></td>';
              
        if(tickerHTMLDivider.toLowerCase() != "none"){tickerHTML+='<td><p>'+tickerHTMLDivider+'</p></td>';}
      }
      y++
    }
    tickerHTML+='</tr></table></div>';
    var tickerDiv=document.getElementById("tickerDiv"); 
    tickerDiv.innerHTML=tickerHTML;
}
// Ensure the width of the tickerHTML is divisible by 2. This allows smooth flowing of the scrolled content
function setWidth()
{ 
  tableObj=document.getElementById("tickerElements"); 
  obj=document.getElementById("div1");   
  objWidth=(orientation.toLowerCase()=="horizontal")?getOffset(tableObj,"width"):getOffset(tableObj,"height");
  HalfWidth=Math.floor(objWidth/2);
  newWidth = (HalfWidth*2)+2;
  obj.style.width=newWidth+"px"
  moveLayer(obj, newWidth);
 
}

// Move the layer by one pixel to the left
function moveLayer(obj, width)
{
  
  maxLeft = (0-(width/2)+2)/2
  if(orientation.toLowerCase()=="horizontal"){
    obj.style.left=(parseInt(obj.style.left) <= maxLeft)?0+"px":parseInt(obj.style.left)-1+"px"
  }else{
    if(obj.style.top==""){obj.style.top="0px";}
 // alert(obj.style.top)
    if (parseInt(obj.style.top)<(0-(width/2)+6)){
      obj.style.top = "0px"
    }else{
      obj.style.top = parseInt(obj.style.top)-1+"px"
    }
  }
  timer = setTimeout ("moveLayer(obj, "+width+");", 25); 
}

// Get width and height of layer
function getOffset(obj, dim) 
{
  if(dim=="width")
  {
    oWidth = obj.offsetWidth
    return oWidth
  }  
  else if(dim=="height")
  {
    oHeight = obj.offsetHeight
    return oHeight
  }    
}

function stopTicker()
{
  if (timer != 0) {
  	clearTimeout(timer)  
  }
}

function runTicker(){
  setWidth()
}
