// AJAX Fetches

function fetchdata(from,filter,start,count,clear,limit){
	if(from == null || !from){from = "africa"};
	if(filter == null || !filter){filter = "normal"};
	if(start == null || !start){start = 0};
	if(count == null || !count){count = 4};
	if(clear == null || !clear){clear = true};
  xmlHttp = openAJAX();
  var url = "app/fetchdata.php?from="+from+"&filter="+filter+"&start="+start+"&count="+count;
  xmlHttp.open("GET", url, false);
  xmlHttp.onreadystatechange = function() {
	var loadingbox = document.getElementById("loadingbox");
	loadingbox.style.display = "block";
	loadingbox.innerHTML = "";
	document.title = "myafricanvoice | Loading...";
		switch(xmlHttp.readyState) {
			case 0:
			loadingbox.innerHTML = "Connecting to server...";
			case 1:
			loadingbox.innerHTML = "Connection made, now processing...";
			case 2:
			loadingbox.innerHTML = "Loading...";
			break;
		}
			if (xmlHttp.readyState == 4) {
			loadingbox.innerHTML = "";
			loadingbox.style.display = "none";
			voiceObj = XMLString(xmlHttp.responseText);
			processVoices(voiceObj,clear,0,limit,true);
			document.getElementById("voicelocation").innerHTML = from.capWords();
			document.title = "myafricanvoice | Voices of "+from.capWords();
		}	
	}
  xmlHttp.send(null);
}

function processVoices(obj,clear,start,limit,mkbuttons){
	if(clear){
		document.getElementById("voicebox1").innerHTML = "";
		document.getElementById("voicebox2").innerHTML = "";
		document.getElementById("voicebox3").innerHTML = "";
		document.getElementById("voicebox4").innerHTML = "";
	}
	if(!start || start == null){var s=0;}
	else{var s = start;}
	
	var sources = obj.getElementsByTagName("source");
	if(limit){limit = Math.min(sources.length,limit)}
	else {limit = sources.length}
	var end = Math.min(sources.length,s + limit);
	
	if(document.getElementById("pictureviewer")){var viewerHeight = Math.max(parseInt(document.getElementById("pictureviewer").offsetHeight),300);}
	else {var viewerHeight = 0;}
	
	while(s < end){
		var currBox = document.getElementById("voicebox1");
		for(vb=2;vb<3;vb++){
			if(parseInt(document.getElementById("voicebox"+vb).scrollHeight) < parseInt(currBox.scrollHeight)){
				currBox = document.getElementById("voicebox"+vb);
		}}
		for(vb=3;vb<5;vb++){
			if(parseInt(document.getElementById("voicebox"+vb).scrollHeight) + viewerHeight < parseInt(currBox.scrollHeight)){
				currBox = document.getElementById("voicebox"+vb);
		}}
		var source = sources[s];
		if(!source || !source.getElementsByTagName("title")[0]){continue}
		var sourceImgs = source.getElementsByTagName("content")[0].firstChild.nodeValue.match(/src=("|')\S+(?:\.(jpg|png))/gi);
		if(sourceImgs){
			for(i=0;i<sourceImgs.length;i++){
				imageArray.push("pvImg"+imageArray.length);
				var arraynum = imageArray.length-1;
				imageArray["pvImg"+arraynum+"_src"] = sourceImgs[i].replace(/(^src=")/i,"");;
				imageArray["pvImg"+arraynum+"_blog"] = source.getElementsByTagName("blog")[0].firstChild.nodeValue;
				imageArray["pvImg"+arraynum+"_link"] = source.getElementsByTagName("link")[0].firstChild.nodeValue;
			}
		}
		currBox.innerHTML += "<a class='title' target='_blank' href='"+source.getElementsByTagName("link")[0].firstChild.nodeValue+"'>"+source.getElementsByTagName("title")[0].firstChild.nodeValue+"</a>";
		currBox.innerHTML += "<div class='content'>"+source.getElementsByTagName("content")[0].firstChild.nodeValue+" <a target='_blank' href='"+source.getElementsByTagName("link")[0].firstChild.nodeValue+"'>read more.</a><p class='date'><span class='blogtitle'>Source: "+source.getElementsByTagName("blog")[0].firstChild.nodeValue+"</span><br />"+source.getElementsByTagName("date")[0].firstChild.nodeValue+"</p></div>";
		s++;		
	}	
	//make pages buttons
	if(mkbuttons){
		var pages = Math.ceil(parseInt(sources.length) / parseInt(limit));
		var buttons = "<a href='#' class='active' onclick='processVoices(voiceObj,true,0,"+limit+",false)'>1</a>";
		for(p=1;p<pages;p++){
			buttons += "<a href='#' onclick='processVoices(voiceObj,true,"+p*limit+","+limit+",false)'>"+(p+1)+"</a>";
		}
		document.getElementById("voicepages").innerHTML = buttons;
	}
}
