var exphbs = require('express-handlebars');
var moment = require('moment');

var activeRoute = '';
var blocks = [];

function HandlebarBoot(expressApp, viewsPath) {
    var Handlebars = exphbs.create({
        defaultLayout: 'main',
        layoutsDir: viewsPath + '/layouts/',
        partialsDir: viewsPath + '/partials/',
        helpers: {
            url: function(routeName, params) {
                return expressApp.locals.url(routeName, params);
            },
            activeRoute: function(routeName) {
                return routeName === activeRoute ? 'active' : '';
            },
            activeRoutes: function(routeNames) {
                // TODO
                return routeNames.split(',').indexOf(activeRoute) >= 0 ? 'active' : '';
            },
            isYes: function(value) {
                if(value=='Y'){
                    return 'checked'
                }else{
                    return '';
                }
            },
            isNo: function(value) {
                if(value=='N'){
                    return 'checked'
                }else{
                    return '';
                }
            },
            isAdmin: function(value) {
                if(value=='Admin'){
                    return 'checked'
                }else{
                    return '';
                }
            },
            isOperator: function(value) {
                if(value=='Operator'){
                    return 'checked'
                }else{
                    return '';
                }
            },
            isReader: function(value) {
                if(value=='Reader'){
                    return 'checked'
                }else{
                    return '';
                }
            },
            block: function(name) {
                var val = (blocks[name] || []).join('\n');

                // clear the block
                blocks[name] = [];
                return val;
            },
            formatTimeDate: function(date, displaySecond) {

                if(displaySecond === true)
                    return moment(date).format('YYYY-MM-DD HH:mm:ss');
                else
                    return moment(date).format('YYYY-MM-DD HH:mm');
            },
            pagination: function(currentPage, totalCount) {

                console.log('currentPage : '+currentPage);
                console.log('totalCount : '+totalCount);


                var str = '';
                var perPage = 15;
                var lastPage = Math.ceil((totalCount / perPage - 1) + 1);
                var maxDisplayPage = 10;

                console.log('lastPage : '+lastPage);

                if(currentPage > 1){
                    str += '<li><a href="?page='+(currentPage - 1) + '">&laquo;</a></li>';
                }
                str += '<li><a href="#">' + currentPage  + '</a></li>';
                var i = currentPage + 1;
                for(; i < lastPage ; i++)
                {
                    str += '<li><a href="?page='+i + '">'+i+'</a></li>';
                }
                console.log(' after loop i '+i);
                if(i <= lastPage){
                    str += '<li><a href="?page=';
                    str += i;
                    str += '">&raquo;</a></li>';
                }
                return str;
            },
            extend: function(name, context) {
                var block = blocks[name];
                if (!block) {
                    block = blocks[name] = [];
                }

                block.push(context.fn(this)); // for older versions of handlebars, use block.push(context(this));
            }
        }
    });

    // View engine setup
    expressApp.engine('handlebars', Handlebars.engine);
    expressApp.set('view engine', 'handlebars');
    expressApp.set('views', viewsPath);
}

module.exports = HandlebarBoot;



@@ -2311,126 +2311,8 @@
  %7B%0A%0A
-                console.log('currentPage : '+currentPage);%0A                console.log('totalCount : '+totalCount);%0A%0A%0A
     
@@ -2371,16 +2371,48 @@
 e = 15;%0A
+                var block = 5;%0A%0A
         
@@ -2477,16 +2477,17 @@
 ) + 1);%0A
+%0A
         
@@ -2502,84 +2502,116 @@
 var 
-maxDisplayPage = 10;%0A%0A                console.log('lastPage : '+lastPage
+totalPage = Math.ceil(totalCount / perPage);%0A                var totalBlock = Math.ceil(totalPage / block
 );%0A
-%0A
     
@@ -2622,18 +2622,44 @@
         
-if
+var currentBlock = Math.ceil
 (current
@@ -2667,13 +2667,18 @@
 age 
-%3E 1)%7B
+/ block);%0A
 %0A   
@@ -2694,83 +2694,63 @@
     
-    str += '%3Cli%3E%3Ca href=%22?page='+(currentPage - 1) + '%22%3E&laquo;%3C/a%3E%3C/li%3E'
+var startPage = (currentBlock * block) - (block - 1)
 ;%0A
+%0A
     
@@ -2765,81 +2765,138 @@
     
-%7D%0A                str += '%3Cli%3E%3Ca href=%22#%22%3E' + currentPage  + '%3C/a%3E%3C/li%3E';
+if(startPage %3C 1) %7B startPage = 1;%7D%0A%0A                var endPage = currentBlock * block;%0A                if(totalPage %3C= endPage)%7B
 %0A   
@@ -2912,31 +2912,31 @@
     
-var i = current
+    end
 Page 
-+ 1
+= totalPage
 ;%0A  
@@ -2953,33 +2953,67 @@
     
-for(; i %3C lastPage ; i
+%7D%0A%0A                for(var p = startPage; p%3C=endPage; p
 ++)
+%7B
 %0A   
@@ -3029,41 +3029,101 @@
     
-%7B%0A                    str += '%3Cli
+    if(currentPage == p) %7B%0A                        str += '%3Cli class=%22paginate_button active%22
 %3E%3Ca 
@@ -3139,20 +3139,26 @@
 ge='
-+i
+ + p
  + '%22%3E'
-+i+
+ + p + 
 '%3C/a
@@ -3174,38 +3174,32 @@
             
-    %7D%0A
         
         cons
@@ -3190,212 +3190,144 @@
     
-        console.log(' after loop i '+i);%0A                if(i %3C= lastPage)%7B%0A                    str += '%3Cli%3E%3Ca href=%22?page=';%0A                    str += i;%0A                    str += '%22%3E&raquo;%3C/a%3E%3C/li%3E';
+%7Delse %7B%0A                        str += '%3Cli class=%22paginate_button%22%3E%3Ca href=%22?page=' + p + '%22%3E' + p + '%3C/a%3E%3C/li%3E';%0A                    %7D
 %0A   
@@ -3333,32 +3333,33 @@
               %7D%0A
+%0A
                 

fix pagination display.