'use strict';

module.exports = LessonHeader;

import Flickity from 'flickity-imagesloaded'

function LessonHeader() {

	console.log("-- LessonHeader initialized")

	let photosSelector = '.LessonHeader__photos'
	let photoSelector = '.LessonHeader__photo'

	let $status = $('.LessonHeader__photos__status')
	let $current = $status.find('.current')
	let $total = $status.find('.total')

  // Init flickity for all carousels
  let flkty = new Flickity(photosSelector, {
    cellAlign: 'center',
    cellSelector: photoSelector,
    contain: true,
    pageDots: false,
    prevNextButtons: false,
    wrapAround: true,
		imagesLoaded: true,
    percentPosition: true,
  })

  document.addEventListener("turbolinks:request-start", function() {
    flkty.destroy()
  })

	$total.html($(photoSelector).length)

	flkty.on( 'select', function() {
		$current.html(flkty.selectedIndex + 1)
	})

  return flkty


}

@@ -204,16 +204,79 @@
 photos'%0A
+%09let $photos = $(photosSelector)%0A%0A%09if ($photos.length %3E 0) %7B%0A%0A%09
 %09let pho
@@ -312,16 +312,17 @@
 photo'%0A%0A
+%09
 %09let $st
@@ -363,16 +363,17 @@
 tatus')%0A
+%09
 %09let $cu
@@ -405,16 +405,17 @@
 rrent')%0A
+%09
 %09let $to
@@ -444,16 +444,17 @@
 otal')%0A%0A
+%09
   // Ini
@@ -482,16 +482,17 @@
 rousels%0A
+%09
   let fl
@@ -528,16 +528,17 @@
 ctor, %7B%0A
+%09
     cell
@@ -554,16 +554,17 @@
 enter',%0A
+%09
     cell
@@ -588,16 +588,17 @@
 lector,%0A
+%09
     cont
@@ -604,24 +604,25 @@
 tain: true,%0A
+%09
     pageDots
@@ -626,24 +626,25 @@
 ots: false,%0A
+%09
     prevNext
@@ -659,16 +659,17 @@
  false,%0A
+%09
     wrap
@@ -682,16 +682,17 @@
 : true,%0A
+%09
 %09%09images
@@ -705,16 +705,17 @@
 : true,%0A
+%09
     perc
@@ -733,22 +733,24 @@
 : true,%0A
+%09
   %7D)%0A%0A
+%09
   docume
@@ -810,16 +810,17 @@
 ion() %7B%0A
+%09
     flkt
@@ -831,22 +831,24 @@
 stroy()%0A
+%09
   %7D)%0A%0A
+%09
 %09$total.
@@ -878,16 +878,17 @@
 ength)%0A%0A
+%09
 %09flkty.o
@@ -913,16 +913,17 @@
 ion() %7B%0A
+%09
 %09%09$curre
@@ -959,13 +959,15 @@
  1)%0A
+%09
 %09%7D)%0A%0A
+%09
   re
@@ -978,11 +978,14 @@
  flkty%0A%0A
+%09%7D%0A
 %0A%7D%0A

Check for exsiatnce of slideshow before inits