/* global $ */
/* eslint "max-len": "off" */
/* eslint "no-unused-vars": "off" */

var tutorialFunctions = function(makeAccordion, tutorials) {
  var removeTutorial = function(title, div, button) {
    return function() {
      button.remove();
      div.remove();
      title.remove();
    };
  };

  var insertDeleteButtonAtLastTutorial = function(tutorialMenu) {
    var lastTitle = tutorialMenu.prev().prev();
    var lastDiv = tutorialMenu.prev();
    var deleteButton = $("<span>");
    deleteButton.addClass("close-icon ui-icon ui-icon-close");
    lastTitle.prepend(deleteButton);
    deleteButton.click(removeTutorial(lastTitle, lastDiv, deleteButton));
  };

  var populateTutorialElement = function(theHtml) {
    // populate a Tutorial element, and return it
    var theLessons = [];
    var tutorial = $("<div>").html(theHtml);
    $("div", tutorial).each(function() {
      theLessons.push({
        title: $("h4:first", $(this)).text(),
        html: $(this)
      });
    });
    return { // class Tutorial
      title: $("<h3>").append($("title", tutorial).text()),
      current: 0,
      lessons: theLessons
    };
  };

  var makeTutorialsList = function(i, tutorialNames, callback) {
    if (i < tutorialNames.length) {
      $.get(tutorialNames[i], function(resultHtml) {
        tutorials[i] = populateTutorialElement(resultHtml);
        console.log(tutorials[i].title);
        makeTutorialsList(i + 1, tutorialNames, callback);
      });
    } else {
      callback();
    }
  };

  var importTutorials = function() {
    console.log("Import tutorials.");

    $.ajax({
      url: '/getListOfTutorials',
      type: 'GET',
      statusCode: {
        500: function(error) {
          console.log("There was an error obtaining the list of tutorial files: " + error);
        }
      },
      success: function(tutorialData) {
        console.log("Obtaining list of tutorials successful: " + tutorialData);
        var tutorialPaths = JSON.parse(tutorialData);
        makeTutorialsList(0, tutorialPaths, function() {
          makeAccordion(tutorials);
        });
      }
    });
    return false;
  };

  return {
    insertDeleteButtonAtLastTutorial: insertDeleteButtonAtLastTutorial,
    importTutorials: importTutorials,
    populateTutorialElement: populateTutorialElement
  };
};

@@ -79,72 +79,8 @@
  */%0A
-%0Avar tutorialFunctions = function(makeAccordion, tutorials) %7B%0A  
 var 
@@ -127,18 +127,16 @@
 tton) %7B%0A
-  
   return
@@ -153,18 +153,16 @@
 ) %7B%0A    
- 
- 
 button.r
@@ -174,18 +174,16 @@
 ();%0A    
-  
 div.remo
@@ -188,26 +188,24 @@
 move();%0A    
- 
- 
 title.remove
@@ -208,31 +208,25 @@
 move();%0A
-  
   %7D;%0A
-  
 %7D;%0A%0A
-  
 var inse
@@ -281,18 +281,16 @@
 Menu) %7B%0A
-  
   var la
@@ -325,26 +325,24 @@
 ).prev();%0A  
- 
- 
 var lastDiv 
@@ -364,18 +364,16 @@
 prev();%0A
-  
   var de
@@ -396,26 +396,24 @@
 %3Cspan%3E%22);%0A  
- 
- 
 deleteButton
@@ -459,18 +459,16 @@
 lose%22);%0A
-  
   lastTi
@@ -494,18 +494,16 @@
 utton);%0A
-  
   delete
@@ -566,24 +566,20 @@
 tton));%0A
-  
 %7D;%0A%0A
-  
 var popu
@@ -624,60 +624,8 @@
 ) %7B%0A
-    // populate a Tutorial element, and return it%0A  
   va
@@ -643,18 +643,16 @@
 s = %5B%5D;%0A
-  
   var tu
@@ -686,18 +686,16 @@
 eHtml);%0A
-  
   $(%22div
@@ -729,18 +729,16 @@
 ) %7B%0A    
-  
 theLesso
@@ -747,18 +747,16 @@
 .push(%7B%0A
-  
       ti
@@ -797,18 +797,16 @@
 ,%0A      
-  
 html: $(
@@ -819,26 +819,20 @@
     
-  
 %7D);%0A  
- 
- 
 %7D);%0A  
- 
- 
 retu
@@ -854,18 +854,16 @@
 utorial%0A
-  
     titl
@@ -916,18 +916,16 @@
 )),%0A    
-  
 current:
@@ -928,18 +928,16 @@
 ent: 0,%0A
-  
     less
@@ -956,21 +956,78 @@
 ons%0A
-  
   %7D;%0A
-  
 %7D;%0A%0A
+var tutorialFunctions = function(makeAccordion, tutorials) %7B%0A
   va

Refactor tutorial code