include_once('/Scripts/dynamicnavigator.js');

/** Static constants */
MemberNavigator.LISTENER_SCRIPT = "/people/listener.php";

/** 
 * Requires a div called X_content and another area called X_pageNavigator
 * Also result should return a span element or equal names X_totalFound
 */
function MemberNavigator(objId,argNumPerPage,argVarName)
{
	// private properties
	var varName = argVarName; // for dynamically updated page nav links
	var numPerPage=argNumPerPage;
	var totalPages=1; // so it won't return on first set to 0
	var currentPage=0;
	var pageSpan = 4;	// how many pages left and right will there be in the navigator


	// public properties
	this.objId = objId; // used for finding the totalfound element
	this.request = new XHR();
	this.request.requestTimeout = 10000; // should be plenty of time for fetch
	
	this.content = document.getElementById(objId+'_content');

	this.pageNavigator = document.getElementById(objId+'_pageNavigator');
	this.bottomPageNavigator = document.getElementById(objId+'_bottomPageNavigator');

	/** Sets current page
	 * 
	 * Except for the obvious, sends an AJAX call to the server. Result is in JSON,
	 * and is parsed as a JS object which contains the friends' info
	 */
	this.SetPage = function (pageNum)
	{
		// Note: If no pageNum entered just refresh with current page
		if (typeof(pageNum) != 'undefined') currentPage = pageNum;
		// if request is in progress ignore
		if (this.request.InProgress()) return;
		var POSTInfo = "act=getmembers&page="+currentPage;
		var mySelf = this; // a little trick to send 'this' to the xhr
		this.request.Post(MemberNavigator.LISTENER_SCRIPT,POSTInfo,RequestResultHandler,mySelf);
	}

	/** Submits new search parameters to server */
	this.PostSearch = function(type)
	{
		// if request is in progress ignore
		if (this.request.InProgress()) return false;
		var POSTInfo = 'act=getmembers&start=0&num_items='+numPerPage;
		switch (type)
		{
			case 'adv':
				// we collect info from gender,age and the rest of them (9 opts total)
				POSTInfo += '&newsearch=1&type=adv';
				var age_min = document.getElementById('search_age_min').value;
				var age_max = document.getElementById('search_age_max').value;
				var gender=null;
				var temp = document.getElementsByName('search_gender');
				for (var i=0;i<temp.length;i++)
				{
					if (temp[i].checked)
					{
						gender = temp[i].value;
						break;
					}
				}
				if (document.getElementById('search_online').checked)
					POSTInfo += '&search_online=1';
				if (document.getElementById('search_pic').checked)
					POSTInfo += '&search_pic=1';
				temp = document.getElementById('sort_order');
				var sort = temp.options[temp.selectedIndex].value;
				temp = document.getElementById('search_country');
				var country = temp.options[temp.selectedIndex].value;
				temp = document.getElementById('search_status');
				var rel_status = temp.options[temp.selectedIndex].value;
				temp = document.getElementById('search_religion');
				var religion = temp.options[temp.selectedIndex].value;
				if (age_min) POSTInfo += '&search_age_min='+age_min;
				if (age_max) POSTInfo += '&search_age_max='+age_max;
				if (gender)  POSTInfo += '&search_gender='+gender;
				if (sort)    POSTInfo += '&sort_order='+sort;
				if (country != 0) POSTInfo += '&search_country='+country;
				if (rel_status != 0) POSTInfo += '&search_status='+rel_status;
				if (religion != 0) POSTInfo += '&search_religion='+religion;
			break;
			case 'name':
				POSTInfo += '&newsearch=1&type=name&search_name=' + document.getElementById('search_name').value;
			break;
			case 'email':
				POSTInfo += '&newsearch=1&type=email&search_email=' + document.getElementById('search_email').value;
			break;
			case 'switchview':
				POSTInfo += '&switchviewmode=1&page='+currentPage;
			break;
		}
		var mySelf = this; // a little trick to send 'this' to the xhr
		this.request.Post(MemberNavigator.LISTENER_SCRIPT,POSTInfo,RequestResultHandler,mySelf);
		if (type != 'switchview')
		{
			currentPage = 0;
			return false; // prevents name/email forms from being submitted
		}
	}

	/** Result of AJAX request */
	function RequestResultHandler(responseText,mNavigator)
	{
		// since the update process is only a few lines, I do it all here
		mNavigator.content.innerHTML = responseText;
		// response text should contain this part that has the total members:
		var foundElement = document.getElementById(mNavigator.objId+'_totalFound');
		if (foundElement)
		{
			var totalFound = parseInt(foundElement.innerHTML);
			totalPages = Math.ceil(totalFound/numPerPage);
			mNavigator.pageNavigator.innerHTML = DynamicNavigator.PageNavigatorCalc(currentPage,totalPages,pageSpan,varName);
			mNavigator.bottomPageNavigator.innerHTML = mNavigator.pageNavigator.innerHTML;
		}
		else
		{
			totalPages = 0;
			mNavigator.pageNavigator.innerHTML = '';
			mNavigator.bottomPageNavigator.innerHTML = '';
		}
	}
	
}