/**
 * dao object must implement getPagingObject method.
 */ 
function Paging(dao, rowsToFetch, startPosition, userId, scope){
	this.daoObject = dao;
	this.rowsToFetch = rowsToFetch;
	if(startPosition != undefined){
		this.startPosition = startPosition;
	} else {
		this.startPosition = 0;
	}
	
	if(userId != undefined){
		this.userId = userId;
	} else {
		this.userId = null;
	}
	
	if(scope != undefined){
		this.scope = scope;
	} else {
		this.scope = null;
	}

	this.getPage();
}


Paging.prototype.daoObject = null;
Paging.prototype.paging = null;
Paging.prototype.list = null;
Paging.prototype.rowsToFetch = null;
Paging.prototype.startPosition = null;
Paging.prototype.rowsFetched = null;
Paging.prototype.hasNext = null;
Paging.prototype.hasPrevious = null;
Paging.prototype.totalRows = null;


Paging.prototype.getPage = function() {
	var tempList;
	var tempRowsFetched;
	var tempIsNext;
	var tempIsPrevious;
	var tempTotalRows;
		var options = {
		async: false, // synchronous call
		callback: function(reply) {
					  tempList = reply.list;
					  tempRowsFetched = reply.actualSize;
					  tempIsNext = reply.nextPos == -1 ? false : true;
					  tempIsPrevious = reply.prevPos == -1 ? false : true; 
					  tempTotalRows = reply.totalRows;
                 	  }
   	};
	if(this.scope == "ready") {
		var reply = jsonrpc.daoStatement.getPagingObject(this.startPosition, this.rowsToFetch, 1); //Order by DATE
	} else if(this.scope == "friends") {
		var reply = jsonrpc.daoStatement.getPagingFriends(this.startPosition, this.rowsToFetch, 1, this.userId); //Order by DATE
	} else if(this.scope == "friendsLite") {
		var reply = jsonrpc.daoStatement.getPagingFriendsLite(this.startPosition, this.rowsToFetch, 1, this.userId);
	} else if(this.scope == "public") {
		var reply = jsonrpc.daoStatement.getPagingPublic(this.startPosition, this.rowsToFetch, 1); //Order by DATE
	} else if(this.scope == "publicLite") {
		var reply = jsonrpc.daoStatement.getPagingPublicLite(this.startPosition, this.rowsToFetch, 1);
	} else if(this.scope == "notesPublic") {
		var reply = jsonrpc.daoStatement.getZlangoNotes(this.startPosition, this.rowsToFetch, 1, this.userId, "public", getServerPath());
	} else if(this.scope == "notesFriends") {
		var reply = jsonrpc.daoStatement.getZlangoNotes(this.startPosition, this.rowsToFetch, 1, this.userId, "friends", getServerPath());
	} else if(this.scope == "notesMy") {
		var reply = jsonrpc.daoStatement.getZlangoNotes(this.startPosition, this.rowsToFetch, 1, this.userId, "my", getServerPath());
	} else if(this.scope == "notesFriend") {
		var reply = jsonrpc.daoStatement.getZlangoNotes(this.startPosition, this.rowsToFetch, 1, this.userId, "friend", getServerPath());
	} else if(this.scope == "editorPick") {
		var reply = jsonrpc.daoStatement.getPagingEditorPick(this.startPosition, this.rowsToFetch, 1);
	} else if(this.scope == "editorPickLite") {
		var reply = jsonrpc.daoStatement.getPagingEditorPickLite(this.startPosition, this.rowsToFetch, 1);
	}

	tempList = reply.list;
	tempRowsFetched = reply.actualSize;
	tempIsNext = reply.nextPos == -1 ? false : true;
	tempIsPrevious = reply.prevPos == -1 ? false : true; 
	tempTotalRows = reply.totalRows;
	
	this.page = reply;
   	this.list = tempList.list;
   	this.rowsFetched = tempRowsFetched;
   	this.hasNext = tempIsNext;
   	this.hasPrevious = tempIsPrevious;
   	this.totalRows = tempTotalRows;
};

Paging.prototype.getPageByIndex = function(index) {
	this.startPosition = index;
	this.getPage();
}

Paging.prototype.getFirstPage = function() {
	this.startPosition = 0;
	this.getPage();
}

Paging.prototype.getLastPage = function() {
	this.startPosition += this.rowsToFetch;
	
	var lastPos = this.totalRows - this.rowsToFetch;
	this.startPosition = lastPos < 0 ? 0 : lastPos;
	this.getPage();
}

Paging.prototype.getNextPage = function() {
	this.startPosition += this.rowsToFetch;
	this.getPage();
}

Paging.prototype.getPreviousPage = function() {
	this.startPosition -= this.rowsToFetch;
	this.getPage();
}

Paging.prototype.getList = function() {
	return this.list;
}

Paging.prototype.getRowsFetched = function() {
	return this.rowsFetched;
}

Paging.prototype.getHasNext = function() {
	return this.hasNext;
}

Paging.prototype.getHasPrevious = function() {
	return this.hasPrevious;
}

Paging.prototype.getTotalRows = function() {
	return this.totalRows;
}

Paging.prototype.getStartPosition = function() {
	return this.startPosition;
}

Paging.prototype.getGetPageInformationString = function() {
	return  "(" + (this.startPosition + 1) + " - " + (this.startPosition + this.rowsFetched) + " of " + this.totalRows + ")";
}
