var i123 = false;var req;var m567;var h345 = 40;var c654 = 50;
var s345 = 5000;var ch456;var sh55;var cs009;
var s34500 = 11000;
var sh55;var theStorySummary;var theTargetLink;var hws64;var sh55Length; var theHomeLink;
var stopped = false;var lu7 = "";var nt;var lhs33;document.onkeyup = kc44;var paused = false;var hasFocus = true;function sTr781() {	try{		v = document.getElementById("actionButton");v.value = "Stop FeedTV";window.clearTimeout(m567);
		theHomeLink = req.responseXML.getElementsByTagName("link")[0].firstChild.nodeValue;
		document.getElementById("whichFeed").innerHTML = '<small><a class="newsTitle" href="' + theHomeLink  + '">' + req.responseXML.getElementsByTagName("title")[0].firstChild.nodeValue + '<\/a><\/small>';
		
		nt = req.responseXML.getElementsByTagName("type")[0].firstChild.nodeValue;
		items = req.responseXML.getElementsByTagName("item");theItemCount = items.length;cs009 = -1;
		theCurrentLength = 0;
		if (document.getElementById) {
				document.getElementById("news").innerHTML = newsTable;
				theAnchorObject = document.getElementById("tickerAnchor");
				theStoryObject = document.getElementById("tickerStory");
				theIndexObject = document.getElementById("tickerIndex");
				theHeaderObject = document.getElementById("headerTicker");
				

				soHt70();
			} else {
				return true;		}	}	catch(e){		showError();	}}
				function soHt70(){
	if( stopped )return false;if( paused || !hasFocus){
		m567 = window.setTimeout("soHt70()", 100);
		return;
	}theStoryObject.innerHTML = "";theAnchorObject.innerHTML = "";theHeaderObject.innerHTML = "";
	lhs33 = "";	cs009++;if( cs009 < 0 ){cs009 = theItemCount - 1;
	}else if( cs009 >= theItemCount ){playNextFeed(true);return;}
	
	try{
		hS = Math.floor(cs009 / 10);
		i=hS*10;
		last = i+10;
		if(last > theItemCount  )
			last = theItemCount;
		var heads = '<ul id="navlist"><li id="title"><a>'+ (i+1) + ' - ' + last +'</a></li>';

		for(j=1; j<=10 && i<=(theItemCount - 1); i++, j++){
			t = getElementTextNS("", "title", items[i], 0);
			if( i == cs009 ){
				if( j != 10 )
					heads += '<li id="active"><a href="#" onClick="pp('+j+');">'+j+'. '+ t+'</a></li>';
				else
					heads += '<li id="active"><a href="#" onClick="pp('+j+');">0. '+ t+'</a></li>';

			}
			else{
				if( j != 10 )
					heads += '<li><a href="#" onClick="pp('+j+');">'+j+'. '+t +'</a></li>';
				else
					heads += '<li><a href="#" onClick="pp('+j+');">0. '+t +'</a></li>';
			}
		}
		heads +='</ul>';

		document.getElementById("itemList").innerHTML =  heads ;



	}
	catch(e){}


	try{pubDate = getElementTextNS("", "pubDate", items[cs009], 0);
		document.getElementById("feedDate").innerHTML =  pubDate ;
		theTargetLink = getElementTextNS("", "link", items[cs009], 0);
		sh55 = getElementTextNS("", "title", items[cs009], 0);
		theStorySummary = getElementTextNS("", "description", items[cs009], 0);
	}catch(e2){}
	sh55Length = sh55.length;if(theStorySummary == null || theStorySummary.length == 0 )
		theStorySummary = "Please click on the link for more details of this news";
	ch456 = 0;theAnchorObject.href = theTargetLink;	theIndexObject.innerHTML = "[" + (cs009+1) + " of " + theItemCount +" ]";
	hws64 = sh55.split(' ');
	anim = document.getElementById("animation");
	if( ! anim.checked ){
		runHT000();
		return true;
	}
	var randomnumber=Math.floor(Math.random()*6);
	if(randomnumber == 0)m567 = window.setTimeout("runHT67()", h345);else
	if(randomnumber == 1)	m567 = window.setTimeout("runHT68()", h345);
	else if(randomnumber == 2)		m567 = window.setTimeout("runHT69()", h345);
	else if(randomnumber == 3)
		m567 = window.setTimeout("runHT70()", h345);else
	if(randomnumber == 4) m567 = window.setTimeout("runHT71(true)", h345);
	else	m567 = window.setTimeout("runHT71(false)", h345);
}
function runHT000(){	if( stopped )
		return false; 
if( paused || !hasFocus){	
	m567 = window.setTimeout("runHT000()", 100);
		return;
	}
	theHeaderObject.innerHTML = sh55;
		sCt1();
}
var countDown;
function rCT000(){

	if( stopped )return false;
	if( paused || !hasFocus){		
		m567 = window.setTimeout("rCT000()", 100);
		return;
	}
	countDown = s34500/1000;
	if(nt == 2){
		theStoryObject.innerHTML = '<p align="center"><a href="'+ theTargetLink +'"><img  border="0" src="'+ theTargetLink +'" width="200" height="200" align="absmiddle"><\/a><\/p>';
		theCurrentLength = 0;
		//m567 = window.setTimeout("soHt70()", 1000);
		m567 = window.setTimeout("rCT0001()", 1000);
		return true;
	}	
	else{
		theStoryObject.innerHTML = theStorySummary;
		//m567 = window.setTimeout("soHt70()", 1000);
		m567 = window.setTimeout("rCT0001()", 1000);
	}
}

