//Defining global variables
var arrNewsItems = new Array();
var intTickSpeed = 3000;
var intTickPos = 0;
var tickLocked = false;
var fadeTimerID;
var autoTimerID = 0;
var intTypeSpeed = 10;
var intCurrentPos = 0;
var currentText = '';
var currentLink = '';
var strText = '';
var isFirstPass = true;
var typeInterval;

//--First function to be called when the document is ready
function initButtons(RSSurl) 
{	
	//--Loading the contents coming from given URL..
	loadContent(RSSurl);
	//--Events to occur as per 'next' and 'previous' button clicks.
  	var kids = document.getElementsByTagName('img');
  	for (var i=0; i < kids.length; i++) 
		{
			kids[i].onclick = buttonClick;
			kids[i].onmousedown = buttonDown;
			kids[i].onmouseup = buttonUp;
			kids[i].oncontextmenu = buttonMenu;
	  	}
	//--Getting the elements in the main 'span' that holds the ticker news..	
	getLinkElems();
	//--Playing Ticker..
	playFirstTicker();
}
//--end of FUNCTION

function loadContent(RSSurl)    
{
	var dataString = new String();
	//--Function to get the XmlHttp request object.
	var ajaxObject = GetXmlHttpObject();
	//--To render the responseText in text/xml.
	if (ajaxObject.overrideMimeType)
		{
			ajaxObject.overrideMimeType('text/xml');
		}
	ajaxObject.onreadystatechange = function() {
		if (ajaxObject.readyState == 4) 
		{
		   //--When request object's readystate changes to '4' (or when the state is 'ready'), retrieve the response	
		   dataString = ajaxObject.responseText;
		   var root;
		   var channels;
		   var xmlDoc;
		   		//--code for IE
				if (window.ActiveXObject) 
					{
						xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
						xmlDoc.loadXML(dataString);
					}
				//--code for Mozilla, Firefox, Opera, etc.
				else if (document.implementation && document.implementation.createDocument) 
					{
						xmlDoc = (new DOMParser()).parseFromString(dataString, "text/xml");	
					}
				else  
					{
						alert('Your browser cannot handle this script');
					}
				if (dataString=='')
				{
					alert('Failed to load the content!');
					return
				}
				
				//--Retrieving rss elements and storing in an array.. 
				root = xmlDoc.getElementsByTagName("rss");
				channels = root[0].getElementsByTagName("channel");
				var items = channels[0].getElementsByTagName("item");
				for (i = 0; i < items.length; i++)
				{
					var RSStitle = items[i].getElementsByTagName("title")[0];
					var Rsslink = items[i].getElementsByTagName("link")[0];
					arrNewsItems.push(RSStitle.firstChild.nodeValue,Rsslink.firstChild.nodeValue);
				}
				//--Items stored in array 'arrNewsItems'.
			}
		}
		ajaxObject.open("GET", RSSurl, true);
		ajaxObject.send(null);
}
//--end of FUNCTION
	
