var ticker;

window.onload = function()
{
	if (document.body.id != 'home')
	{
		return(false);
	}
	
	var SourcePath = '/rss/covernews.xml';
	var ImageSpeed = 1.5;
	var TickerSpeed = 7;
	var TextLimit = 285;
	
	document.getElementById('RSSViewer').style.position = 'static';
	
	ticker = new RSSTicker(SourcePath, ImageSpeed, TickerSpeed, TextLimit);
};

//Constructor
function RSSTicker(SourcePath, ImageSpeed, TickerSpeed, TextLimit)
{
	//Private
	var AJAXTransport = null;
	var Me = this;
	var ItemCount = 0;
	var PauseBTN = document.getElementById('RSSPause');
	var PlayBTN = document.getElementById('RSSPlay');

	//Privileged
	this.placeHolder = '/images/flashnews_placeholder.gif';
	this.selectedIMG = '/images/flashnews_rsscontrols_active.gif';
	this.inactiveIMG = '/images/flashnews_rsscontrols_inactive.gif';
	this.currentIndex = -1;
	this.itemList = null;
	this.timerID = 0;
	this.imageSpeed = ImageSpeed;
	this.tickerSpeed = (TickerSpeed * 1000);
	this.textLimit = TextLimit;
	this.handleBlender = document.getElementById('RSSBlender');
	this.handleImage = document.getElementById('RSSImage');
	this.handleLink = document.getElementById('RSSLink');
	this.handleLink2 = document.getElementById('RSSLink2');
	this.handleText = document.getElementById('RSSText');

	if (window.XMLHttpRequest)
	{
		AJAXTransport = new XMLHttpRequest();
	}
	else
	{
		try 
		{
			AJAXTransport = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e)
		{ 
			try 
			{
				AJAXTransport = new ActiveXObject('Microsoft.XMLHTTP'); 
			}	
			catch (e)
			{
				//null handler;
			}
		}
	}
	
	with (AJAXTransport)
	{
		open('GET', SourcePath, true);

		onreadystatechange = function()
		{
			if (readyState == 4)
			{
				if (status == 200)
				{
					Me.initializeViewer(responseXML.documentElement);
				}
			}
		}
		
		send(null);
	}
	
	this.initializeViewer = function(RSSContent)
	{
		var SelectorHTML = '';
		
		var SelectionBar = document.getElementById('RSSSelection');
		var PreviousBTN = document.getElementById('RSSPrevious');
		var NextBTN = document.getElementById('RSSNext');
		
		this.itemList = RSSContent.getElementsByTagName('item');
		
		ItemCount = this.itemList.length;
	
		for (LoopCounter = 0; LoopCounter < ItemCount; LoopCounter++)
		{
			SelectorHTML += "<img id='selector" + LoopCounter + "' src='" + this.inactiveIMG + "' onclick='ticker.stopTicker();ticker.displayItem(" + LoopCounter + ")' />";
		}
		
		if (document.images)
		{
			var ImageList = RSSContent.getElementsByTagName('enclosure');
		
			if (ImageList.length > 0)
			{
				var Preloader = new Image();
				
				for (LoopCounter = 0; LoopCounter < ImageList.length; LoopCounter++)
				{
					Preloader.src = ImageList[LoopCounter].getAttribute('url');
				}
			}
		}

		SelectionBar.innerHTML = SelectorHTML;
		
		PreviousBTN.onclick = function()
		{
			Me.stopTicker();
			
			Me.setIndex(false);
		};
		
		PauseBTN.onclick = function()
		{
			Me.stopTicker();
		};
		
		PlayBTN.onclick = function()
		{
			Me.runTicker();
			
			this.style.display = 'none';
			
			PauseBTN.style.display = 'inline';
		};
		
		NextBTN.onclick = function()
		{
			Me.stopTicker();
			
			Me.setIndex(true);
		};

		this.runTicker();
	};
	
	this.runTicker = function()
	{
		this.setIndex(true);
		
		this.timerID = setInterval(function(){Me.setIndex(true);}, this.tickerSpeed);
	};
	
	this.stopTicker = function()
	{
		clearInterval(this.timerID);
			
		PauseBTN.style.display = 'none';
		
		PlayBTN.style.display = 'inline';
	};
	
	this.setIndex = function(Increment)
	{
		var NextIndex = 0;
		
		if (Increment)
		{
			if (this.currentIndex == (ItemCount - 1))
			{
				NextIndex = 0;	
			}
			else
			{
				NextIndex = (this.currentIndex + 1);
			}
		}
		else
		{
			if (this.currentIndex == 0)
			{
				NextIndex = (ItemCount - 1);	
			}
			else
			{
				NextIndex = (this.currentIndex - 1);
			}
		}
		
		this.displayItem(NextIndex);
	};
	
	if(navigator.userAgent.indexOf('Firefox') != -1)
	{
		this.rotateImage = function(ImageSource)
		{
			this.handleImage.style.backgroundImage = 'url(' + this.handleBlender.src + ')';
			
			with (this.handleBlender)
			{
				src = ImageSource;
			
				style.display = 'inline';
			}
		};
	}
	else
	{
		this.rotateImage = function(ImageSource)
		{
			var Timer = 0;
			
			this.handleImage.style.backgroundImage = 'url(' + this.handleBlender.src + ')'; 
			
			this.setAlpha(0);
			
			with (this.handleBlender)
			{
				src = ImageSource;
			
				style.display = 'inline';
			}
	
			for(LoopCounter = 0; LoopCounter < 100; LoopCounter++)
			{
				setTimeout("ticker.setAlpha(" + LoopCounter + ")", (Timer * this.imageSpeed));
				
				Timer++; 
			}
		};
	}
	
	this.setAlpha = function(Opacity)
	{
		var OpacityFactor = (Opacity / 100);
		
		with (this.handleBlender.style)
		{
			opacity = OpacityFactor;								//FireFox
			KhtmlOpacity = OpacityFactor;							//Safari
			filter = "alpha(opacity=" + Opacity + ")";				//IE
		}
	};
}