function rCT0001(){
	if( paused || !hasFocus){	
	m567 = window.setTimeout("rCT0001()", 100);
		return;
	}

	countDown -- ;
	theIndexObject.innerHTML = "[" + (cs009+1) + " of " + theItemCount +" ] &nbsp;&nbsp;"+ countDown ;
	if( countDown  >= 0 ){
		m567 = window.setTimeout("rCT0001()", 1000);
	}
	else{
		soHt70();
	}
}


function runHT67(){	if( stopped )
		return false; if( paused || !hasFocus){	m567 = window.setTimeout("runHT67()", 100);
		return;
	}
	try{
	theHeaderObject.innerHTML = sh55.substring(0,ch456); }catch(e){}
	if(ch456 < sh55.length) {
		ch456++;
		m567 = window.setTimeout("runHT67()", h345);
	} else {
		//start the content ticker
		sCt1();
	}}function runHT68(){if( stopped )	return false;	if( paused || !hasFocus){
		m567 = window.setTimeout("runHT68()", 100);
		return;}
	lhs33  = lhs33 +"  "+ hws64[ch456];	try{theHeaderObject.innerHTML = lhs33;}catch(e){}
	ch456++;if(ch456 < hws64.length) {		m567 = window.setTimeout("runHT68()", 60);
	} else {	sCt1();
	}}function fill(s, l, st){	if( st ){for(i=s.length; i<l; i++){
			s = "&nbsp;"+s;	}}
	else{for(i=s.length; i<l; i++){	s = s + "&nbsp;";}}	return s;}function runHT69(){
	if( stopped )return false;if( paused || !hasFocus){	m567 = window.setTimeout("runHT69()", 100);
		return;}lhs33  = lhs33+"  "+ hws64[ch456];rS = fill(lhs33 , sh55Length , true);
	try{theHeaderObject.innerHTML = rS; }catch(e){} ch456++;
	if(ch456 < hws64.length) {		m567 = window.setTimeout("runHT69()", 60);} else {		sCt1();	}}function runHT70(){
	if( stopped )	return false;	if( paused || !hasFocus){
		m567 = window.setTimeout("runHT70()", 100);
		return;
	}lhs33  = lhs33+"  "+ hws64[ch456];
	rS = fill(lhs33 , sh55Length , false);
	try{
	theHeaderObject.innerHTML = rS;}catch(e){}

	ch456++;if(ch456 < hws64.length) {
		m567 = window.setTimeout("runHT70()", 60);} else {	sCt1();}}function runHT71(t){
	if( stopped )	return false;
	if( (paused || !hasFocus) && t){		m567 = window.setTimeout("runHT71(true)", 100);
		return;
	}	else
	if( (paused || !hasFocus ) && !t){		m567 = window.setTimeout("runHT71(false)", 100);
		return;
	}rS = sh55.substring(0,ch456);rS = fill(rS, sh55Length , t);
	try{theHeaderObject.innerHTML = rS;	}catch(e){} if(ch456 < sh55.length) {
		ch456++;	if(t)		m567 = window.setTimeout("runHT71(true)", 60);
		else			m567 = window.setTimeout("runHT71(false)", 60);
	} else {	sCt1();
	}}function sCt1(){
	if( stopped )
		return false;if( paused || !hasFocus){
		m567 = window.setTimeout("sCt1()", 100);		return;
	}	h = theHeaderObject.innerHTML
	theHeaderObject.innerHTML = "";
	theAnchorObject.innerHTML =  h;	theCurrentLength = 0;	
	anim = document.getElementById("animation");

	if( ! anim.checked ){
		rCT000();
		return true;
	}
	
	
	rCT1();
	
	}function rCT1(){

	if( stopped )		return false;
	if( paused || !hasFocus){		m567 = window.setTimeout("rCT1()", 100);
		return;
	}
	if(nt == 2){
		theStoryObject.innerHTML = '<p align="center"><a href="'+ theTargetLink +'"><img  border="0" src="'+ theTargetLink +'" width="200" height="200" align="absmiddle"><\/a><\/p>';
		theCurrentLength = 0;
		m567 = window.setTimeout("soHt70()", s345);
		return true;
	}	try{theStoryObject.innerHTML = theStorySummary.substring(0,theCurrentLength);}catch(e){}
   	if(theCurrentLength < theStorySummary.length) {
		theCurrentLength++;
		m567 = window.setTimeout("rCT1()", c654);} else {
		theCurrentLength = 0;	m567 = window.setTimeout("soHt70()", s345);}}function loadXMLDoc(url) {
	lu7 = url;
	stopped = false;
	paused = false;
		focusGained();
	// Display loading flag
	startLoadFlag();
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {	i123 = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {		req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();	}	}}function processReqChange() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			stopLoadFlag();
			sTr781();
		} else {
			showError();
		}
	}
}

 var errorMsg= 	'<p align="center">&nbsp;<\/p>'+
		'<p align="center"><font face="Garamond" color="#800080" size="4">'+
		'<span style="background-color: #E8EEFA">Sorry..Unable to show the Feeds from the selected channel.<br>'+ 
		'Please select another channel.<\/span><\/font><\/p>'+
		'<p>&nbsp;<\/td>';

 var loadMsg= 	'<p align="center">&nbsp;<\/p>'+
		'<p align="center"><font face="Garamond" color="#800080" size="4">'+
		'<span style="background-color: #E8EEFA"><img border=\"0\" src=\"../images/indicator.gif\" width=\"16\" height=\"16\"> Please wait while the feeds'+ 
		' are being loaded..<\/span><\/font><\/p>'+
		'<p>&nbsp;<\/td>';

 var openMsg= 	'<p align="center">&nbsp;<\/p>'+
		'<p align="center"><font face="Garamond" color="#800080" size="4">'+
		'<span style="background-color: #E8EEFA"><img border=\"0\" src=\"../images/indicator.gif\" width=\"16\" height=\"16\"> Please wait while the requested '+ 
		' page is being loaded.<br> Press your browser back button to return to FeedTV.<\/span><\/font><\/p>'+
		'<p>&nbsp;<\/td>';


