$$('.slideshow').each(function(slideshow){
  var slides = slideshow.getElement('.slides')
  var slideLabels = slideshow.getElements('.bullets li a');
  
	Slick.parse(slideshow.get('data-slideshow')).expressions.each(function(expression){
		slideshow.store('slideshow-' + expression[0].tag, expression[0].pseudos[0].key);
	});
  
  var ss = new SlideShow(slides,{
    delay: slideshow.retrieve('slideshow-delay'), 
    autoplay: true,
    onShow: function(param){
      var index = slides.getElements('li').indexOf(param.next.element)
      slideLabels.each(function(slide, i){
        if(i != index){
          slide.removeClass("selected")
        } else {
          slide.addClass("selected")
        }
      })
    }
  })


  function reverseTransition(transitionName){
    // turn left into rigth or right into left
    // turn top into bottom or bottom into top
    var swaps = {'Right': 'Left', 'Left': 'Right', 'Up': 'Down', 'Down': 'Up'}
    for( key in swaps){
      if (transitionName.indexOf(key) != -1) {
        transitionName = transitionName.replace(key, swaps[key])
        break;
      };
    }
    return transitionName
  }
  
  
  var step = undefined
  var duration = undefined
  var slideToShow = undefined
  slideLabels.each(function(el, index){
    el.store('slide', ss.slides[index]);

    el.addEvent('click', function(e){
      e.preventDefault()
      
      // which slide are we on ?
      var slideDisplayed = ss.current
      // which slide do we wanna show ?
      slideToShow = el.retrieve('slide')
      
      // do nothing if same slide
      if(slideDisplayed == slideToShow){
        return
      }

      // which direction ?
      var indexSlideDisplayed = ss.slides.indexOf(slideDisplayed)
      var indexSlideToShow = ss.slides.indexOf(slideToShow)

      // get a +1/-1 according to direction
      step =  (indexSlideToShow - indexSlideDisplayed) / Math.abs(indexSlideToShow-indexSlideDisplayed)
      
      // duration = current duration / nb of slides to pass
      duration =  500 / Math.abs(indexSlideToShow-indexSlideDisplayed)
      
      // play each slide transition with custom duration and transition
      var nextSlide = ss.slides[ss.slides.indexOf(ss.current) + step]
      var transition = nextSlide.retrieve('slideshow-transition')
      if (step < 0){
        transition = reverseTransition(transition)
      }
      ss.show(nextSlide, {'duration': duration, transition: transition})

      ss.pause()
    });
  });

  ss.addEvent('showComplete', function(slideData){
    // are we forcing the slides ?
    if(step != undefined){
      if(ss.current != slideToShow){
        var nextSlide = ss.slides[ss.slides.indexOf(ss.current) + step]
        var transition = nextSlide.retrieve('slideshow-transition')
        if (step < 0){
          transition = reverseTransition(transition)
        }
        ss.show(nextSlide, {'duration': duration, transition: transition})
      } else {
        step = undefined
        duration = undefined
        slideToShow = undefined
      }
    }
  })
  

})


