/***********************************************
* Loosely based on ....
***********************************************/

// -------------------------------------------------------------------
// RSS Display Box (Ajax invocation)- Created Dec 2nd 2006 | Last updated Dec 18th, 2006
// Author: Dynamic Drive (http://www.dynamicdrive.com)
// -------------------------------------------------------------------


var rootdomain="http://"+window.location.hostname
//specify path to feed parser - must be on the same box
var rssoutputscript=rootdomain+"/attributes/rssParser/RssParser.aspx"
//Spedify HTML to show while feed is being fetched
var loadingHTML='<img src="http://cehd.umn.edu/attributes/images/rss-loading.gif" /> Loading ...' 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// jwo

var Alpha = 0;
var Index = 0;
var StayOnTime = 8000;
var FadeTime = 80;
var ArticleGroupArray = new Array();

function FadeIn() {
	var MyDiv = document.getElementById("mydivId3");
	Alpha += 0.1;
	MyDiv.style.filter = "alpha(opacity=" + (Alpha * 100) + ")";
	MyDiv.style.opacity = Alpha;
	if (Alpha > 1) {
		setTimeout(FadeOut, StayOnTime);
	} else {
		setTimeout(FadeIn, FadeTime);
	}
}

function FadeOut() {
	var MyDiv = document.getElementById("mydivId3");
	Alpha -= 0.1;
	MyDiv.style.filter = "alpha(opacity=" + (Alpha * 100) + ")";
	MyDiv.style.opacity = Alpha;
	if (Alpha <= 0) {
		MyDiv.innerHTML = ArticleGroupArray[Index];
		Index++;
		if (Index >= ArticleGroupArray.length) {
			Index = 0;
		}
		setTimeout(FadeIn, FadeTime);
	} else {
		setTimeout(FadeOut, FadeTime);
	}
}

// end jwo
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function getRssFeed(divId, sourcefile, stylesheet, CustomErrorMessage){
var myrssdisplaybox = new rssdisplaybox(divId, sourcefile, stylesheet, CustomErrorMessage)
}

////////////No need to edit beyond here//////////////

// -------------------------------------------------------------------
// PUBLIC: rssdisplaybox(RSS_id, cachetime, divId, divClass)
// Main RSS Display Box Object function.
// -------------------------------------------------------------------

function rssdisplaybox(divId,  sourcefile, stylesheet, CustomErrorMessage){
this.loadingHTML=loadingHTML
//this.loadingHTML='<img src="loading.gif" /> Loading...' //Specify HTML to show while feed is being fetched
//this.RSS_id=RSS_id //Array key indicating which RSS feed to display
this.boxid=divId //CSS ID of DIV that will hold the RSS feed items
this.sourcefile=sourcefile //url of RSS Feed
this.stylesheet=stylesheet //XSLT stylesheet that will transform the feed
this.CustomErrorMessage=CustomErrorMessage //Custom error message to display if feed does not respond
//document.write('<div id="'+divId+'-maincontainer"></div>') //Output a master DIV to contain RSS box and pagination div, plus to anchor box's position on the page
this.start()
}


// -------------------------------------------------------------------
// PUBLIC: start()- User initiated start() function, to tell the script to initialize itself.
// -------------------------------------------------------------------

rssdisplaybox.prototype.start=function(){
//var rssboxhtml='<div id="'+this.boxid+'"></div>'
//document.getElementById(this.boxid+'-maincontainer').innerHTML=rssboxhtml
document.write('<div id="'+this.boxid+'"></div>')
this.ajaxobj=createAjaxObj()
this.getAjaxcontent()
}


// -------------------------------------------------------------------
// PRIVATE: getAjaxcontent()- Makes asynchronous GET request to "content.php" with the supplied parameters
// -------------------------------------------------------------------

