//
//get charity data from rss feed and use for map points
//
function getCharityData(params) {
		this.rss = '';
		this.sitepath = '';
		this.filterA = [];
		this.filterR = [];
		this.filterC = [];
		this.filterP = [];
		this.filterAll = [];
	
		this.loadParams = function(params){
			if(params.feeds) {
				this.datafeeds = new Array;
				this.datafeeds = params.feeds;
			} else {
				alert('Error: Feed not found in initilization\nInclude feeds in initilization function');
			}
			this.sitepath = params.sitepath;
			this.google = (params.google != undefined) ? params.google : false;
			
			if (window.attachEvent) {
				window.attachEvent("onresize", function() {getOverlayPos()});
			} else { 
				window.addEventListener("resize", function() {getOverlayPos()},false);
			}
		}
		
		this.createLoading = function() {
			$('FilterRegion').innerHTML = '<img src="'+this.sitepath+'/loading.gif" />';
			$('CharityList').innerHTML='Retrieving List of Member Charities... <img src="'+this.sitepath+'/loading.gif" />';
		}
		
		this.loadData = function() {
			var divcnt = 0;
			for (var key in this.datafeeds) {
				if (this.google)
					var obj = new AjaxRequestGoogle();
				else
					var obj = new AjaxRequest();
				obj.rss = this.datafeeds[key];
				obj.count = divcnt;
				obj.init();
				divcnt++;
			}
			this.datafeedlength = divcnt;
			
			this.checkStatus();
		}
	
		this.checkStatus = function() {
			var self = this;
			var complete = true;
			for (x=0;x<this.datafeedlength;x++) {
			   if (statusobj[x] == "running") {
				   complete = false;
				   break;
			   }
			}
			if (!complete) {
				setTimeout(function() {self.checkStatus()}, 200); /* check status every 2 seconds */
			} else {
				this.filterAll = [];
				for (x=0;x<this.datafeedlength;x++) {
					this.filterAll = this.filterAll.concat(xhrs[x]);
				}
				
				this.parseRSSAll();
				
				this.initializeFilterList();
			}
		}
		this.parseRSSAll = function() {
			var XMLItem = [], XMLItemR = [], XMLItemC = [], XMLItemP = [], j=0, p=0;
			for (i=0; this.filterAll.length>i; i++)
			{
				//121
				var countrylist = [];
				var dataval = '';
				var writec = false;
				var countrylist = this.filterAll[i].description.split(']');
				//console.log(this.filterAll[i].title);				
				for (c=0;c<countrylist.length-1;c++) {
					var regionlist = this.htmlFix(countrylist[c]).split(';');
					var region = trim(regionlist[0]).substr(1,100);
					var country = trim(regionlist[1]);
					var programlist = trim(regionlist[2]);
					//console.log(country + ' ' + programlist);
					XMLItem[j] = new Array();
					XMLItem[j]["region"] = region;
					XMLItem[j]["country"] = country;
					XMLItem[j]["charity"] = trim(this.filterAll[i].title);
					XMLItem[j]["charity_link"] = this.filterAll[i].linkurl;
					XMLItem[j]["program"] = programlist;
					
					XMLItemR[j] = new Array();
					XMLItemR[j]["region"] = region;
					
					XMLItemC[j] = new Array();
					XMLItemC[j]["region"] = region;
					XMLItemC[j]["country"] = country;
					
					XMLItemArr = programlist.split(',');
					for (x=0;x<XMLItemArr.length;x++) {
						XMLItemP[p] = new Array();
						XMLItemP[p]["program"] = trim(XMLItemArr[x]);
						p++;
					}
					
					j++;
				}
			}

			//sort by charity
			XMLItem.sort(function (a, b){
						a = a['charity'];
						b = b['charity'];
						if (a>b) return 1;
						if (a <b) return -1;
						return 0; } );
			this.filterA = XMLItem;
			
			//sort by region
			XMLItemR.sort(function (a, b){
						a = a['region'];
						b = b['region'];
						if (a>b) return 1;
						if (a <b) return -1;
						return 0; } );
			this.filterR = XMLItemR;
			
			//sort by country
			XMLItemC.sort(function (a, b){
						a = a['country'];
						b = b['country'];
						if (a>b) return 1;
						if (a <b) return -1;
						return 0; } );
			this.filterC = XMLItemC;
			//sort by program
			XMLItemP.sort(function (a, b){
						a = a['program'];
						b = b['program'];
						if (a>b) return 1;
						if (a <b) return -1;
						return 0; } );
			this.filterP = XMLItemP;
		}
		
		this.htmlFix = function(html) {
				html = html.replace("&amp;", "&");
				html = html.replace("&lt;", "<");
				html = html.replace("&gt;", ">");
				html = html.replace('&quot;','"');
				html = html.replace("&apos;", "'");
				return html;
		}
		
		this.initializeFilterList = function(){
			var filterDiv = $('FilterList');
			var filterDivContent = "";
			
			this.buildRegionDDL();
			this.buildCountryDDL();
			this.buildProgramDDL();

			$('CharityList').innerHTML='';
			if (($('region').selectedIndex > 0) ||
			    ($('country').selectedIndex > 0) ||
			    ($('program').selectedIndex > 0))
				   this.getCharities();
		}
		
		
		this.getCharitiesMap = function(countrySel) {
			$('mapdetail').style.display="block";
			var filterItemContent = "<h3>Member Charities In "+countrySel+":</h3><div id='CharityMembers'>";
			var passc=false;
			var prevfilter='';
			$('CharityDetailText').innerHTML='';
		
			for (i=0;i<this.filterA.length;i++) {
				passc=false;
				if (this.filterA[i].country.indexOf(countrySel) > -1) passc=true;
				
				if (passc && prevfilter!=this.filterA[i].charity) {
					//filterItemContent += "<a href='"+filterAll[i].charity_link+"'>"+filterAll[i].charity+"</a><br />";
					filterItemContent += "<strong><a href='javascript:void(0)' onclick='localMap.createDetail2("+i+")'>"+this.filterA[i].charity+"</a></strong><br />";
					prevfilter = this.filterA[i].charity;
				}
			}
			filterItemContent += '</div><input type="hidden" id=countrySel value="'+countrySel+'" />';
			$('CharityList').innerHTML=filterItemContent;
		}
		
		this.createDetail2 = function(rec) {
				$("Charity").innerHTML = this.filterA[rec].charity;
				var filterText = '';
				filterText += "<h3>"+this.filterA[rec].charity+"</h3>"
				if ($("countrySel")) {
					filterText += "<strong>Programs in "+$("countrySel").value+":</strong><br /> "+this.createProgramHTML(this.filterA[rec].program)+"<br /><br />";
				}
				filterText += "<div id='readmoreinfo'><strong><a href='javascript:void(0)' onClick='localMap.openWindow(\""+this.filterA[rec].charity_link+"\");return false;'>View charity details</a></strong></div>";
				filterText += "</table></div>";
				$("CharityDetailText").innerHTML = filterText;
		
		}
		
		/* --------------- */
		/* for text search */
		/* --------------- */
		this.buildRegionDDL = function() {
			var filterItemContent = "<select id='region' name='region' onchange='localMap.buildCountryDDL();localMap.getCharities();'><option selected>- Select a Region -</option>";
			var prevfilter='', selected='';
			
			for (i=0;i<this.filterR.length;i++) {
				if (decodeURIComponent(getUrlVal("region")) == this.filterR[i].region)
					selected = 'selected';
				else
					selected = '';
				if (prevfilter!=this.filterR[i].region) filterItemContent += '<option value="'+this.filterR[i].region+'" '+selected+'>'+this.filterR[i].region+'</option>';
				prevfilter=this.filterR[i].region;
			}
			filterItemContent += "</select>";
			
			$('FilterRegion').innerHTML=filterItemContent;
		
		}
		
		this.buildCountryDDL = function() {
			var filterItemContent = "<select id='country' name='country' onchange='localMap.getCharities();'>";
			//if ($("region").selectedIndex == 0) filterItemContent += "<option selected>- Select a Region First -</option>";
			//	else filterItemContent += "<option selected>- Select a Country -</option>";
			filterItemContent += "<option selected>- Select a Country -</option>";
			var prevfilter='', selected='';
			
			for (i=0;i<this.filterC.length;i++) {
				if (decodeURIComponent(getUrlVal("country")) == this.filterC[i].country)
					selected = 'selected';
				else
					selected = '';
				if ($("region").selectedIndex == 0) {
					if (prevfilter!=this.filterC[i].country) filterItemContent += '<option value="'+this.filterC[i].country+'" '+selected+'>'+this.filterC[i].country+'</option>';
					prevfilter=this.filterC[i].country;
				} else {
					if ($("region").selectedIndex > 0) {
						if ($("region").value == this.filterC[i].region) {
							if (prevfilter!=this.filterC[i].country) filterItemContent += '<option value="'+this.filterC[i].country+'" '+selected+'>'+this.filterC[i].country+'</option>';
							prevfilter=this.filterC[i].country;
						}
					}
				}
			}
			filterItemContent += "</select>";
			
			$('FilterCountry').innerHTML=filterItemContent;
		
		}
		
		this.buildProgramDDL = function() {
			var filterItemContent = "<select id='program' name='program' onchange='localMap.getCharities();'><option selected>- Select a Program -</option>";
			var prevfilter='', selected='';
			
			for (i=0;i<this.filterP.length;i++) {
				if (decodeURIComponent(getUrlVal("program")) == this.filterP[i].program)
					selected = 'selected';
				else
					selected = '';
				if (prevfilter!=this.filterP[i].program) filterItemContent += '<option value="'+this.filterP[i].program+'" '+selected+'>'+this.filterP[i].program+'</option>';
				prevfilter=this.filterP[i].program;
			}
			filterItemContent += "</select>";
			
			$('FilterProgram').innerHTML=filterItemContent;
		}
		
		this.getCharities = function() {
			var regionSel = $("region");
			var countrySel = $("country");
			var programSel = $("program");
			var passr=false, passc=false, passp=false;
			var prevfilter='';
			var filterItemContent = '';
			var count = 0;
			$("CharityListDetailText").innerHTML = '';
			$('CharityListText').innerHTML='<img src="'+this.sitepath+'/loading.gif" />';
			
			if ((regionSel.selectedIndex > 0) || (countrySel.selectedIndex > 0) || (programSel.selectedIndex > 0)){
				filterItemContent += "<h3>Search Criteria:</h3>";
				if (regionSel.selectedIndex > 0)
					filterItemContent += "<div>Region = " + regionSel.value + "</div>";
				
				if (countrySel.selectedIndex > 0) 
					filterItemContent += "<div>Country = " + countrySel.value + "</div>";
				
				if (programSel.selectedIndex > 0)
					filterItemContent += "<div>Program = " + programSel.value + "</div>";

			}
			filterItemContent += "<br />";
			
			filterItemContent += "<h3>Search Results:</h3>";
			
			for (i=0;i<this.filterA.length;i++) {
				passr=false; passc=false; passp=false;
				if (regionSel.selectedIndex > 0) {
					if (this.filterA[i].region.indexOf(regionSel.value) > -1) passr=true;
				}
				else passr=true; //if nothing selected in region then set to true
				
				if (countrySel.selectedIndex > 0) {
					if (this.filterA[i].country.indexOf(countrySel.value) > -1) passc=true;
				}
				else passc=true; //if nothing selected in country then set to true
				
				if (programSel.selectedIndex > 0) {
					if (this.filterA[i].program.indexOf(programSel.value) > -1) passp=true;
				}
				else passp=true; //if nothing selected in program then set to true
				
				if (passr && passc && passp && prevfilter!=this.filterA[i].charity) {
					filterItemContent += "<a href='javascript:void(0)' onclick='localMap.createDetail3("+i+")'>"+this.filterA[i].charity+"</a><br />";
					prevfilter = this.filterA[i].charity;
					count++;
				}
			}
			$('CharityListText').innerHTML=filterItemContent;
		}
		
		this.createDetail3 = function(rec) {
				$("Charity").innerHTML = this.filterA[rec].charity;
				var filterText = '';
				filterText += "<h3>"+this.filterA[rec].charity+"</h3>"
				if ($("country").selectedIndex > 0) {
					filterText += "<strong>Programs in "+$("country").value+":</strong><br /> "+this.createProgramHTML(this.filterA[rec].program)+"<br /><br />";
				}
				filterText += "<div id='readmoreinfo'><strong><a href='javascript:void(0)' onClick='localMap.openWindow(\""+this.filterA[rec].charity_link+"\");return false;'>View charity details</a></strong></div>";
				filterText += "</table></div>";
				$("CharityListDetailText").innerHTML = filterText;
		
		}
		
		/* ------------- */
		
		/* tab functions */
		this.resetDDLs = function() {
			this.initializeFilterList();
		}
		
		this.showText = function() {
			$('mapdetail').style.display="block";
			$("charityMap").style.display="none";
			$("Search_Text").style.display="block";	
			$("CharityList").innerHTML = "";
			$("CharityDetailText").innerHTML = "";
			this.initializeFilterList();
		}
		this.showMap = function() {
			$('mapdetail').style.display="none";
			$("charityMap").style.display="block";
			$("Search_Text").style.display="none";	
			$("CharityList").innerHTML = "";
			$("CharityDetailText").innerHTML = "";
			this.initializeFilterList();
		}
		this.openWindow = function(charityurl, charity) {
				var disp_setting="toolbar=yes,location=yes,directories=yes,menubar=yes,resizable=yes,"; 
				disp_setting+="scrollbars=yes,width=820, height=600" ; 
				window.open(charityurl, "_charitypanel",disp_setting);
		}

		this.createProgramHTML = function(programs) {
			var html = '';
			if (programs != undefined) {
				var newProgram = programs.split(',');				
				for(var i=0; i<newProgram.length; i++) {
					var program = newProgram[i].replace(/^\s+|\s+$/g,"");;
					var lastIndex = newProgram.length-1;
					html += '<a href="/Where_We_Work?search=text&program='+encodeURIComponent(program)+'">';
					html += program;
					html += '</a>';
					if(lastIndex != i) {
						html += ', ';
					}
				}
			}
			return html;
			
		}
	
			
		this.init = function(params) {
			this.loadParams(params);
			this.createLoading();
			this.loadData();
		}
		
		this.init(params);
}

function getOverlayPos(){
   var x = $('mapOverlay');
   var pos = findPos(x);
   var y = $('charity_detailbg');
   y.style.top = (pos[1]) + 'px';
   y.style.left = (pos[0]+1) + 'px';
   var z = $('charity_detail');
   z.style.top = (pos[1]) + 'px';
   z.style.left = (pos[0]+10) + 'px';
   var m = $('mapdetail_nav');
   m.style.top = (pos[1]+320) + 'px';
   m.style.left = (pos[0]+10) + 'px';
}

function backToMap(){
	$('mapdetail').style.display="none";
}

google.load("feeds", "1");

