/**
 * Slider next-preview thumbnail (demo-style).
 */
( function ( $ ) {
	'use strict';

	function updateNextPreview( $carousel ) {
		var $preview = $carousel.find( '.hbs-slider-next-preview' );
		var $img = $preview.find( '.hbs-slider-next-img' );
		if ( ! $preview.length || ! $img.length ) {
			return;
		}

		var $items = $carousel.find( '.carousel-item' );
		var $active = $items.filter( '.active' );
		var index = $items.index( $active );
		var nextIndex = ( index + 1 ) % $items.length;
		var nextThumb = $items.eq( nextIndex ).attr( 'data-thumb' );

		if ( nextThumb ) {
			$img.attr( 'src', nextThumb );
		}
	}

	$( function () {
		var $carousel = $( '#carouselExampleIndicators' );
		if ( ! $carousel.length ) {
			return;
		}

		$carousel.on( 'slid.bs.carousel', function () {
			updateNextPreview( $carousel );
		} );

		updateNextPreview( $carousel );
	} );
}( jQuery ) );
