// -------------------------------------------------------------------
// gAjax RSS Pausing Scroller- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Created: Aug 2nd, 2007 Updated: n/a
// REQUIRES: gfeedfetcher.js class, available at http://dynamicdrive.com/dynamicindex18/gajaxrssdisplayer.htm
// -------------------------------------------------------------------

var gfeedfetcher_loading_image="http://www.clockworxclients.co.uk/playfootball/graphics/indicator.gif" //Specify full URL to "loading" image. Overwrites same var from gfeedfetcher.js


function gfeedRandom(divid, divClass, linktarget){
	this.tickerid=divid //ID of outermost scroller div
	this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
	this.hiddendivpointer=1 //index of RSS feed array's entry for hidden div
	this.itemsperpage=2 //Entries to show per page
	document.write('<div id="'+divid+'" class="'+divClass+'">')
	document.write('</div>')
	this.feedcontainer=document.getElementById(divid)
	//ds - uncommented following 6 lines as a test
	//this.tickerdiv=document.getElementById(divid)
	//this.visiblediv=document.getElementById(divid+"1")
	//this.hiddendiv=document.getElementById(divid+"2")
	//this.visibledivtop=parseInt(this.tickerdiv.currentStyle? this.tickerdiv.currentStyle["paddingTop"] : window.getComputedStyle? window.getComputedStyle(this.tickerdiv, "").getPropertyValue("padding-top") : 0) //Determine the "top" boundary of the visible div, factoring in any CSS padding
	//this.tickerdivwidth=this.tickerdiv.currentStyle? parseInt(this.tickerdiv.currentStyle["width"]) : this.tickerdiv.offsetWidth //IE has trouble getting offsetWidth while page is loading, so use global CSS value instead
	//this.tickerdivheight=this.tickerdiv.currentStyle? parseInt(this.tickerdiv.currentStyle["height"])+50 : 0 //IE has trouble getting offsetWidth while page is loading, so use global CSS value instead. "50" accounts for any possible padding skewing things. Variable used by any other browser.
}

gfeedRandom.prototype=new gfeedfetcher //inherit methods from gfeedfetcher class
gfeedRandom.prototype.constructor=gfeedRandom
gfeedRandom.prototype._displayresult=null //Remove inherited method "_displayresult()"


// -------------------------------------------------------------------
// entries_per_page()- Sets the number of RSS entries to display per page (at once)
// -------------------------------------------------------------------

gfeedRandom.prototype.entries_per_page=function(num){
	this.itemsperpage=num
}

// -------------------------------------------------------------------
// _signaldownloadcomplete()- Signals to the rest of the script when the fetching of all RSS feeds is complete
// -------------------------------------------------------------------

gfeedRandom.prototype._signaldownloadcomplete=function(){ //overwrite inherited method "_signaldownloadcomplete()"
	this.feedsfetched+=1
	if (this.feedsfetched==this.feedurls.length) //if all feeds fetched
		this._populateticker(this.feeds) //Populate the two DIVs within scroller with the fetched data
}

// -------------------------------------------------------------------
// formatrssmessage()- Global function that formats a RSS entry(s) to the desired components (title, date, description etc)
// -------------------------------------------------------------------

function formatrssmessage(feedslice, showoptions, itemcontainer, linktarget){
	var rssoutput=(itemcontainer=="<li>")? "<ul>" : "" //if "itemcontainer" is set to "<li>", define a "<ul>" tag to wrap around the result
	for (var i=0; i<feedslice.length; i++){ //Loop through the entered slice of a RSS feed (1 or more entries)
		var itemtitle="<a href=\"" + feedslice[i].link + "\" target=\"" + linktarget + "\" class=\"bluelink\"><b>" + feedslice[i].title + "</b></a>"
		//var itemlabel=/label/i.test(showoptions)? '<span class="labelfield">['+feedslice[i].ddlabel+']</span>' : " "
		//var itemdate=gfeedfetcher._formatdate(feedslice[i].publishedDate, showoptions)
		var itemdescription=/description/i.test(showoptions)? ""+feedslice[i].content : /snippet/i.test(showoptions)? ""+(feedslice[i].contentSnippet.length > 50 ? feedslice[i].contentSnippet.substr(0,feedslice[i].contentSnippet.lastIndexOf(' ',40))+'...':feedslice[i].contentSnippet)  : ""
		rssoutput+=itemcontainer + itemtitle + "" + itemdescription + itemcontainer.replace("<", "</") + ""
		//rssoutput+=itemcontainer + itemtitle + " " + itemlabel + " " + itemdate + "\n" + itemdescription + itemcontainer.replace("<", "</") + "\n\n"
	}
	rssoutput+=(itemcontainer=="<li>")? "</ul>" : ""
	return rssoutput
}


// -------------------------------------------------------------------
// _populateticker()- Pre-populates the two DIVs of the scroller with the 1st and 2nd RSS entries.
// -------------------------------------------------------------------

gfeedRandom.prototype._populateticker=function(feeds){
	var rands=new Array();
	for(var i=0;i<this.itemsperpage;i++)
	{
		var genran = Math.floor(Math.random()*20);
		//check if its already been used
		var found=false;
		for(var j=0; j<this.itemsperpage;j++)
		{
			if(genran==rands[j])
			{
				found=true;
			}
		}
		if(found==false)
		{
			rands[i] = genran;
		}else
		{
			//do the iteration again
			i--;
		}
	}
	
	gfeedfetcher._sortarray(feeds, this.sortstring)
	//this.itemsperpage=6;
	var feedslice1=new Array(this.itemsperpage);
	for(var i=0;i<this.itemsperpage;i++)
	{
		feedslice1[i]=feeds[rands[i]];
	}		
	this.feedcontainer.innerHTML=formatrssmessage(feedslice1, this.showoptions, this.itemcontainer, "_New")
}