var membersNavigator;
function initMembers(numPerPage)
{
	MembersNavigator.prototype = new PageNavigator;
	membersNavigator = new MembersNavigator('memberList', numPerPage, 3);

	var element = $('members_count');
	if (element == null) return;
	var count = parseInt(element.innerHTML);
	if (isNaN(count) || count == 0) return;

	membersNavigator.secondNavigator = $('topNavigator');
	membersNavigator.jumpElement = $('listTitle');
	membersNavigator.setNumItems(count);
	membersNavigator.setPage(0);
}

/** Gallery comment navigator object */
function MembersNavigator(elementIdPrefix,numPerPage,pageSpan)
{
	var myself = this;
	PageNavigator.apply(this,arguments);
	/** Valid options are 'id','name','numcomm' and 'lastcomm' */
	this.sortMethod = MembersNavigator.defaultSortMode;
	/** Will contain last search value, or null on 'all' view */
	this.searchVal = null;
	this.picsOnly = true;
	// Contains parameters that are sent with the request. Will be applied if request is successful.
	this.requestData = new Object();

	this.request = new XHR(15000);
	this.serverScript = window.location;
	// Using a RowLighter (From common.js)
	this.highlighter = new RowLighter('mooshy','#dce0e2');
	
	/** We keep track of sorting links from the sortby menu 
	 * (Making this class pretty much related to the rest of the page) 
	 */
	this.sortByOptions = $('sortByContainer').getElementsByTagName('div');
	// Finish initing sortByOptions
	for (var i=0;i<this.sortByOptions.length;i++)
	{
		this.sortByOptions[i].style.cursor = 'pointer';
		this.sortByOptions[i].onclick = sortByClick; // Function defined below
	}

	this.highlightedSortBy = null;
	this.chosenSortBy = null;
	this.sortByChosenColor = '#108fc4';
	this.sortByHighColor = '#FFEE00';
	this.sortByStdColor = '#000000';

	this.onError = function(errorText)
	{
		if (myself.highlightedSortBy)
		{
			if (myself.highlightedSortBy == myself.chosenSortBy) 
				myself.highlightedSortBy.style.color = myself.sortByChosenColor;
			else
				myself.highlightedSortBy.style.color = myself.sortByStdColor;				

			myself.highlightedSortBy = null;
		}
		alert(errorText);
	}	
	this.request.SetErrorFunc(this.onError);

	this.onPageRequest = function(pageNum)
	{
		this.requestData.picsOnly = false;
		this.requestData.pageNum = pageNum;

		if (this.requestData.searchVal) 
		{
			var postInfo = 'act=search&value=' + this.requestData.searchVal;
		}
		else 
		{
			var postInfo = 'act=list&sort=' + this.requestData.sortMethod;
			if ($('picsOnly').checked) 
			{
				this.requestData.picsOnly = true;
				postInfo += '&picsOnly=1';
			}
		}

		postInfo += '&page='+(pageNum+1);
		this.request.Post(this.serverScript,postInfo,this.onRequestResult,this.requestData);
	}

	this.onRequestResult = function(responseText,requestData)
	{
		if (myself.highlightedSortBy)
		{
			myself.highlightedSortBy.style.color = myself.sortByChosenColor;
			if (myself.chosenSortBy && myself.chosenSortBy != myself.highlightedSortBy)
			{
				myself.chosenSortBy.style.color = myself.sortByStdColor;
				myself.chosenSortBy = myself.highlightedSortBy;
			}
			myself.highlightedSortBy = null;
		}
		PageNavigator.prototype.onRequestResult.call(myself,responseText,requestData.pageNum,'members_count');
		// Set the title above the list
		var listTitle = $('listTitle');
		if (requestData.searchVal == null)
			listTitle.innerHTML = (requestData.picsOnly) ? "רשימת שינים בעלי תמונות" : "רשימת כל השינים";			
		else
			listTitle.innerHTML = 'חיפוש שינים לפי "' + htmlSpecialChars(requestData.searchVal)
								 + '". נמצאו: ' + myself.numItems;
		// Google analytics
		pageTracker._trackPageview('/members/ajax');
		// Since the request was successful - assign request variables to navigator
		myself.searchVal  = requestData.searchVal;
		myself.picsOnly   = requestData.picsOnly;
		myself.sortMethod = requestData.sortMethod;
		
		// Init the highlighter
		this.highlighter = null;
		if (myself.numItems > 0) this.highlighter = new RowLighter('mooshy','#dce0e2');
	}

	this.search = function()
	{
		if (this.request.InProgress())
		{
			alert('אנא המתינו בסבלנות לסיום הפעולה הקודמת');
			return;
		} 
		var searchVal = trim($('searchVal').value);
		if (searchVal.length < MembersNavigator.minSearchLength)
		{
			alert("על שורת החיפוש להכיל לפחות " + MembersNavigator.minSearchLength + " תוים");
			return;
		}
		for (var i=0;i<this.sortByOptions.length;i++)
		{
			this.sortByOptions[i].style.color = this.sortByStdColor;
		}
		this.requestData.sortMethod = MembersNavigator.defaultSortMode; 
		this.requestData.searchVal = searchVal;
		this.requestPage(0);
	}

	/** Private function - called when clicking on the sortBy links - need to use 'myself' */
	function sortByClick(evt)
	{
		if (myself.request.InProgress()) return;

		for (var i=0;i<myself.sortByOptions.length;i++)
		{
			if (myself.sortByOptions[i] == myself.chosenSortBy) continue;
			myself.sortByOptions[i].style.color = myself.sortByStdColor;
		}
		myself.highlightedSortBy = getEventTarget(evt);
		myself.highlightedSortBy.style.color = myself.sortByHighColor;

		myself.requestData.sortMethod = myself.highlightedSortBy.id;
		myself.requestData.searchVal = null;
		myself.requestPage(0);
	}
}
/** Static (Class) variables */
MembersNavigator.minSearchLength = 3;
MembersNavigator.defaultSortMode = 'id';