RSSTicker.prototype.imageSpeed = 0;
RSSTicker.prototype.tickerSpeed = 0;
RSSTicker.prototype.textLimit = 0;

RSSTicker.prototype.displayItem = function(ItemIndex)
{
	var CurrentItem = this.itemList[ItemIndex];
	var CurrentImage;
	var CurrentTitle;
	var CurrentText;
	var CurrentSelect = document.getElementById(('selector' + ItemIndex));
	var PreviousSelect = document.getElementById(('selector' + this.currentIndex));
	var ImageSource = '';
	var RSSTitle = '';
	var RSSText = '';
	
	with (CurrentItem)
	{
		CurrentImage = getElementsByTagName('enclosure');
		CurrentTitle = getElementsByTagName('title')[0];
		CurrentText = getElementsByTagName('description')[0];
	}
	
	CurrentSelect.src = this.selectedIMG;
	
	if ((PreviousSelect) && (this.currentIndex != ItemIndex))
	{
		PreviousSelect.src = this.inactiveIMG;
	}
	
	if (CurrentImage.length > 0)
	{
		ImageSource = CurrentImage[0].getAttribute('url');
	}
	else
	{
		ImageSource = this.placeHolder;
	}
	
	if (CurrentTitle.firstChild)
	{
		RSSTitle = "<h3>" + CurrentTitle.firstChild.nodeValue + "</h3>";
	}
	
	if (CurrentText.firstChild)
	{
		RSSText = CurrentText.firstChild.nodeValue;
		
		if (RSSText.length > this.textLimit)
		{
			var Pattern = new RegExp('.{1,' + this.textLimit + '}\\b', 'm');
			var Matches = RSSText.match(Pattern);
		
			if (Matches)
			{
				RSSText = Matches[0] + "&hellip; [<span class='RSSMore'>more</span>]";
			}
		}
		
	}
	
	this.handleText.innerHTML = RSSTitle + RSSText;
	
	this.handleLink.href = CurrentItem.getElementsByTagName('link')[0].firstChild.nodeValue;

	this.handleLink2.href = CurrentItem.getElementsByTagName('link')[0].firstChild.nodeValue;
	
	this.currentIndex = ItemIndex;
	
	this.rotateImage(ImageSource);
}


function toggleDivOL( elemID )
{
	var elem = document.getElementById( elemID );	
	if( elem.style.position != 'relative' )
	{
		elem.style.position = 'relative';
		elem.style.left = '0px';
	}
	else
	{
		elem.style.position = 'absolute';
		elem.style.left = '-4000px';
	}
}