var newsTable = '<table border="0" cellpadding="5" cellspacing="1" style="border-collapse: collapse" bordercolor="#F0F0F0" width="90%" >'+
                '<tr><td width="100%"><p align="center"><b><font face="Book Antiqua"><span id="headerTicker"><\/span><a  id="tickerAnchor" href="#"><\/a></font></b></td>'+
                '</tr><tr><td width="100%" align="left"><font face="Verdana"><span id="tickerStory"><\/span><\/font></td></tr>'+
				'<tr><td width="100%" align="center"><font face="Garmond" color="#808000" size="2"><span id="tickerIndex"><\/span><\/font></td></tr></table>';



// DISPLAY LOADING ANIMATION
function startLoadFlag() {window.clearTimeout(m567);	cs009 = -1;
	sh55 = "";	theStorySummary = "";	theTargetLink = "";	ch456 = -1;	document.getElementById('news').innerHTML = loadMsg;
}function stopLoadFlag() {	document.getElementById('news').innerHTML = "";
}function showError() {	document.getElementById('news').innerHTML = errorMsg;}function doAction(v){
	if(v.value == "Stop FeedTV"){
		window.clearTimeout(m567);
		stopped = true;
		v.value = "Start FeedTV";
	}
	else{
		window.clearTimeout(m567);
		stopped = false;
		paused = false;		v.value = "Stop FeedTV";	focusGained(); soHt70();	}	
		
		}
		function startFeedTV(){
				hasFocus = true;
	v = document.getElementById('feed');
	try{
		if( v.value != null && v.value.length > 0 ){
			loadXMLDoc(v.value);		}	}	catch(e){}}function playNextFeed(d){
	v = document.getElementById('feed');
		hasFocus = true;
	  paused = false;
	  stopped = false;
	  focusGained();


	try{
		currentSelected = v.selectedIndex;
		
		if( d ){
			if(currentSelected==v.options.length-1){
				v.selectedIndex = 0;
			}
			else{
				v.selectedIndex = (currentSelected+1);
			}
		}
		else{
			if(currentSelected==0){
				v.selectedIndex = v.options.length-1;
			}
			else{
				v.selectedIndex = (currentSelected-1);
			}
		}		if( v.value != null && v.value.length > 0 ){			loadXMLDoc(v.value);	}	}	catch(e){}
		
		}function getElementTextNS(prefix, local, parentElem, index) {
	var result = "";
	if (prefix && i123) {
		result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
	} else {
		result = parentElem.getElementsByTagName(local)[index];
	}
	if (result) {
		if (result.childNodes.length > 1) {
			return result.childNodes[1].nodeValue;		} else {			return result.firstChild.nodeValue;		}	} else {
		return "n/a";}}function kc44(e){  	 var KeyID = (window.event) ? event.keyCode : e.keyCode;

		var cPressed = false;
		var aPressed = false;
		if (document.all) {
		cPressed = event.ctrlKey ? true : false;
		aPressed = event.altKey ? true : false;
	  }
	  else if (document.getElementById) {
		cPressed = e.ctrlKey ? true : false;
		aPressed = e.altKey ? true : false;

	  }
	  else if (document.layers) {
		cPressed = e.modifiers & Event.CONTROL_MASK ? true : false;
		aPressed = e.modifiers & Event.ALT_MASK ? true : false;
	  }

  
   switch(KeyID)
   {
	  case 45:
	  case 96:	  
      case 48:
		  pp(10);
		  break;
      case 35:
      case 97:
      case 49:
		  pp(1);
		  break;
      
      case 98: 
      case 50:
		  pp(2);
		  break;
      case 34:
      case 99: 
      case 51:
		  pp(3);
		  break;
      
      case 100:  
      case 52:
		  pp(4);
		  break;
      case 12:
      case 101:
      case 53:
		  pp(5);
		  break;
      
      case 102:
      case 54:
		  pp(6);
		  break;

      case 36:
		  try{
			window.open(theHomeLink);
		  }
		  catch(e){}
		break;

      case 103:
      case 55:
		  pp(7);
		  break;

      case 104:
      case 56:
		  pp(8);
		  break;
      
	  case 33:
      case 105:
      case 57:
		  pp(9);
		  break;

      case 13:
	if(cPressed ){
		  focusLost();
	  try{
		window.clearTimeout(m567);
		document.getElementById('news').innerHTML = openMsg;
    	window.location = theTargetLink;
	  }
	  catch(e){}
	  }
	  else
	if(aPressed ){
	  try{
    	window.open(theTargetLink);
	  }
	  catch(e){}
	  }


	  break;
      case 32:
      paused = !paused;

	  if(  paused ){
		  focusLost();
	  }
	  else{
		  focusGained();
	  }
      break;

      case 37:
		  paused = false;
		  stopped = false;
		  focusGained();
	  window.clearTimeout(m567);
      cs009 = cs009-2 ;
      soHt70();
      break;

      case 38:
		  paused = false;
		  stopped = false;
		  focusGained();
	  window.clearTimeout(m567);
      playNextFeed(true);
      break;

      case 39:
		  paused = false;
		  stopped = false;
	  focusGained();
	  window.clearTimeout(m567);
      soHt70();
      break;

      case 40:
		  paused = false;
		  stopped = false;
		  focusGained();
	  window.clearTimeout(m567);
      playNextFeed(false);
      break;
   }


}

function focusLost(){
	hasFocus = false;
	//if(!paused){
		document.getElementById('actionButton').value = "Start FeedTV";	
	//}
}

function focusGained(){
	hasFocus = true;
	if(!paused){
		document.getElementById('actionButton').value = "Stop FeedTV";	
	}
}

function pp(c){
	c=c-1;
	hS1 = Math.floor(cs009 / 10);
	i1 = hS1 * 10;

	i1 = i1 + c;
	if(i1 > theItemCount -1  )
		return;

	  paused = false;
	  stopped = false;
	  focusGained();
	  window.clearTimeout(m567);

	cs009 = i1-1;
	soHt70	();
}
