var __base = __base || '../',
    c = require(__base + 'shared-config/constants'),
    mongodb = require(__base + 'shared-utils/mongo-db'),

   	//move to constants
    DATABASE = c.db.fatalities,
    COLLECTION = c.collection.fatalities,
    DEFAULT_LIMIT = 10;

function getModel (d, cb) {

    function getData(err, db, close){

        if(err){
            c.l('err', err);
            return;
        }

        var limit = parseInt(d.limit) || DEFAULT_LIMIT,
            page = parseInt(d.page),
            startAt = page * limit,
            collection = db.collection(COLLECTION),
            data = {
                page: page,
                limit: limit
            };

        function filterOptions(){
        	return {};
    	}

   		function queryFilter(){
        	return {};
    	}

    	function querySelect(){
        	return {
            	"value.subject.name" : true,
           		"value.subject.age" : true,
            	"value.subject.race" : true,
           		"value.subject.sex" : true,
           		"value.death.cause" : true,
            	"value.death.event.date" : true,
            	"value.location.state" : true
        	}
    	}

    	function querySort(){
        	return { 
            	"value.death.event.date" : -1
       		}
    	}

    	function buildModel(body, data){
        	return {
            	'body' : body,
            	'data' : data
        	}
    	}

        function getCount(countCb){
            
            collection
            .find(queryFilter(), querySelect())
            .count(function(err, count){

            	if(err){
            		cb(err);
            	}
                
                countCb(count);
            
            })
        
        }
        
        
        function getResults(count){

        	function render(err, model){

        		if(err){
            		c.l('err', err);
           			cb(err);
            	}

	        	cb(null, {
	            	results: model.body,
	           		count: model.data.count,
	            	filters: filterOptions(),
	            	locals: { 
	                	title: 'List of Fatalities',
	                	js: ['config/list'],
	                	css: ['list']
	            	}
	        	});

        		close();
    		}

            collection.find(queryFilter(), querySelect())

            .sort(querySort())

            .skip(startAt).limit(limit)
            
            .toArray(function(err, body){

                if(err){
            		c.l('err', err);
           			cb(err);
            	}

                c.l('body', body);

	        	cb(null, {
	            	results: body,
	           		count: count,
	            	filters: filterOptions()
	        	});

        		close();
            
            });
        }
        
        //init
        getCount(function(count){

            getResults(count);
        
        });
        
    }

    mongodb(DATABASE, getData);
}

module.exports = getModel;
@@ -348,32 +348,46 @@
       if(err)%7B%0D%0A
+            %0D%0A
             c.l(
@@ -390,32 +390,44 @@
 c.l('err', err);
+            
 %0D%0A            re
@@ -761,24 +761,26 @@
 Options()%7B%0D%0A
+%0D%0A
         %09ret
@@ -829,32 +829,46 @@
 er()%7B%0D%0A        %09
+%0D%0A            
 return %7B%7D;%0D%0A    
@@ -904,32 +904,46 @@
 ct()%7B%0D%0A        %09
+%0D%0A            
 return %7B%0D%0A      
@@ -1290,32 +1290,46 @@
 rt()%7B%0D%0A        %09
+%0D%0A            
 return %7B %0D%0A     
@@ -1429,32 +1429,46 @@
 ata)%7B%0D%0A        %09
+%0D%0A            
 return %7B%0D%0A      
@@ -1936,494 +1936,8 @@
 %0D%0A%0D%0A
-        %09function render(err, model)%7B%0D%0A%0D%0A        %09%09if(err)%7B%0D%0A            %09%09c.l('err', err);%0D%0A           %09%09%09cb(err);%0D%0A            %09%7D%0D%0A%0D%0A%09        %09cb(null, %7B%0D%0A%09            %09results: model.body,%0D%0A%09           %09%09count: model.data.count,%0D%0A%09            %09filters: filterOptions(),%0D%0A%09            %09locals: %7B %0D%0A%09                %09title: 'List of Fatalities',%0D%0A%09                %09js: %5B'config/list'%5D,%0D%0A%09                %09css: %5B'list'%5D%0D%0A%09            %09%7D%0D%0A%09        %09%7D);%0D%0A%0D%0A        %09%09close();%0D%0A    %09%09%7D%0D%0A%0D%0A
     
@@ -2068,28 +2068,16 @@
 limit)%0D%0A
-            
 %0D%0A      
@@ -2133,32 +2133,34 @@
       if(err)%7B%0D%0A
+%0D%0A
             %09%09c.
@@ -2171,24 +2171,26 @@
 rr', err);%0D%0A
+%0D%0A
            %09
@@ -2193,32 +2193,38 @@
    %09%09%09cb(err);%0D%0A
+%0D%0A    
             %09%7D%0D%0A
@@ -2215,26 +2215,36 @@
             
-%09%7D
+    close();
 %0D%0A%0D%0A        
@@ -2255,26 +2255,35 @@
     
-c.l('body', body);
+    return;%0D%0A            %09%7D
 %0D%0A%0D%0A
@@ -2304,16 +2304,18 @@
 ull, %7B%0D%0A
+%0D%0A
 %09       
@@ -2405,25 +2405,28 @@
 ions()%0D%0A
-%09
+%0D%0A  
         
 %09%7D);%0D%0A%0D%0A
@@ -2417,17 +2417,22 @@
         
-%09
+      
 %7D);%0D%0A%0D%0A 
@@ -2446,36 +2446,24 @@
 %09%09close();%0D%0A
-            
 %0D%0A          

format cleanup