'use strict';

var fs = require('fs');
var path = require('path');
var async = require('async');
var config = require('../config');
var $ = require('./jquery');  


var directoryFiles = [];

exports.getFiles = function (directories, callback) {
		
    console.log('getting files...');                				       

    async.forEach(directories, exports.walkDirectories, function(err) {
        console.log("all done");
        callback(directoryFiles);
    });      
};

exports.walkDirectories = function (directory, done) {
        
    setTimeout(function() {
        exports.walk (directory, function (error, results) {
                                
            if (error)	{
                console.log(error);				
            }
            else {
                console.log('adding files: ' +  results);
                directoryFiles.push(results);                
                done();
            }
        })                    
    }, 2000);
         
};


exports.walk = function(dir, done) {
	
  var results = [];
  
  fs.readdir(dir, function(err, list) {
  
    if (err) {
	  return done(err);  
    }
  
    var pending = list.length;  
    if (!pending) {
	  return done(null, results);
    }

    list.forEach( function(file) {
		
      file = path.resolve(dir, file);
      fs.stat(file, function(err, stat) {
        if (stat && stat.isDirectory()) {
          exports.walk(file, function(err, res) {
            results = results.concat(res);
            if (!--pending){
				done(null, results);
			}
          });

        } else {			
		
		  var isImage = false;
		  for(var i = 0; i < config.supported_extensions.length; i++) {
		    if (file.toLowerCase().endsWith(config.supported_extensions[i])){
				isImage = true;
				break;
			}  			  
		  }
		  
          if (isImage) {
			results.push(file);  
		  }
          
          if (!--pending) {
			done(null, results);  
		  }
        }
      });
	  
    });
	
  });
  
};

exports.deleteFile = function(filename) {
    fs.unlink(filename);
};

exports.shuffle = function(array) {
        
  var currentIndex = array.length;
  var temporaryValue;
  var randomIndex;
  
  while (0 !== currentIndex) {
      
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
    
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
    
  }
  
  return array;
}
@@ -316,20 +316,17 @@
   async.
-forE
+e
 ach(dire
@@ -507,20 +507,24 @@
 ectory, 
-done
+callback
 ) %7B%0A    
@@ -532,42 +532,8 @@
    %0A
-    setTimeout(function() %7B%0A      
   ex
@@ -579,32 +579,25 @@
  results) %7B%0A
-        
+%09
             
@@ -605,32 +605,24 @@
            %0A
-        
     if (erro
@@ -626,26 +626,16 @@
 rror)%09%7B%0A
-          
       co
@@ -660,34 +660,18 @@
 %09%09%09%0A    
-        %7D%0A        
+%7D%0A
     else
@@ -673,26 +673,16 @@
  else %7B%0A
-          
       co
@@ -721,26 +721,16 @@
 sults);%0A
-          
       di
@@ -783,84 +783,31 @@
     
-          done();%0A            %7D%0A        %7D)                    %0A    %7D, 2000);
+callback();%0A    %7D%0A  %7D) 
 %0A   
@@ -2281,8 +2281,9 @@
 array;%0A%7D
+%0A

Update utilities.js