/*
 * DIRECTSCIENCE.INFO ARTICLELIST JAVASCRIPT
 *
 * COPYRIGHT BOYANG ZHAO
 *
 * Revision Date: January 06, 2007 22:05
 * Revision: 3
 */

/*article list class*/
function articlelist(){
	this.loadingid = 'flloading';
	this.resultid = 'fllist';
	this.option = getSelectValue('flselect');
	this.addtxt = '<fieldset class="fbox"><legend><b>Topics</b></legend><a href="/'+this.option+'/topic.php">Click here</a> to find articles by '+this.option+' topics <b>>></b></fieldset>';
	this.qn = 80;
	this.format = 'xml';
	
	this.listurl = null;
	this.listpara = new Array();
	
	var self = this;
	this.responsei = true;
	this.start=0;
	
	this.initialize = function(){
		if(this.option == 'null'){
			document.getElementById(this.resultid).innerHTML = '';
		} else {
			this.getarticlelist();
		}
	};
	
	this.getarticlelist = function(){
		document.getElementById(this.loadingid).style.backgroundColor='#639C63';
		document.getElementById(this.loadingid).innerHTML = 'Loading...';
		this.listquery = new Array('option='+this.option,'start='+this.start,'qn='+this.qn);
		for(i=0;i<this.listpara.length;i++){
			this.listquery[i+3] = this.listpara[i];
		}
		ajax.getxmlhttp(this.listurl,this.responser,'POST',this.listquery,this.format);
	};
	
	this.responser = function(response){
		if(self.responsei){
			self.fresponsei(response);
		} else {
			self.faddresults(response);
		}
	}
	
	this.fresponsei = function(response){
		document.getElementById(self.loadingid).innerHTML = '';
		document.getElementById(self.loadingid).style.backgroundColor='white';
		document.getElementById(self.resultid).innerHTML = self.addtxt;
		
		var flck = response.getElementsByTagName("total")[0].firstChild.nodeValue;
		if(flck == '0'){
			var flr = createElement('div');
			flr.appendChild(document.createTextNode('No articles are found in database'));
			document.getElementById(self.resultid).appendChild(flr);
		} else {
			var fl = response.getElementsByTagName("fl");
			
			//container for the span's
			var flr = createElement('div');
			flr.setAttribute('id','flr');
			
			for(i=0;i<flck;i++){
				var flrs = createElement('span');
				flrs.setAttribute('id','fl'+i);
				flr.appendChild(flrs);
				flr.appendChild(createElement('br'));
			}
			
			document.getElementById(self.resultid).appendChild(flr);
			
			flr = document.getElementById('flr');
			
			flrst1 = 0;
			flrb = 0;
			
			//array to keep record of requested entries
			flrarray = new Array();
			for(i=0;i<flck;i=i+self.qn){
				flrarray[i.toString()] = 0;
			}
			
			self.faddresults(response);
			
			addEventHandler(flr,'scroll',function(){
				flrst = flr.scrollTop;
				flrn = Math.floor(flrst/15); //estimate list #
				flrnd = Math.ceil(flrn/self.qn); //section based on qn
				
				//ck if the current scroll is in the next section
				if(flrn - (self.qn*flrnd - 20) >= 0){
					flrb = (flrn - self.qn*flrnd <= 0) ? flrnd*self.qn : (flrnd-1)*self.qn;
				
					//ck if it is already requested	
					if(flrarray[flrb.toString()] == 0){
						self.start = flrb;
						self.getarticlelist();
					}
				}
			});
			
			self.responsei = false;
		}
	};
	
	this.faddresults = function(response){
		document.getElementById(self.loadingid).innerHTML = '';
		document.getElementById(self.loadingid).style.backgroundColor='white';
		
		var fl = response.getElementsByTagName("fl");
		var et = response.getElementsByTagName("etotal")[0].firstChild.nodeValue;
		
		for(i=0;i<et;i++){
			var n = fl[i].childNodes[0].firstChild.nodeValue;
			n = parseInt(n,'10') + flrb;
			var flrs = document.getElementById('fl'+n); //match with span id
			if(flrs.innerHTML == ''){
				var flra = createElement('a');
				flra.setAttribute('href','/'+fl[i].childNodes[1].firstChild.nodeValue);
				flra.appendChild(document.createTextNode(fl[i].childNodes[2].firstChild.nodeValue));
				flrs.appendChild(flra);
			}
		}
		
		//add to record array
		flrarray[flrb.toString()] = 1;
	};
}

/*fields*/
function getfieldlist(){
	list = new articlelist();
	list.listurl = '/dsmain/processes/getfieldlist.php';
	list.initialize();
}

function gofield(){
	var field = getSelectValue('flselect');
	if(field == '/fields/index.php?fieldoption=all'){
		location.href=field;
	} else {
		location.href='/'+field;
	}
}

function directfieldlist(field){
	document.getElementById('flselect').value=field;
	setTimeout(getfieldlist,500);
}

/*references*/
function getrefoptions(){
	var fieldoption = getSelectValue('flselect');
	document.getElementById("fllist").innerHTML = '';
	if(fieldoption == "null"){
		document.getElementById("refselect").disabled = true;
	} else {
		groarray = new Array('field='+fieldoption);
		url = '/dsmain/processes/getreferences.php';
		ajax.getxmlhttp(url,getrefoptionsr,'POST',groarray,'xml');
	}
}

function getrefoptionsr(response){
	document.getElementById("refselect").innerHTML = '';
	frselect = document.getElementById("refselect");
	
	frselecto = createElement('option');
	frselecto.setAttribute('value','null');
	frselecto.appendChild(document.createTextNode(' -- Select Reference -- '));
	frselect.appendChild(frselecto);
	
	fro = response.getElementsByTagName('option');
	for(i=0;i<fro.length;i++){
		frselecto = createElement('option');
		frselecto.setAttribute('value',fro[i].firstChild.nodeValue);
		frselecto.appendChild(document.createTextNode(fro[i].firstChild.nodeValue+"s"));
		frselect.appendChild(frselecto);
	}
	
	frselect.disabled = false;
	
	addEventHandler(document.getElementById('refselect'),'change',getreflist);
}

function getreflist(){
	var refoption = getSelectValue('refselect');
	if(refoption == "null"){
		document.getElementById("fllist").innerHTML = '';
	} else {
		list = new articlelist();
		list.listurl = '/dsmain/processes/getreflist.php';
		list.listpara = new Array('refoption='+refoption);
		list.getarticlelist();
	}
}

function directreflist(option,refoption){
	document.getElementById('flselect').value=option;
	getrefoptions();
	
	setTimeout (directreflistg,1000);

	function directreflistg(){
		document.getElementById('refselect').value=refoption;
		getreflist();
	}
}

/*topic*/
function gettopiclist(){
	list = new articlelist();
	list.listurl = '/dsmain/processes/gettopiclist.php';
	list.addtxt = '<div class="fbox"></div>';
	list.initialize();
}

function directtopiclist(topic){
	document.getElementById('flselect').value=topic;
	setTimeout(gettopiclist,500);
}
