$(document).ready(function(){
	$('.gtwmtextscroll').each(function(){
		fTextScrollInitialise(this);
		
	});
});

function fTextScrollInitialise(oHTMLObj) {
	
	function fLoadError(oXMLHttpRequest, sTextStatus, oErrorThrown) {
		alert('there was a problem loading the text\n'+sTextStatus);
	}
	
	function fCreateItems(oXML) {
		function fXMLtoArray(oXML,sItems) {
			// load all the XML into an array
			var aText=new Array();
			var sText=''; // a test string
			
			// for all the items
			$(oXML).find(sItems).each(function() {
				var aItem=new Array();
				
				// push on an array of all the children (assuming they're a text node)
				$(this).children().each(function() {
					aItem[this.nodeName]=$(this).text();
					sText+=(this.nodeName+'='+$(this).text()+'\n');
				});
				aText.push(aItem);
				sText+='\n\n';
			});		
			
			return aText;	
		}
				
		var aItems=fXMLtoArray(oXML,'item');
		var aHTMLItems=new Array(); // array to store the created HTML items
		
		$(aItems).each(function(){
			var oItem=document.createElement('DIV');
			var oCaption=document.createElement('DIV');
			oCaption.appendChild(document.createTextNode(this['caption']));
			var oText=document.createElement('DIV')
			oText.appendChild(document.createTextNode(this['text']));
			
			$(oItem).addClass('item');
			$(oCaption).addClass('caption');
			$(oText).addClass('text');
			
			oItem.appendChild(oCaption);
			oItem.appendChild(oText);
			
			aHTMLItems.push(oItem);
		});
		
		$(oHTMLObj).toggleClass('loading');
		
		fShowItems(aHTMLItems);
	}
	
	function fShowItems(aHTMLItems) {		
		function fRotateItems() {
			
		  // if we're still hiding something, wait until that hide is complete
		  if( (oHTMLObj.getAttribute('hiding')) || (oHTMLObj.getAttribute('hiding')==false) ) {
		  	setTimeout(fRotateItems,5);
		  	return;
		  }
			
			
			// stop the page resizing if the new content is bigger by hiding any overflowing content
			$(oHTMLObj).css('overflow','hidden'); 
			$(oHTMLObj).css('height',$(oHTMLObj).height());
			
		  var oFirstChild=$(oHTMLObj).children('.item:first');
		  
		  oHTMLObj.setAttribute('hiding',true);
		  $(oFirstChild).hide('slow',function(){
		  	oHTMLObj.removeAttribute('hiding');
		  	if (jQuery.browser.msie) $(this).get(0).style.removeAttribute('filter');
				$(this).remove();
				
				// something to watch but prolly ok in most cases
				// $(oHTMLObj).css('overflow','visible'); //  this will cause the page to jump in IE, other browsers will render the content outside the div
																							   //  this is quite neat but could cause some overlap problems
				
				$(oHTMLObj).css('height','auto'); // this may cause the page to jump if the new height exceeds the old
				if (jQuery.browser.msie) {// it's IE 
					$(oHTMLObj).css('height',$(oHTMLObj).height()); // set the height in IE
				} else { 
				  $(oHTMLObj).css('min-height',$(oHTMLObj).height()); // this will mean that the div sizes to the biggest content after one pass of the items
				}
				
			});
					  
			if(iNextItem>=aHTMLItems.length) iNextItem=0;			
			
			oHTMLObj.appendChild(aHTMLItems[iNextItem]);
			$(aHTMLItems[iNextItem]).show();
			iNextItem++;
		  setTimeout(fRotateItems,iSpeed);	
		}
		
		function fInit() {
			for(var i=0; i<iLimit; i++) oHTMLObj.appendChild(aHTMLItems[i]);
		  iNextItem=iLimit;
		  setTimeout(fRotateItems,iSpeed);	
		}
		
		var iLimit=oHTMLObj.getAttribute('limit')?oHTMLObj.getAttribute('limit'):aHTMLItems.length;
		if(isNaN(iLimit)||(iLimit>aHTMLItems.length))iLimit=aHTMLItems.length;
		
		var iNextItem=0;
		var iSpeed=oHTMLObj.getAttribute('speed')||'2000';
		fInit();
			
	}
		
	if(oHTMLObj.getAttribute('src')) {
		with($(oHTMLObj)) {
			empty();
			toggleClass('loading');
		}
	  var oReq=$.ajax({url: oHTMLObj.getAttribute('src'), cache: false, success: fCreateItems, dataType: 'xml', timeout:5000, error:fLoadError});
	} else {
		var aItems=$(oHTMLObj).children();
		$(oHTMLObj).empty();
		fShowItems(aItems);
	}
}