(function ( $ ) {
	'use strict';

	qodefCore.shortcodes.ginevra_core_instagram_list = {};

	$( document ).ready(
		function () {
			qodefInstagram.init();
		}
	);

	var qodefInstagram = {
		init: function () {
			this.holder = $( '.qodef-instagram-list #sb_instagram' );

            if ( this.holder.length ) {
                this.holder.each(
                    function () {

                        if ( $( this ).parent().hasClass( 'qodef-instagram-columns' ) ) {
                            var $imagesHolder  = $( this ).find( '#sbi_images' ),
                                $images        = $imagesHolder.find( '.sbi_item.sbi_type_image' ),
                                initialPadding = $imagesHolder.css( 'padding' );

                            // remove some unnecessary paddings
                            $imagesHolder.css('padding', '0');
                            $imagesHolder.css('margin', '-' + initialPadding);
                            $imagesHolder.css('width', 'calc(100% + ' + ( initialPadding) + ' + ' + ( initialPadding) + ')');

                            $images.attr('style', 'padding: ' + initialPadding + '!important');
                        } else if ( $( this ).parent().hasClass( 'qodef-instagram-slider' ) ) {
                            qodefInstagram.initSlider( $( this ) );
                        }
                    }
                );
            }
		},
        initSlider: function ( $currentItem, $initAllItems ) {

            var $imagesHolder  = $currentItem.find( '#sbi_images' ),
                $images        = $currentItem.find( '.sbi_item.sbi_type_image' ),
                initialPadding = $imagesHolder.css( 'padding' );

            // remove some unnecessary paddings
            $imagesHolder.css('padding', '0');
            $images.css('padding', '0');

            // items will inherit this margin
            $imagesHolder.attr('style', 'margin-right: ' + initialPadding + '!important');

            var sliderOptions = {};

            sliderOptions.spaceBetween      = parseInt( initialPadding );
            sliderOptions.customStages      = true;
            sliderOptions.slidesPerView     = $currentItem.data( 'cols' ) !== undefined && $currentItem.data( 'cols' ) !== '' ? $currentItem.data( 'cols' ) : 3;
            sliderOptions.slidesPerView1024 = $currentItem.data( 'cols' ) !== undefined && $currentItem.data( 'cols' ) !== '' ? $currentItem.data( 'cols' ) : 3;
            sliderOptions.slidesPerView680  = $currentItem.data( 'colstablet' ) !== undefined && $currentItem.data( 'colstablet' ) !== '' ? $currentItem.data( 'colstablet' ) : 2;
            sliderOptions.slidesPerView480  = $currentItem.data( 'colsmobile' ) !== undefined && $currentItem.data( 'colsmobile' ) !== '' ? $currentItem.data( 'colsmobile' ) : 1;

            $currentItem.attr( 'data-options', JSON.stringify(sliderOptions) );

            $imagesHolder.addClass( 'swiper-wrapper' );

            if ( $images.length ) {
                $images.each(
                    function () {
                        $( this ).addClass( 'qodef-e qodef-image-wrapper swiper-slide' );
                    }
                );
            }

            if ( typeof qodef.qodefSwiper === 'object' ) {

                if ( false === $initAllItems ) {
                    qodef.qodefSwiper.initSlider( $currentItem );
                } else {
                    qodef.qodefSwiper.init( $currentItem );
                }
            }
        },
	};

	qodefCore.shortcodes.ginevra_core_instagram_list.qodefInstagram = qodefInstagram;
	qodefCore.shortcodes.ginevra_core_instagram_list.qodefSwiper    = qodef.qodefSwiper;

})( jQuery );