
/* get parameters and write main div */
function setupInfoServ(params) {

	this.init = function(params) {
			this.params = params;
			this.params.google = params.google;
			this.sitepath = params.sitepath;
			this.loadingImage = params.loadingImage;
			this.rssfeed = params.feeds.infoserv;
			this.contentArea = params.contentArea;
			this.divcnt = 0;
			this.displayLoading();
			this.buildList();
		}

	this.buildList = function() {
			if (this.params.google)
				var obj = new AjaxRequestGoogle();
			else
				var obj = new AjaxRequest();
			statusobj[this.divcnt] = "running";
			obj.rss = this.rssfeed;
			obj.count = this.divcnt;
			obj.init();
		
			this.checkStatus();
		}

	this.checkStatus = function() {
			var self = this;
			if (statusobj[this.divcnt] == "running") {
				setTimeout(function(){self.checkStatus()}, 200); /* check status every 2 seconds */
			} else {
				infoData = new buildInfoServ();
				infoData.divcnt = this.divcnt;
				infoData.contentArea = this.contentArea;
				infoData.init();
			}
		}
	
	this.displayLoading = function() {
			$(this.contentArea).innerHTML = "<strong>Loading...</strong>&nbsp;&nbsp;<img src='" + this.sitepath + this.loadingImage + "' />";
		}
	
	this.init(params);
}