function GetXmlHttpObject()
{
	var xmlhttp;
	if (window.XMLHttpRequest)    
		{
			//--code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		}
	else if (window.ActiveXObject)
		{
			//--code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	else
		{
		alert("Your browser does not support XMLHTTP!");
		}
	return xmlhttp;
} 
//--end of FUNCTION
	
//-------Functions to handle 'next' and 'previous' buttons events-------
function buttonMenu(e) {
	return false;
}
function buttonDown(e) {
	if (!e) var e = window.event;
	if ((tickLocked == false) && (e.button != 2)) {
		//document.getElementById(this.id).style.cssText = "margin: 2px 0px 0px 2px;";
	}
}
function buttonUp(e) {
	if (!e) var e = window.event;
	if ((tickLocked == false) && (e.button != 2)) {
		//document.getElementById(this.id).style.cssText = "";
	}
}
function buttonClick(e) {
  delayTicker();
  if (this.id == "back") {
    prevArticle();
  } else if (this.id == "next") {
    nextArticle();
  }
}
//-------End of FUNCTIONS------------

//--Function to display last news.
function prevArticle() 
{
	if (tickLocked == false) 
  	{
		if (intTickPos == 0)
		{
			intTickPos = arrNewsItems.length;
	  	}
		else
		{
			intTickPos=intTickPos-2
		}
		setArticle(intTickPos);
	}
}
//--end of function

//--Function to display next news.
function nextArticle()
{	
	if (tickLocked == false)
	{
		if (intTickPos == arrNewsItems.length) 
		{
	    	intTickPos = 0;
	  	}
		else
		{
			intTickPos = intTickPos + 2;
		}
		setArticle(intTickPos);
	}
}
//--end of function

//--Function to type the news one letter at a time.
function typeText() 
{
	//--Case: When current position of cursor is less then news' total length.
	if(intCurrentPos < currentText.length) 
	{
		strText += currentText.charAt(intCurrentPos);
		setSpan(strText,currentLink);
		intCurrentPos++;		
    	document.getElementById("cursor").style.display='';
		
	}
	//--Case: When current position of cursor reaches news' total length.
	else if (intCurrentPos == currentText.length) 
	{
		strText += currentText.charAt(intCurrentPos);
		setSpan(strText,currentLink);
		clearInterval(typeInterval);
		document.getElementById("cursor").style.display='none';
	} 
	//--Case: When current position of cursor becomes graeter than news' total length.
	else if (intCurrentPos > currentText.length)
	{
		setSpan(strText,currentLink);
		clearInterval(typeInterval);
		document.getElementById("cursor").style.display='none';
	}
}
//--end of function

//--Function to prepare the 'span' that holds news.
function setSpan(strText, strLink) 
{
	var tickElem = document.getElementById("tick");
	var tickFirstChild =  tickElem.firstChild;
	var tickLinkElem = document.createElement("a");
	tickLinkElem.setAttribute('href', strLink);
	tickLinkElem.setAttribute('target', '_top');
	tickText = document.createTextNode(strText);
	tickLinkElem.appendChild(tickText);
	tickElem.replaceChild(tickLinkElem,tickFirstChild);
	getLinkElems();
}
//--end of function

//--Function to get the elements holding the ticker news.
function getLinkElems() 
{
	var tickerElem = document.getElementById("tick"); 
	var tickerAElem = tickerElem.getElementsByTagName("a"); 
	for (var i=0; i < tickerAElem.length; i++) 
	{
    	tickerAElem[i].onmouseover = stopTicker;
   		tickerAElem[i].onmouseout = resumeTicker;
  	}
}
//--end of function

//--Function to set the Ticker news according to the array position
function setArticle(intPos) 
{
	if(arrNewsItems[intPos]!=null) 
	{
		tickLocked = true;
		intCurrentPos = 0;
		strText = '';
		currentText = arrNewsItems[intPos];
		currentLink = arrNewsItems[intPos + 1];
		typeInterval = setInterval( "typeText()", intTypeSpeed);
		tickLocked = false;
  	}
}
//--end of function

//--Function to move to the next Ticker news.
function playTicker() 
{
  	isInFirstTimeout = false;
	if (autoTimerID != 0) 
	{
		clearInterval(typeInterval);
		nextArticle();
  	}
	autoTimerID = self.setTimeout("playTicker()", intTickSpeed);
}
//--end of function

//--Function to delay while news items are being populated in the array
function playTickerF() 
{
	if (arrNewsItems.length != 0) 
	{
		clearInterval(typeInterval1);
		isFirstPass = false;
		typeInterval = setInterval('',0);
		isInFirstTimeout = true;
		intTickPos=-2;
		autoTimerID = self.setTimeout("playFirstTicker()", intTickSpeed);
  	}
}
//--end of function

//--Function to play first ticker news.
function playFirstTicker() 
{
	if(isFirstPass == true) 
	{
		document.getElementById("cursor").style.display='none';
		setSpan("Loading.....","");
		typeInterval1 = setInterval( "playTickerF()", intTickSpeed);
	}
	else if(isFirstPass == false) 
	{
		clearTimeout(autoTimerID);
		isInFirstTimeout = false;
		setArticle(intTickPos);
		playTicker();
	}
}
//--end of function

//--Function to stop the ticker
function stopTicker() {
	clearTimeout(autoTimerID);
}
//--end of function

//--Function to resume the ticker
function resumeTicker() {
	clearTimeout(autoTimerID);
	autoTimerID = self.setTimeout("playTicker()", intTickSpeed);
}
//--end of function

//--Function to delay the ticker
function delayTicker() {
  clearTimeout(autoTimerID);
	clearInterval(typeInterval);
  autoTimerID = self.setTimeout("playTicker()", intTickSpeed * 2);
}
//--end of function
