Guest User

Untitled

a guest
May 10th, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ======================================================================================================================================================
  2. Avia Slideshow
  3. ====================================================================================================================================================== */
  4.  
  5. (function($)
  6. {
  7.     "use strict";
  8.  
  9.     $.AviaSlider = function(options, slider)
  10.     {
  11.         var self = this;
  12.  
  13.         this.$win    = $( window );
  14.  
  15.         this.$slider = $( slider );
  16.  
  17.         this.isMobile = $.avia_utilities.isMobile;
  18.  
  19.         this._prepareSlides(options);
  20.  
  21.         //default preload images then init slideshow
  22.         $.avia_utilities.preload({container: this.$slider, single_callback: function(){ self._init( options ); }});
  23.     };
  24.  
  25.     $.AviaSlider.defaults = {
  26.  
  27.  
  28.         //interval between autorotation switches
  29.         interval:5,
  30.  
  31.         //autorotation active or not
  32.         autoplay:false,
  33.  
  34.         //set if the loop will stop at the last/first slide or if the slides will loop infinite
  35.         //set to false for infinite loop, "last" to stop at the last slide or "first" to stop at the first slide
  36.         stopinfiniteloop: false,
  37.  
  38.         //fade or slide animation
  39.         animation:'slide',
  40.  
  41.         //transition speed when switching slide
  42.         transitionSpeed:900,
  43.  
  44.         //easing method for the transition
  45.         easing:'easeInOutQuart',
  46.  
  47.         //slide wrapper
  48.         wrapElement: '>ul',
  49.  
  50.         //slide element
  51.         slideElement: '>li',
  52.  
  53.         //pause if mouse cursor is above item
  54.         hoverpause: false,
  55.  
  56.         //attach images as background
  57.         bg_slider: false,
  58.  
  59.         //delay of milliseconds to wait before showing the next slide
  60.         show_slide_delay: 0,
  61.  
  62.         //if slider animation is set to "fade" the fullfade property sets the crossfade behaviour
  63.         fullfade: false,
  64.  
  65.         //enable carousel mode with multiple visible slides
  66.         carousel: 'no',
  67.  
  68.         // how many slides are displayed at once in the carousel
  69.         carouselSlidesToShow: 3,
  70.  
  71.         // TODO: how many slides are scrolled in the carousel
  72.         carouselSlidesToScroll: 1,
  73.  
  74.         // responsive carousel
  75.         carouselResponsive : new Array()
  76.  
  77.     };
  78.  
  79.     $.AviaSlider.prototype =
  80.     {
  81.         _init: function( options )
  82.         {
  83.             // set slider options
  84.             this.options = this._setOptions(options);
  85.  
  86.             //slidewrap
  87.             this.$sliderUl  = this.$slider.find(this.options.wrapElement);
  88.  
  89.             // slide elements
  90.             this.$slides = this.$sliderUl.find(this.options.slideElement);
  91.  
  92.             // goto dots
  93.             this.gotoButtons = this.$slider.find('.avia-slideshow-dots a');
  94.  
  95.             //perma caption
  96.             this.permaCaption =  this.$slider.find('>.av-slideshow-caption');
  97.  
  98.             // slide count
  99.             this.itemsCount = this.$slides.length;
  100.  
  101.             // current image index
  102.             this.current = 0;
  103.  
  104.             // current carousel index
  105.             this.currentCarousel = 0;
  106.  
  107.             // carousel slide width
  108.             this.slideWidthCarousel = '240';
  109.  
  110.             //loop count
  111.             this.loopCount = 0;
  112.  
  113.             // control if the slicebox is animating
  114.             this.isAnimating = false;
  115.  
  116.             // css browser prefix like -webkit-, -moz-
  117.             this.browserPrefix = $.avia_utilities.supports('transition');
  118.  
  119.             // css3 animation?
  120.             this.cssActive = this.browserPrefix !== false ? true : false;
  121.  
  122.             // css3D animation?
  123.             this.css3DActive = document.documentElement.className.indexOf('avia_transform3d') !== -1 ? true : false;
  124.  
  125.             //if we have a bg slider no images were preloaded yet. in that case start preloading and attaching images
  126.             if(this.options.bg_slider == true)
  127.             {
  128.                 //create array that holds all image urls to preload
  129.                 this.imageUrls = [];
  130.  
  131.                 //create a preloader icon to indicate loading
  132.                 this.loader = $.avia_utilities.loading(this.$slider);
  133.  
  134.                 //preload the images ony by one
  135.                 this._bgPreloadImages();
  136.  
  137.             }
  138.             else //if it was a default slider all images are already loaded and we can start showing the slider
  139.             {
  140.                 //kickoff the slider: bind functions, show first slide, if active start the autorotation timer
  141.                 this._kickOff();
  142.             }
  143.  
  144.             if(this.options.carousel === 'yes')
  145.             {
  146.                 this.options.animation = 'carouselslide';
  147.             }
  148.         },
  149.  
  150.         //set the slider options by first merging the default options and the passed options, then checking the slider element if any data attributes overwrite the option set
  151.         _setOptions: function(options)
  152.         {
  153.             var newOptions  = $.extend( true, {}, $.AviaSlider.defaults, options ),
  154.                 htmlData    = this.$slider.data();
  155.  
  156.             //overwrite passed option set with any data properties on the html element
  157.             for( var i in htmlData )
  158.             {
  159.                 //  data attribute is transformed to lower case, but js is case sensitive - transform key
  160.                 var key = ( 'transition_speed' != i ) ? i :'transitionSpeed';
  161.  
  162.                 if( typeof htmlData[ i ] === "string" || typeof htmlData[ i ] === "number" || typeof htmlData[ i ] === "boolean" )
  163.                 {
  164.                     newOptions[ key ] = htmlData[ i ];
  165.                 }
  166.  
  167.             }
  168.  
  169.             return newOptions;
  170.         },
  171.  
  172.         _prepareSlides: function(options)
  173.         {
  174.             //if its a mobile device find all video slides that need to be altered
  175.             if(this.isMobile)
  176.             {
  177.                 var alter = this.$slider.find('.av-mobile-fallback-image');
  178.                 alter.each(function()
  179.                 {
  180.                     var current  = $(this).removeClass('av-video-slide').data({'avia_video_events': true, 'video-ratio':0}),
  181.                         fallback = current.data('mobile-img'),
  182.                         fallback_link = current.data('fallback-link'),
  183.                         appendTo = current.find('.avia-slide-wrap');
  184.  
  185.                     current.find('.av-click-overlay, .mejs-mediaelement, .mejs-container').remove();
  186.  
  187.                     if(!fallback)
  188.                     {
  189.                         $('<p class="av-fallback-message"><span>Please set a mobile device fallback image for this video in your wordpress backend</span></p>').appendTo(appendTo);
  190.                     }
  191.  
  192.                     if(options && options.bg_slider)
  193.                     {
  194.                         current.data('img-url', fallback);
  195.  
  196.                         //if we got a fallback link we need to either replace the default link on mobile devices, or if there is no default link change the wrapping <div> to an <a>
  197.                         if(fallback_link != "")
  198.                         {
  199.                             if(appendTo.is('a'))
  200.                             {
  201.                                 appendTo.attr('href', fallback_link);
  202.                             }
  203.                             else
  204.                             {
  205.                                 appendTo.find('a').remove();
  206.                                 appendTo.replaceWith(function(){
  207.                                     var cur_slide = $(this);
  208.                                     return $("<a>").attr({'data-rel': cur_slide.data('rel'), 'class': cur_slide.attr('class'), 'href': fallback_link} ).append( $(this).contents() );
  209.                                 });
  210.  
  211.                                 appendTo = current.find('.avia-slide-wrap');
  212.                             }
  213.  
  214.                             if($.fn.avia_activate_lightbox)
  215.                             {
  216.                                 current.parents('#main').avia_activate_lightbox();
  217.                             }
  218.                         }
  219.                     }
  220.                     else
  221.                     {
  222.                         var image = '<img src="'+fallback+'" alt="" title="" />';
  223.                         var lightbox = false;
  224.  
  225.                         if( 'string' == typeof fallback_link && fallback_link.trim() != '' )
  226.                         {
  227.                             if( appendTo.is('a') )
  228.                             {
  229.                                 appendTo.attr('href', fallback_link);
  230.                             }
  231.                             else
  232.                             {
  233.                                 var rel = fallback_link.match(/\.(jpg|jpeg|gif|png)$/i) != null ? ' rel="lightbox" ' : '';
  234.                                 image = '<a href="' + fallback_link.trim() + '"' + rel + '>' + image + '</a>';
  235.                             }
  236.                             lightbox = true;
  237.                         }
  238.  
  239.                         current.find('.avia-slide-wrap').append(image);
  240.  
  241.                         if( lightbox && $.fn.avia_activate_lightbox)
  242.                         {
  243.                             current.parents('#main').avia_activate_lightbox();
  244.                         }
  245.                     }
  246.  
  247.                 });
  248.             }
  249.  
  250.         },
  251.  
  252.         //start preloading the background images
  253.         _bgPreloadImages : function(callback)
  254.         {
  255.             this._getImageURLS();
  256.  
  257.             this._preloadSingle(0, function()
  258.             {
  259.                 this._kickOff();
  260.                 this._preloadNext(1);
  261.             });
  262.         },
  263.  
  264.         //if we are using a background image slider, fetch the images from a data attribute and preload them one by one
  265.         _getImageURLS: function()
  266.         {
  267.             var _self = this;
  268.  
  269.             //collect url strings of the images to preload
  270.             this.$slides.each(function(i)
  271.             {
  272.                 _self.imageUrls[i] = [];
  273.                 _self.imageUrls[i]['url'] = $(this).data("img-url");
  274.  
  275.                 //if no image is passed we can set the slide to loaded
  276.                 if(typeof _self.imageUrls[i]['url'] == 'string')
  277.                 {
  278.                     _self.imageUrls[i]['status'] = false;
  279.                 }
  280.                 else
  281.                 {
  282.                     _self.imageUrls[i]['status'] = true;
  283.                 }
  284.             });
  285.         },
  286.  
  287.  
  288.         _preloadSingle: function(key, callback)
  289.         {
  290.             var _self       = this,
  291.                 objImage    = new Image();
  292.  
  293.             if(typeof _self.imageUrls[key]['url'] == 'string')
  294.             {
  295.                 $(objImage).on('load error', function()
  296.                 {
  297.                     _self.imageUrls[key]['status'] = true;
  298.                     _self.$slides.eq(key).css('background-image','url(' + _self.imageUrls[key]['url'] + ')');
  299.                     if(typeof callback == 'function') callback.apply( _self, [objImage, key] );
  300.                 });
  301.  
  302.                 if(_self.imageUrls[key]['url'] != "")
  303.                 {
  304.                     objImage.src = _self.imageUrls[key]['url'];
  305.                 }
  306.                 else
  307.                 {
  308.                     $(objImage).trigger('error');
  309.                 }
  310.             }
  311.             else
  312.             {
  313.                 if(typeof callback == 'function') callback.apply( _self, [objImage, key] );
  314.             }
  315.         },
  316.  
  317.         _preloadNext: function(key)
  318.         {
  319.             if(typeof this.imageUrls[key] != "undefined")
  320.             {
  321.                 this._preloadSingle(key, function()
  322.                 {
  323.                     this._preloadNext(key + 1);
  324.                 });
  325.             }
  326.         },
  327.  
  328.  
  329.         //bind click events of slide controlls to the public functions
  330.         _bindEvents: function()
  331.         {
  332.             var self = this,
  333.                 win  = $( window );
  334.  
  335.             this.$slider.on('click','.next-slide', $.proxy( this.next, this) );
  336.             this.$slider.on('click','.prev-slide', $.proxy( this.previous, this) );
  337.             this.$slider.on('click','.goto-slide', $.proxy( this.go2, this) );
  338.  
  339.             if(this.options.hoverpause)
  340.             {
  341.                 this.$slider.on('mouseenter', $.proxy( this.pause, this) );
  342.                 this.$slider.on('mouseleave', $.proxy( this.resume, this) );
  343.             }
  344.  
  345.             if(this.options.stopinfiniteloop && this.options.autoplay)
  346.             {
  347.                 if(this.options.stopinfiniteloop == 'last')
  348.                 {
  349.                     this.$slider.on('avia_slider_last_slide', $.proxy(this._stopSlideshow, this) );
  350.                 }
  351.                 else if(this.options.stopinfiniteloop == 'first')
  352.                 {
  353.                     this.$slider.on('avia_slider_first_slide', $.proxy(this._stopSlideshow, this) );
  354.                 }
  355.             }
  356.  
  357.             if (this.options.carousel === 'yes'){
  358.                 // recalculate carousel dimensions on viewport size change
  359.                 // use on desktop only, debouncedresize fires on scroll on mobile
  360.                 if (!this.isMobile)
  361.                 {
  362.                     win.on( 'debouncedresize',  $.proxy( this._buildCarousel, this) );
  363.                 }
  364.             }
  365.             else{
  366.                 win.on( 'debouncedresize.aviaSlider',  $.proxy( this._setSize, this) );
  367.             }
  368.  
  369.  
  370.             //if its a desktop browser add arrow navigation, otherwise add touch nav
  371.             if(!this.isMobile)
  372.             {
  373.                 this.$slider.avia_keyboard_controls();
  374.             }
  375.             else
  376.             {
  377.                 this.$slider.avia_swipe_trigger();
  378.             }
  379.  
  380.             self._attach_video_events();
  381.         },
  382.  
  383.         //kickoff the slider by binding all functions to slides and buttons, show the first slide and start autoplay
  384.         _kickOff: function()
  385.         {
  386.             var self            = this,
  387.                 first_slide     = self.$slides.eq(0),
  388.                 video           = first_slide.data('video-ratio');
  389.  
  390.             // bind events to to the controll buttons
  391.             self._bindEvents();
  392.  
  393.             this.$slider.removeClass('av-default-height-applied');
  394.  
  395.             //show the first slide. if its a video set the correct size, otherwise make sure to remove the % padding
  396.             if(video)
  397.             {
  398.                 self._setSize(true);
  399.             }
  400.             else
  401.             {
  402.                 if(this.options.keep_pading != true)
  403.                 {
  404.                     self.$sliderUl.css('padding',0);
  405.                     self.$win.trigger('av-height-change');
  406.                 }
  407.             }
  408.  
  409.             self._setCenter();
  410.             if ( this.options.carousel === 'no' ) {
  411.                 first_slide.css({visibility:'visible', opacity:0}).avia_animate({opacity:1}, function()
  412.                 {
  413.                     var current = $(this).addClass('active-slide');
  414.  
  415.                     if(self.permaCaption.length)
  416.                     {
  417.                         self._setCaptionPointer();
  418.                        
  419.                         self.permaCaption.addClass('active-slide');
  420.  
  421.                         self.permaCaption.on("click", function(e) {
  422.                             var active_slide_link = $(this).siblings('.avia-slideshow-inner').find('>.active-slide a');
  423.  
  424.                             if(active_slide_link.length) {
  425.                                 window.location.href = active_slide_link.attr("href");
  426.                             }
  427.                         });
  428.                     }
  429.                 });
  430.             }
  431.  
  432.  
  433.             // start autoplay if active
  434.             if( self.options.autoplay )
  435.             {
  436.                 self._startSlideshow();
  437.             }
  438.  
  439.             // prepare carousel if active
  440.             if (self.options.carousel === 'yes') {
  441.                 self._buildCarousel();
  442.             }
  443.  
  444.             self.$slider.trigger('_kickOff');
  445.  
  446.         },
  447.  
  448.         _buildCarousel : function(){
  449.  
  450.             var self            = this,
  451.             stageWidth          = this.$slider.outerWidth(),
  452.             slidesWidth         = parseInt(stageWidth / this.options.carouselSlidesToShow),
  453.             windowWidth = window.innerWidth || $(window).width();
  454.  
  455.  
  456.             // responsive carousel
  457.             if ( this.options.carouselResponsive &&
  458.                 this.options.carouselResponsive.length &&
  459.                 this.options.carouselResponsive !== null) {
  460.  
  461.                 for (var breakpoint in this.options.carouselResponsive){
  462.                     var breakpointValue = this.options.carouselResponsive[breakpoint]['breakpoint'];
  463.                     var newSlidesToShow = this.options.carouselResponsive[breakpoint]['settings']['carouselSlidesToShow'];
  464.  
  465.                     if (breakpointValue >= windowWidth) {
  466.                         slidesWidth = parseInt(stageWidth / newSlidesToShow);
  467.                         this.options.carouselSlidesToShow = newSlidesToShow;
  468.                     }
  469.                 }
  470.             }
  471.  
  472.             // set width and height for each slide
  473.             this.slideWidthCarousel = slidesWidth;
  474.  
  475.             this.$slides.each(function(i){
  476.                 $(this).width(slidesWidth);
  477.             });
  478.  
  479.             // set width for the UL
  480.             var slideTrackWidth = slidesWidth * this.itemsCount;
  481.             this.$sliderUl.width(slideTrackWidth).css('transform', 'translateX(0px)');
  482.  
  483.             // hide nav if not needed
  484.             if (this.options.carouselSlidesToShow >= this.itemsCount){
  485.                 this.$slider.find('.av-timeline-nav').hide();
  486.             }
  487.  
  488.         },
  489.  
  490.         //calculate which slide should be displayed next and call the executing transition function
  491.         _navigate : function( dir, pos ) {
  492.  
  493.             if( this.isAnimating || this.itemsCount < 2 || !this.$slider.is(":visible") )
  494.             {
  495.                 return false;
  496.             }
  497.  
  498.             this.isAnimating = true;
  499.  
  500.             // current item's index
  501.             this.prev = this.current;
  502.  
  503.             // if position is passed
  504.             if( pos !== undefined )
  505.             {
  506.                 this.current = pos;
  507.                 dir = this.current > this.prev ? 'next' : 'prev';
  508.             }
  509.  
  510.             // if not check the boundaries
  511.             else if( dir === 'next' )
  512.             {
  513.                 this.current = this.current < this.itemsCount - 1 ? this.current + 1 : 0;
  514.  
  515.                 if( this.current === 0 && this.options.autoplay_stopper == 1 && this.options.autoplay )
  516.                 {
  517.                     this.isAnimating = false;
  518.                     this.current = this.prev;
  519.                     this._stopSlideshow();
  520.                     return false;
  521.                 }
  522.             }
  523.             else if( dir === 'prev' )
  524.             {
  525.                 this.current = this.current > 0 ? this.current - 1 : this.itemsCount - 1;
  526.             }
  527.  
  528.             //set goto button
  529.             this.gotoButtons.removeClass('active').eq(this.current).addClass('active');
  530.  
  531.             //set slideshow size if carousel not in use
  532.             if( this.options.carousel === 'no') {
  533.                 this._setSize();
  534.             }
  535.  
  536.             //if we are using a background slider make sure that the image is loaded. if not preload it, then show the slide
  537.             if(this.options.bg_slider == true)
  538.             {
  539.                 if(this.imageUrls[this.current]['status'] == true )
  540.                 {
  541.                     this['_' + this.options.animation].call(this, dir);
  542.                 }
  543.                 else
  544.                 {
  545.                     this.loader.show();
  546.                     this._preloadSingle(this.current, function()
  547.                     {
  548.                         this['_' + this.options.animation].call(this, dir);
  549.                         this.loader.hide();
  550.                     });
  551.                 }
  552.             }
  553.             else //no background loader -> images are already loaded
  554.             {
  555.                 //call the executing function. for example _slide, or _fade. since the function call is absed on a var we can easily extend the slider with new animations
  556.                 this['_' + this.options.animation].call(this, dir);
  557.             }
  558.  
  559.             if(this.current == 0)
  560.             {
  561.                 this.loopCount++;
  562.                 this.$slider.trigger('avia_slider_first_slide');
  563.             }
  564.             else if(this.current == this.itemsCount - 1)
  565.             {
  566.                 this.$slider.trigger('avia_slider_last_slide');
  567.             }
  568.             else
  569.             {
  570.                 this.$slider.trigger('avia_slider_navigate_slide');
  571.             }
  572.  
  573.             this._setCaptionPointer();
  574.         },
  575.  
  576.         _setCaptionPointer: function()
  577.         {
  578.             if( ! this.permaCaption.length ) return;
  579.  
  580.             var with_link = $(this.$slides[this.current]).find("a").length;
  581.             var cursor = with_link ? "pointer" : "default";
  582.  
  583.             this.permaCaption.css("cursor", cursor);
  584.         },
  585.  
  586.         //if the next slide has a different height than the current change the slideshow height
  587.         _setSize: function(instant)
  588.         {
  589.             //if images are attached as bg images the slider has a fixed height
  590.             if(this.options.bg_slider == true) return;
  591.  
  592.             var self            = this,
  593.                 slide           = this.$slides.eq(this.current),
  594.                 img             = slide.find('img'),
  595.                 current         = Math.floor(this.$sliderUl.height()),
  596.                 ratio           = slide.data('video-ratio'),
  597.                 setTo           = ratio ? this.$sliderUl.width() / ratio : Math.floor(slide.height()),
  598.                 video_height    = slide.data('video-height'), //forced video height %. needs to be set only once
  599.                 video_toppos    = slide.data('video-toppos'); //forced video top position
  600.  
  601.                 this.$sliderUl.height(current).css('padding',0); //make sure to set the slideheight to an actual value
  602.  
  603.                 if(setTo != current)
  604.                 {
  605.                     if(instant == true)
  606.                     {
  607.                         this.$sliderUl.css({height:setTo});
  608.                         this.$win.trigger('av-height-change');
  609.                     }
  610.                     else
  611.                     {
  612.                         this.$sliderUl.avia_animate({height:setTo}, function()
  613.                         {
  614.                             self.$win.trigger('av-height-change');
  615.                         });
  616.                     }
  617.                 }
  618.  
  619.                 this._setCenter();
  620.  
  621.                 if(video_height && video_height!= "set")
  622.                 {
  623.                     slide.find('iframe, embed, video, object, .av_youtube_frame').css({height: video_height + '%', top: video_toppos + '%'});
  624.                     slide.data('video-height','set');
  625.                 }
  626.         },
  627.  
  628.         _setCenter: function()
  629.         {
  630.             // if the image has a min width and is larger than the slider center it
  631.             // positon img based on caption. right caption->left pos, left caption -> right pos
  632.             var slide       = this.$slides.eq(this.current),
  633.                 img         = slide.find('img'),
  634.                 min_width   = parseInt(img.css('min-width'),10),
  635.                 slide_width = slide.width(),
  636.                 caption     = slide.find('.av-slideshow-caption'),
  637.                 css_left    = ((slide_width - min_width) / 2);
  638.  
  639.             if(caption.length)
  640.             {
  641.                 if(caption.is('.caption_left'))
  642.                 {
  643.                     css_left = ((slide_width - min_width) / 1.5);
  644.                 }
  645.                 else if(caption.is('.caption_right'))
  646.                 {
  647.                     css_left = ((slide_width - min_width) / 2.5);
  648.                 }
  649.             }
  650.  
  651.             if(slide_width >= min_width)
  652.             {
  653.                 css_left = 0;
  654.             }
  655.  
  656.             img.css({left:css_left});
  657.         },
  658.  
  659.         _carouselmove : function(){
  660.  
  661.         //    var offset = (this.options.carouselSlidesToScroll*this.slideWidthCarousel)*this.currentCarousel;
  662.             var offset = this.slideWidthCarousel*this.currentCarousel;
  663.             this.$sliderUl.css('transform', 'translateX(-'+offset+'px)');
  664.  
  665.         },
  666.  
  667.         _carouselslide: function(dir){
  668.  
  669.             if (dir === 'next') {
  670.  
  671.                 if (this.options.carouselSlidesToShow + this.currentCarousel < this.itemsCount){
  672.                     this.currentCarousel++;
  673.                     this._carouselmove();
  674.                 }
  675.             }
  676.             else if (dir === 'prev'){
  677.                 if (this.currentCarousel > 0) {
  678.                     this.currentCarousel--;
  679.                     this._carouselmove();
  680.                 }
  681.             }
  682.  
  683.             this.isAnimating = false;
  684.  
  685.         },
  686.  
  687.         _slide: function(dir)
  688.         {
  689.             var dynamic         = false, //todo: pass by option if a slider is dynamic
  690.                 modifier        = dynamic == true ? 2 : 1,
  691.                 sliderWidth     = this.$slider.width(),
  692.                 direction       = dir === 'next' ? -1 : 1,
  693.                 property        = this.browserPrefix + 'transform',
  694.                 reset           = {}, transition = {},  transition2 = {},
  695.                 trans_val       = ( sliderWidth * direction * -1),
  696.                 trans_val2      = ( sliderWidth * direction) / modifier;
  697.  
  698.             //do a css3 animation
  699.             if(this.cssActive)
  700.             {
  701.                 property  = this.browserPrefix + 'transform';
  702.  
  703.                 //do a translate 3d transformation if available, since it uses hardware acceleration
  704.                 if(this.css3DActive)
  705.                 {
  706.                     reset[property]  = "translate3d(" + trans_val + "px, 0, 0)";
  707.                     transition[property]  = "translate3d(" + trans_val2 + "px, 0, 0)";
  708.                     transition2[property] = "translate3d(0,0,0)";
  709.                 }
  710.                 else //do a 2d transform. still faster than a position "left" change
  711.                 {
  712.                     reset[property]  = "translate(" + trans_val + "px,0)";
  713.                     transition[property]  = "translate(" + trans_val2 + "px,0)";
  714.                     transition2[property] = "translate(0,0)";
  715.                 }
  716.             }
  717.             else
  718.             {
  719.                 reset.left = trans_val;
  720.                 transition.left = trans_val2;
  721.                 transition2.left = 0;
  722.             }
  723.  
  724.             if(dynamic)
  725.             {
  726.                 transition['z-index']  = "1";
  727.                 transition2['z-index']  = "2";
  728.             }
  729.  
  730.             this._slide_animate(reset, transition, transition2);
  731.         },
  732.  
  733.         _slide_up: function(dir)
  734.         {
  735.             var dynamic         = true, //todo: pass by option if a slider is dynamic
  736.                 modifier        = dynamic == true ? 2 : 1,
  737.                 sliderHeight    = this.$slider.height(),
  738.                 direction       = dir === 'next' ? -1 : 1,
  739.                 property        = this.browserPrefix + 'transform',
  740.                 reset           = {}, transition = {},  transition2 = {},
  741.                 trans_val       = ( sliderHeight * direction * -1),
  742.                 trans_val2      = ( sliderHeight * direction) / modifier;
  743.  
  744.             //do a css3 animation
  745.             if(this.cssActive)
  746.             {
  747.                 property  = this.browserPrefix + 'transform';
  748.  
  749.                 //do a translate 3d transformation if available, since it uses hardware acceleration
  750.                 if(this.css3DActive)
  751.                 {
  752.                     reset[property]  = "translate3d( 0," + trans_val + "px, 0)";
  753.                     transition[property]  = "translate3d( 0," + trans_val2 + "px, 0)";
  754.                     transition2[property] = "translate3d(0,0,0)";
  755.                 }
  756.                 else //do a 2d transform. still faster than a position "left" change
  757.                 {
  758.                     reset[property]  = "translate( 0," + trans_val + "px)";
  759.                     transition[property]  = "translate( 0," + trans_val2 + "px)";
  760.                     transition2[property] = "translate(0,0)";                   }
  761.             }
  762.             else
  763.             {
  764.                 reset.top = trans_val;
  765.                 transition.top = trans_val2;
  766.                 transition2.top = 0;
  767.             }
  768.  
  769.             if(dynamic)
  770.             {
  771.                 transition['z-index']  = "1";
  772.                 transition2['z-index']  = "2";
  773.             }
  774.             this._slide_animate(reset, transition, transition2);
  775.         },
  776.  
  777.  
  778.         //slide animation: do a slide transition by css3 transform if possible. if not simply do a position left transition
  779.         _slide_animate: function( reset , transition , transition2 )
  780.         {
  781.  
  782.             var self            = this,
  783.                 displaySlide    = this.$slides.eq(this.current),
  784.                 hideSlide       = this.$slides.eq(this.prev);
  785.  
  786.                 hideSlide.trigger('pause');
  787.                 if( !displaySlide.data('disableAutoplay') ) {
  788.  
  789.                     if(displaySlide.hasClass('av-video-lazyload') && !displaySlide.hasClass('av-video-lazyload-complete'))
  790.                     {
  791.                         displaySlide.find('.av-click-to-play-overlay').trigger('click');
  792.                     }
  793.                     else
  794.                     {
  795.                         displaySlide.trigger('play');
  796.                     }
  797.                 }
  798.  
  799.                 displaySlide.css({visibility:'visible', zIndex:4, opacity:1, left:0, top:0});
  800.                 displaySlide.css(reset);
  801.  
  802.                 hideSlide.avia_animate(transition, this.options.transitionSpeed, this.options.easing);
  803.  
  804.                 var after_slide = function()
  805.                 {
  806.                     self.isAnimating = false;
  807.                     displaySlide.addClass('active-slide');
  808.                     hideSlide.css({visibility:'hidden'}).removeClass('active-slide');
  809.                     self.$slider.trigger('avia-transition-done');
  810.                 };
  811.  
  812.                 if(self.options.show_slide_delay > 0)
  813.                 {
  814.                     setTimeout(function() { displaySlide.avia_animate(transition2, self.options.transitionSpeed, self.options.easing, after_slide); },self.options.show_slide_delay);
  815.                 }
  816.                 else
  817.                 {
  818.                     displaySlide.avia_animate(transition2, self.options.transitionSpeed, self.options.easing, after_slide);
  819.                 }
  820.  
  821.         },
  822.  
  823.         //simple fade transition of the slideshow
  824.         _fade: function()
  825.         {
  826.             var self            = this,
  827.                 displaySlide    = this.$slides.eq(this.current),
  828.                 hideSlide       = this.$slides.eq(this.prev),
  829.                 properties      = {visibility:'visible', zIndex:3, opacity:0},
  830.                 fadeCallback    = function()
  831.                 {
  832.                     self.isAnimating = false;
  833.                     displaySlide.addClass('active-slide');
  834.                     hideSlide.css({visibility:'hidden', zIndex:2}).removeClass('active-slide');
  835.                     self.$slider.trigger('avia-transition-done');
  836.  
  837.                 };
  838.  
  839.             hideSlide.trigger('pause');
  840.             if( !displaySlide.data('disableAutoplay') ) {
  841.  
  842.                 if(displaySlide.hasClass('av-video-lazyload') && !displaySlide.hasClass('av-video-lazyload-complete'))
  843.                 {
  844.                     displaySlide.find('.av-click-to-play-overlay').trigger('click');
  845.                 }
  846.                 else
  847.                 {
  848.                     displaySlide.trigger('play');
  849.                 }
  850.             }
  851.  
  852.             if(self.options.fullfade == true)
  853.             {
  854.                 hideSlide.avia_animate({opacity:0}, 200, 'linear', function()
  855.                 {
  856.                     displaySlide.css(properties).avia_animate({opacity:1}, self.options.transitionSpeed, 'linear',fadeCallback);
  857.                 });
  858.             }
  859.             else
  860.             {
  861.                 if (self.current === 0) {
  862.                     hideSlide.avia_animate({opacity:0}, self.options.transitionSpeed/2, 'linear');
  863.                     displaySlide.css(properties).avia_animate({opacity:1}, self.options.transitionSpeed/2, 'linear',fadeCallback);
  864.                 }
  865.                 else{
  866.                     displaySlide.css(properties).avia_animate({opacity:1}, self.options.transitionSpeed/2, 'linear', function()
  867.                     {
  868.                         hideSlide.avia_animate({opacity:0}, 200, 'linear', fadeCallback);
  869.                     });
  870.                 }
  871.  
  872.             }
  873.  
  874.         },
  875.  
  876.  
  877.         /************************************************************************
  878.         Video functions
  879.         *************************************************************************/
  880.  
  881.         //bind events to the video that tell the slider to autorotate once a video has been played
  882.         _attach_video_events: function()
  883.         {
  884.             var self = this, $html = $('html');
  885.  
  886.             self.$slides.each(function(i)
  887.             {
  888.                 var currentSlide    = $(this),
  889.                     caption         = currentSlide.find('.caption_fullwidth, .av-click-overlay'),
  890.                     mejs            = currentSlide.find('.mejs-mediaelement'),
  891.                     lazyload        = currentSlide.hasClass('av-video-lazyload') ? true : false;
  892.  
  893.  
  894.                 if(currentSlide.data('avia_video_events') != true)
  895.                 {
  896.                     currentSlide.data('avia_video_events', true);
  897.  
  898.                     currentSlide.on('av-video-events-bound', { slide: currentSlide, wrap: mejs , iteration: i , self: self, lazyload: lazyload }, onReady);
  899.  
  900.                     currentSlide.on('av-video-ended', { slide: currentSlide , self: self}, onFinish);
  901.  
  902.                     currentSlide.on('av-video-play-executed', function(){ setTimeout( function(){  self.pause(); }, 100 ); } );
  903.  
  904.                     caption.on('click', { slide: currentSlide }, toggle);
  905.  
  906.                     // also if the player was loaded before the _bindEvents function was bound trigger it manually
  907.                     if(currentSlide.is('.av-video-events-bound')) currentSlide.trigger('av-video-events-bound');
  908.  
  909.                     //if we are on the first slide and autoplay is enabled and lazy loading is enabled we need to simulate a click event to the lazy load container
  910.                     if(lazyload && i === 0 && !currentSlide.data('disableAutoplay'))
  911.                     {
  912.                         currentSlide.find('.av-click-to-play-overlay').trigger('click');
  913.                     }
  914.                 }
  915.             });
  916.  
  917.  
  918.             //function that takes care of events once the video is loaded for the first time.
  919.             //needs to take into account 2 different scenarios: normally embedded videos or lazyloaded videos that start on user interaction/autoplay
  920.             function onReady( event )
  921.             {
  922.                 //autostart for first slide
  923.                 if(event.data.iteration === 0)
  924.                 {
  925.                     event.data.wrap.css('opacity',0);
  926.                     if(!event.data.self.isMobile && !event.data.slide.data('disableAutoplay'))
  927.                     {
  928.                         event.data.slide.trigger('play');
  929.                     } 
  930.                     setTimeout(function(){ event.data.wrap.avia_animate({opacity:1}, 400); }, 50);
  931.                 }
  932.                 else if ($html.is('.avia-msie') && !event.data.slide.is('.av-video-service-html5'))
  933.                 {
  934.                     /*
  935.                     * Internet Explorer fires the ready event for external videos once they become visible
  936.                     * as oposed to other browsers which always fire immediately.
  937.                     */
  938.                     if( !event.data.slide.data('disableAutoplay') ) event.data.slide.trigger('play');
  939.                 }
  940.  
  941.  
  942.                 //make sure that the html5 element does not play if autoply is enabled but its not the first slide.
  943.                 //the autoplay attribute on the video element might cause this
  944.                 if(event.data.slide.is('.av-video-service-html5') && event.data.iteration !== 0 )
  945.                 {
  946.                     event.data.slide.trigger('pause');
  947.                 }
  948.  
  949.                 //make sure that lazyloaded videos always get started once a user clicks them
  950.                 if(event.data.lazyload)
  951.                 {
  952.                     event.data.slide.addClass('av-video-lazyload-complete');
  953.                     event.data.slide.trigger('play');
  954.                 }
  955.             }
  956.  
  957.  
  958.  
  959.  
  960.             function onFinish( event )
  961.             {
  962.                 //if the video is not looped resume the slideshow
  963.                 if(!event.data.slide.is('.av-single-slide') && !event.data.slide.is('.av-loop-video'))
  964.                 {
  965.                     event.data.slide.trigger('reset');
  966.                     self._navigate( 'next' );
  967.                     self.resume();
  968.                 }
  969.  
  970.                 //safari 8 workaround for self hosted videos which wont loop by default
  971.                 if(event.data.slide.is('.av-loop-video') && event.data.slide.is('.av-video-service-html5'))
  972.                 {
  973.                     if($html.is('.avia-safari-8'))
  974.                     {
  975.                         setTimeout(function(){ event.data.slide.trigger('play'); },1);
  976.                     }
  977.                 }
  978.             }
  979.  
  980.             function toggle( event )
  981.             {
  982.                 if(event.target.tagName != "A")
  983.                 {
  984.                     event.data.slide.trigger('toggle');
  985.                 }
  986.             }
  987.  
  988.         },
  989.  
  990.  
  991.  
  992.         /************************************************************************
  993.         Slideshow control functions
  994.         *************************************************************************/
  995.  
  996.         _timer: function(callback, delay, first)
  997.         {
  998.             var self = this, start, remaining = delay;
  999.  
  1000.             self.timerId = 0;
  1001.  
  1002.             this.pause = function() {
  1003.                 window.clearTimeout(self.timerId);
  1004.                 remaining -= new Date() - start;
  1005.             };
  1006.  
  1007.             this.resume = function() {
  1008.                 start = new Date();
  1009.                 self.timerId = window.setTimeout(callback, remaining);
  1010.             };
  1011.  
  1012.             this.destroy = function()
  1013.             {
  1014.                 window.clearTimeout(self.timerId);
  1015.             };
  1016.  
  1017.             this.resume(true);
  1018.         },
  1019.  
  1020.         //start autorotation
  1021.         _startSlideshow: function()
  1022.         {
  1023.             var self = this;
  1024.  
  1025.             this.isPlaying = true;
  1026.  
  1027.             this.slideshow = new this._timer( function()
  1028.             {
  1029.                 self._navigate( 'next' );
  1030.  
  1031.                 if ( self.options.autoplay )
  1032.                 {
  1033.                     self._startSlideshow();
  1034.                 }
  1035.  
  1036.             }, (this.options.interval * 1000));
  1037.         },
  1038.  
  1039.         //stop autorotation
  1040.         _stopSlideshow: function()
  1041.         {
  1042.             if ( this.options.autoplay ) {
  1043.  
  1044.                 this.slideshow.destroy();
  1045.                 this.isPlaying = false;
  1046.                 this.options.autoplay = false;
  1047.             }
  1048.         },
  1049.  
  1050.         // public method: shows next image
  1051.         next : function(e)
  1052.         {
  1053.             e.preventDefault();
  1054.             this._stopSlideshow();
  1055.             this._navigate( 'next' );
  1056.         },
  1057.  
  1058.         // public method: shows previous image
  1059.         previous : function(e)
  1060.         {
  1061.             e.preventDefault();
  1062.             this._stopSlideshow();
  1063.             this._navigate( 'prev' );
  1064.         },
  1065.  
  1066.         // public method: goes to a specific image
  1067.         go2 : function( pos )
  1068.         {
  1069.             //if we didnt pass a number directly lets asume someone clicked on a link that triggered the goto transition
  1070.             if(isNaN(pos))
  1071.             {
  1072.                 //in that case prevent the default link behavior and set the slide number to the links hash
  1073.                 pos.preventDefault();
  1074.                 pos = pos.currentTarget.hash.replace('#','');
  1075.             }
  1076.  
  1077.             pos -= 1;
  1078.  
  1079.             if( pos === this.current || pos >= this.itemsCount || pos < 0 )
  1080.             {
  1081.                 return false;
  1082.             }
  1083.  
  1084.             this._stopSlideshow();
  1085.             this._navigate( false, pos );
  1086.  
  1087.         },
  1088.  
  1089.         // public method: starts the slideshow
  1090.         // any call to next(), previous() or goto() will stop the slideshow autoplay
  1091.         play : function()
  1092.         {
  1093.             if( !this.isPlaying )
  1094.             {
  1095.                 this.isPlaying = true;
  1096.  
  1097.                 this._navigate( 'next' );
  1098.                 this.options.autoplay = true;
  1099.                 this._startSlideshow();
  1100.             }
  1101.  
  1102.         },
  1103.  
  1104.         // public methos: pauses the slideshow
  1105.         pause : function()
  1106.         {
  1107.             if( this.isPlaying )
  1108.             {
  1109.                 this.slideshow.pause();
  1110.             }
  1111.         },
  1112.  
  1113.         // publiccmethos: resumes the slideshow
  1114.         resume : function()
  1115.         {
  1116.             if( this.isPlaying )
  1117.             {
  1118.                 this.slideshow.resume();
  1119.             }
  1120.         },
  1121.  
  1122.         // public methos: destroys the instance
  1123.         destroy : function( callback )
  1124.         {
  1125.             this.slideshow.destroy( callback );
  1126.         }
  1127.  
  1128.     };
  1129.  
  1130.     //simple wrapper to call the slideshow. makes sure that the slide data is not applied twice
  1131.     $.fn.aviaSlider = function( options )
  1132.     {
  1133.         return this.each(function()
  1134.         {
  1135.             var self = $.data( this, 'aviaSlider' );
  1136.  
  1137.             if( ! self )
  1138.             {
  1139.                 self = $.data( this, 'aviaSlider', new $.AviaSlider( options, this ) );
  1140.             }
  1141.         });
  1142.     };
  1143.  
  1144.  
  1145.  
  1146. })( jQuery );
  1147.  
  1148.  
  1149.  
  1150.  
Advertisement
Add Comment
Please, Sign In to add comment