rssdisplaybox.prototype.getAjaxcontent=function(){
if (this.ajaxobj){
var instanceOfBox=this
//var parameters=""
//var parameters="id="+encodeURIComponent(this.RSS_id)+"&cachetime="+this.cachetime+"&limit="+this.utotalitems+"&template="+this.template+"&bustcache="+new Date().getTime()
var parameters="rssfeed="+encodeURIComponent(this.sourcefile)+"&xslt="+rootdomain+encodeURIComponent(this.stylesheet)+"&bustcache="+new Date().getTime()
document.getElementById(this.boxid).innerHTML=this.loadingHTML
this.loadingHTML=null
this.ajaxobj.onreadystatechange=function(){instanceOfBox.initialize()}
this.ajaxobj.open('GET', rssoutputscript+"?"+parameters, true)
this.ajaxobj.send(null)
}
}

// -------------------------------------------------------------------
// PRIVATE: initialize()- Initialize RSS Display Box method.
// -------------------------------------------------------------------

rssdisplaybox.prototype.initialize=function(){ 
if (this.ajaxobj.readyState == 4){ //if request of file completed
	if (this.ajaxobj.status==200){ //if request was successful
		var rsscontent=this.ajaxobj.responseText
		
////////////////  jwo edits  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// get all article|html into string
	var ArticleCount = rsscontent.match(/(<div)/g).length;
	var ArticlePosStart = 0;
	var ArticlePosEnd = rsscontent.indexOf("</div>");
	ArticlePosEnd += 6;

	var ArticleArray = new Array();
//	var ArticleGroupArray = new Array();
	var ArticleGroupCount = Math.floor((ArticleCount / 2) + (ArticleCount % 2));
//	add first article to array
	ArticleArray[0] = rsscontent.slice(ArticlePosStart, ArticlePosEnd);

//	add remainder of articles
	for (i = 1; i < ArticleCount; i++) {
		ArticlePosStart = ArticlePosEnd + 1;
		ArticlePosEnd = rsscontent.indexOf("/div>", ArticlePosStart);
		(i > 1) ? ArticleArray[i] = rsscontent.slice(ArticlePosStart + 4, ArticlePosEnd + 5) : ArticleArray[i] = rsscontent.slice(ArticlePosStart, ArticlePosEnd + 5);
		// hack for flaw in above logic...why is it adding '<div' ?? -- fix later.  big problem.  :/
	}

	var RemainingArticles = ArticleCount;
	var ArticleCounter = 0;
	var GroupElement = 0;

	while (RemainingArticles > 0) {
		ArticleGroupArray[GroupElement] = ArticleArray[ArticleCounter];
		RemainingArticles--;
		ArticleCounter++;
		if (RemainingArticles >= 1) {
			ArticleGroupArray[GroupElement] += ArticleArray[ArticleCounter];
			RemainingArticles--;
			ArticleCounter++;
		}
		GroupElement++;
	}

	//window.onload = function() { setTimeout(FadeIn, 80); } - this was causing the extra lag on CI site
	window.onload = function() { setTimeout(FadeOut, 80); }

//	document.getElementById('mydivId3').style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";
//	document.getElementById("mydivId3").style.filter = "alpha(opacity=10)";

/////////////////////////////////             uncomment following line for revert     ///////////////////////////////////////////////////////////////////////////////////

//		document.getElementById(this.boxid).innerHTML=rsscontent
	}
	else //if an error has occured
		document.getElementById(this.boxid).innerHTML=this.CustomErrorMessage
		//document.getElementById(this.boxid).innerHTML=this.ajaxobj.responseText
	}
}


////////// END RSSDISPLAYBOX() FUNCTION ////////////////////

//Create Ajax instance function

function createAjaxObj(){
var httprequest=false
if (window.XMLHttpRequest){ // if Mozilla, IE7, Safari etc
httprequest=new XMLHttpRequest()
}
else if (window.ActiveXObject){ // if IE6 or below
try {
httprequest=new ActiveXObject("Msxml2.XMLHTTP");
} 
catch (e){
try{
httprequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
return httprequest
}

