define([
	'backbone',
	'Patient'
],
	function(Backbone, Patient){
		var singleInstance,
			Patients = Backbone.Collection.extend({
			model: Patient,
			initialize: function(){
			},
			parse: function(response){
                response = response || {};

				if(typeof response.selectedPatient !== 'undefined')
				{
					var length = response.selectedPatient.length;
						response.patient = [];
					for(var i = 0; i < length; i++)
					{
						response.patient.push(response.selectedPatient[i].patient);
					}


					this.totalResults = response.patient.length;
					this.numberOfDisplayedResults = this.totalResults;
					this.nextPage = null;

					return response.patient;
				}
				if (response.paging) {
					var pageSize = response.paging.pageSize,
						thisPage = response.paging.thisPage,
						size = response.size;
					
					this.numberOfDisplayedResults = (thisPage - 1) * pageSize + size;
					this.totalResults = response.paging.totalResults;
					this.nextPage = response.paging.nextPage;
					
					return response.patient;
				} else {
					this.numberOfDisplayedResults = 0;
					this.totalResults = 0;
					this.nextPage = null;
				}
			}
		});
		
		Patients.instance = function() {
			if (typeof singleInstance === "undefined") {
				singleInstance = new Patients();
			}
			return singleInstance;
		};
		
		return Patients;
	}
);



