/*

FamilyLife Article Listing - Source code
Version: beta

This contains the source code for the Broadcast Listing and Search. 
Pulling from Kintera Sphere RSS feeds
*/

/* get parameters and write main div */
function setupGlossary(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.glossary;
			this.contentArea = params.contentArea;
			this.glossaryPage = $(params.glossaryPage);
			this.divcnt = 0;
			this.displayLoading();
			this.buildGlossaryList();
		}

	this.buildGlossaryList = 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 {
				glossaryData = new buildGlossary();
				glossaryData.divcnt = this.divcnt;
				glossaryData.contentArea = this.contentArea;
				this.removeLoading();
			}
		}
	
	this.removeLoading = function() {
			changeOpac(100,this.glossaryPage);
			$(this.contentArea).innerHTML = "";			
		}

	this.displayLoading = function() {
			changeOpac(40,this.glossaryPage);
			$(this.contentArea).innerHTML = "<strong>Loading Glossary...</strong>&nbsp;&nbsp;<img src='" + this.sitepath + "/" + this.loadingImage + "' />";
		}
	
	this.init(params);
}
/* build category, subcategory and calendar tables */
function buildGlossary() {
	this.divcnt = 0;
	this.contentArea = '';
	this.glossaryData = [];
	this.titleprefix = '';
	
	this.init = function() {
			this.glossaryData = this.glossaryData.concat(xhrs[this.divcnt]);
			this.buildAutoComplete();
		}
	
	this.buildAutoComplete = function() {
		//sort title
		this.glossaryData.sort(function (a, b){
				a = a['title'];
				b = b['title'];
				if (a>b) return 1;
				if (a <b) return -1;
				return 0; } );
		
		var glossaryTerms = [];
		for (x=0;x<this.glossaryData.length;x++) {
			glossaryTerms.push(this.glossaryData[x].title);
		}
		new Autocompleter.Local('sbKeywords', 'glossaryTerms', glossaryTerms, {fullSearch: false, partialSearch: false, partialChars: 0});							
	}
	
	/* filter data based on left menu and keyword search */
	this.filterGlossary = function(item) {
			var filter = this.glossaryData;
			var pagecnt = 1;
			var newfilter = [], j=0;
			/* setup object names */
			var objectk = 'sbKeywords';
			var passkeyword = '';
			/* get values */
			if (item == undefined) {
				passkeyword = ($(objectk).value != '') ? $(objectk).value : "";	
				this.titleprefix = '';
			} else {
				passkeyword = '';
				this.titleprefix = item.innerHTML;
			}

			for (i=0;i<filter.length;i++) {
				/* remove previous highlight */
				
				cont=true;
				if (this.titleprefix != '') cont = (this.titleprefix != filter[i].title.substr(0,1)) ? 0 : 1;
				//if (passkeyword != '') cont = (passkeyword != filter[i].title.substr(0,1)) ? 0 : 1;				
				if (passkeyword != "" &&
					(filter[i].title.toLowerCase().indexOf(passkeyword.toLowerCase()) == -1)) cont=false;
				/* loop through all catagories */
				if (cont) {
					newfilter[j] = [];
					newfilter[j].title = filter[i].title;
					newfilter[j].description = filter[i].description;
					newfilter[j].date = filter[i].date;
					newfilter[j].pagecount = pagecnt;
					//if ((j+1)/pagecnt==itemsPerPage) pagecnt++; /* add 1 to page count every so many records */
		
					/* add highlighting */
					if (passkeyword != "") {
						newfilter[j].title = doHighlight(newfilter[j].title, passkeyword,'','');
					}
					j++;
				}
			}
			this.FilterData = newfilter;

			this.displayFilterData();
			this.titleprefix = ''; /* clear title prefix - if passed */
			this.urlcategory = ''; /* clear url category - if passed */	
		}

	/* name of highlevel filter array passed, the actual name, div the data will return to and the pagecount to display */
	this.displayFilterData = function() {
			var filter = [];
			var filterItemContent = '';
			filter = this.FilterData.slice(0);
			for (i=0;i<filter.length;i++) 
				filterItemContent += this.buildContent(filter[i]);
			if (filter.length == 0) filterItemContent = "<strong>No Glossary Terms found starting with " + this.titleprefix + ".</strong>";
			$(this.contentArea).innerHTML = this.writeOutput(filterItemContent);
		}
	
	/* builds content to be written */
	this.buildContent = function(filter) {
			var content = "";
			content += "<div id='umc-glossary'>"
			content += "<div class='umc-glossary-title'><h3>"+filter.title+"</h3></div>";
			content += "<p><div class='umc-glossary-summary'>"+filter.description+"</div></p>";
			content += "</div>";
			return content;
		}

	/* write's output data */
	this.writeOutput = function(filteritem) {
			return "<div id='glossaryResults'><p>"+filteritem+"</p></div>";
		}
	
	this.init();
}


google.load("feeds", "1"); /* initialize google feed */