(function($) {
  $(document).ready(function() {

    var slides = $('#homescroller .slide');
    var captions = $('#homescroller .caption');
    var parent = $(slides[0]).parent();

    parent.css({overflow: 'hidden', zIndex: 2});


    var SLIDE_COUNT = slides.length;
    var SLIDE_WIDTH = slides.width();

    slides.css({
      display: 'block', 
      position: 'absolute', 
      top: 0, 
      left: 0
    });

    slides.each(function(index) {
      $(this).css({left: index * SLIDE_WIDTH});
    });

    //position last slide to the left.
    $(slides[SLIDE_COUNT - 1]).css({left: -1 * SLIDE_WIDTH});


    captions.css({
      display: 'none',
      position:'absolute',
      left: 270,
      top: 386,
      'line-height': '40px',
      height: 40,
      background: '#BEBEBE',
      overflow: 'hidden',
      zIndex: 5
    });

    //show the first caption
    $(captions[0]).css({display: 'block'});

    function slide_left() {
      //prevent click while moving
      if(slides.queue().length) { return; }
      captions.css({display: 'none'});
      slides.animate({left: '-='+ SLIDE_WIDTH}, function() {
        slides.each(function(index) {
          var slide = $(this);
          var left_pos = parseInt(slide.css('left'), 10);
          //adjust the slide position if too far left
          if(left_pos < -1 * SLIDE_WIDTH) {
            slide.css({left: SLIDE_WIDTH * (SLIDE_COUNT - 2)});
          }
          //show the caption if this is the slide currently visible
          if(left_pos == 0) {
            $(captions[index]).css({display: 'block'});
          }
        });
      });
    }

    function slide_right() {
      //prevent click while moving
      if(slides.queue().length) { return; }
      captions.css({display: 'none'});
      slides.animate({left: '+='+ SLIDE_WIDTH}, function() {
        slides.each(function(index) {
          var slide = $(this);
          var left_pos = parseInt(slide.css('left'), 10);
          //adjust the slide position if too far left
          if(left_pos > SLIDE_WIDTH * (SLIDE_COUNT - 2)) {
            slide.css({left: -1 * SLIDE_WIDTH});
          }
          //show the caption if this is the slide currently visible
          if(left_pos == 0) {
            $(captions[index]).css({display: 'block'});
          }
        });
      });
    }

    var controls = $('#homescroller .controls img');
    controls.css({cursor: 'pointer'});
    var left = $(controls[0]);
    var right = $(controls[2]);
    var pause = $(controls[1]);

    var interval = setInterval(slide_left, 8000);

    pause.click(function() {
      if(interval) {
        clearInterval(interval);
        interval = null;
        pause.attr('src', '/wp-content/themes/metromont/images/homecontrols_pause_clicked.png');
        pause.css('cursor', 'auto');
      } else {
        //interval = setInterval(slide_left, 8000);
      }
    });

    left.click(slide_right);
    right.click(slide_left);
  
  });

})(jQuery);