/* build category, subcategory and calendar tables */
function buildInfoServ() {
	this.divcnt = 0;
	this.contentArea = '';
	this.infoData = [];
	this.titleprefix = '';
	
	this.init = function() {
			this.infoData = this.infoData.concat(xhrs[this.divcnt]);
			this.getArticles();
		}
	
	//process article data in array and store by category
	this.getArticles = function() {
		var XMLItem = this.infoData;
		XMLItem.sort(function (a,b) {
						var x = (a.type == "undefined") ? "" : a.type;
						var y = (b.type == "undefined") ? "" : b.type;
						return ((x < y) ? -1 : ((x > y) ? 1 : 0));
					});
		
		var oldType = "";
		k = -1;
		m = 0;
		var articles = new Array();
		for (i=0; XMLItem.length>i; i++)
		{
			
			XMLItem[i].type = (XMLItem[i].type == null) ? "" : XMLItem[i].type;
			XMLItem[i].title = (XMLItem[i].title == null) ? "" : XMLItem[i].title;
			XMLItem[i].question = (XMLItem[i].question == null) ? "" : XMLItem[i].question;
			XMLItem[i].date = (XMLItem[i].date == null) ? "" : XMLItem[i].date;
			XMLItem[i].id = (XMLItem[i].id == null) ? "" : XMLItem[i].id;
			XMLItem[i].sponsor_link = (XMLItem[i].sponsor_link == null) ? "" : XMLItem[i].sponsor_link;
			XMLItem[i].linkurl = (XMLItem[i].linkurl == null) ? "" : XMLItem[i].linkurl;
			XMLItem[i].answer = (XMLItem[i].answer == null) ? "" : XMLItem[i].answer;
			XMLItem[i].summary = (XMLItem[i].summary == null) ? "" : XMLItem[i].summary;
	
			
			if(XMLItem[i].type_sub == null) {
				XMLItem[i].type_sub = "1";
			}
			
			if (oldType != XMLItem[i].type ) {
				k++;
				oldType = XMLItem[i].type;
				articles[k] = new Array();
				m=0;
			}
			articles[k].type = XMLItem[i].type;
			articles[k][m] = XMLItem[i];
		
			m++;
		}
		var dateRE = /^(\d{2})[\/\- ](\d{2})[\/\- ](\d{4})/;
		for (i=0; articles.length>i; i++)
		{
			//articles[i].sort(function (a, b){
			//		a =  a['date'];
			//		b =  b['date'];
			//		if (a>b) return -1;
			//		if (a <b) return 1;
			//		return 0; }
			///	);
			
			articles[i].sort(function (a,b) {
						var x = a.type_sub+a.title;
						var y = b.type_sub+b.title;
						return ((x < y) ? -1 : ((x > y) ? 1 : 0));
					});
		}
	
		this.articles = articles;
		$(this.contentArea).appendChild(this.createResources());
		// Remove top wrapper part on confirmation
		if((window.location.toString().indexOf('ContactSent.asp') > -1) && (document.getElementById('AskTopMainArea') != null) ) {
			document.getElementById('AskTopMainArea').style.display = 'none';
		}
	}
		
	//create Question list
	this.createResources = function() {
		var resourceBlocks = document.createElement("div");
		var maxArticles = 2;
		var self = this;

		for (i=0; this.articles.length>i; i++)
		{
			var h3 = document.createElement("h3");
			h3.innerHTML = this.articles[i].type;
			resourceBlocks.appendChild(h3)
			var ul = document.createElement("ul");
			
			for (j=0; this.articles[i].length>j; j++) {
				if (maxArticles<=j) { break; }
				var li = document.createElement("li");
					var href = document.createElement("a");
					href.href = "javascript:void(0)";
					href.innerHTML = this.articles[i][j].question;
					href.onclick = function() {
						turnme(this.parentNode);
					}
				li.appendChild(href);
					var pp = document.createElement("p");
					pp.innerHTML = this.articles[i][j].answer;
						var br = document.createElement("br");
					pp.appendChild(br);
						var href = document.createElement("a");
						href.href = "#";
						href.cat = i;
						href.subcat = j;
						href.innerHTML = "Read More";
						href.onclick = function() {
							self.displayQA(this.cat,this.subcat);
						}
					pp.appendChild(href);
				li.appendChild(pp);
			ul.appendChild(li);
			}
			resourceBlocks.appendChild(ul);
				var href = document.createElement("a");
				href.innerHTML = "Find more "+this.articles[i].type+" Questions";
				href.href = "#";
				href.cat = i;
				href.onclick = function() {
					self.viewMore(this.cat);
				}
			resourceBlocks.appendChild(href);
		}
		//clear current contents
		$(this.contentArea).innerHTML = '';
		
		return resourceBlocks;
	}
	
	//show more data within "howicanlearn" div
	this.viewMore = function(id) {
		var self = this;
		var tC = ""; // temp category
		
		var resourceBlocks = document.createElement("div");
			var h2 = document.createElement("h2");
			h2.innerHTML = this.articles[id].type;
		resourceBlocks.appendChild(h2);
			var div = document.createElement("div");
			div.id = "AskInfoCategoryList"
		
			for (j=0; this.articles[id].length>j; j++) {
				if(tC != this.articles[id][j].type_sub) {
					if(this.articles[id][j].type_sub != '1') {
						var h2 = document.createElement("h2");
						h2.innerHTML = this.articles[id][j].type_sub;
						div.appendChild(h2);
					}
					var ul = document.createElement("ul");
					tC = this.articles[id][j].type_sub;
				}
				var li =  document.createElement("li");
					var href =  document.createElement("a");
					href.href = "#";
					href.cat = id;
					href.subcat = j;
					href.innerHTML = this.articles[id][j].question;
					href.onclick = function(){
						self.displayQA(this.cat, this.subcat);
					}
				li.appendChild(href);
				ul.appendChild(li);

				if(this.articles[id][j+1] != null) {
					if(tC != this.articles[id][j+1].type_sub) {
						div.appendChild(ul);
					}
				}
			}
			div.appendChild(ul);
		resourceBlocks.appendChild(div);
			var span = document.createElement("span");
			span.innerHTML = '<a class="BackInfoServ" href="http://www.kintera.org/site/c.lwL4KnN1LtH/b.2068577/k.9ACD/Ask_InfoServ/apps/ka/ct/contactcustom.asp">Back to Ask InfoServ Home.</a>';
		resourceBlocks.appendChild(span);
		document.getElementById('howcanilearn').innerHTML = ''; //clear current contents
		document.getElementById('howcanilearn').appendChild(resourceBlocks);
	}
	
	//display Question and Answer in the "howicanlearn" div
	this.displayQA = function( i, j ) {
		var self = this;
		var resourceBlocks = document.createElement("div");
		resourceBlocks.id = "QA";
			var h2 = document.createElement("h2");
			h2.innerHTML = this.articles[i].type
		resourceBlocks.appendChild(h2);
			var pp = document.createElement("p");
				var span = document.createElement("span");
				span.innerHTML = this.articles[i][j].summary;
			pp.appendChild(span);
				var href = document.createElement("a");
				href.className = "BackInfoServ";
				href.href = "javascript:void(0);"
				href.innerHTML = "Back to "+this.articles[i].type+" questions."
				href.cat = i;
				href.onclick = function() {
					self.viewMore(this.cat);
				}
			pp.appendChild(href);
				var text = document.createElement("span");
				text.innerHTML = '<br /><a class="BackInfoServ" href="http://www.kintera.org/site/c.lwL4KnN1LtH/b.2068577/k.9ACD/Ask_InfoServ/apps/ka/ct/contactcustom.asp">Back to Ask InfoServ Home.</a></div';
			pp.appendChild(text);
		resourceBlocks.appendChild(pp);
		document.getElementById('howcanilearn').innerHTML = ''; //clear current content
		document.getElementById('howcanilearn').appendChild(resourceBlocks);		
	}
}
//addLoadListener(makeRequestResources);
google.load("feeds", "1"); /* initialize google feed */