| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634 | /*!----------------------------------------------------------------------------- * Vegas - Fullscreen Backgrounds and Slideshows. * v2.0.0-wip - built 2015-03-04 * Licensed under the MIT License. * http://vegas.jaysalvat.com/ * ---------------------------------------------------------------------------- * Copyright (C) 2010-2015 Jay Salvat * http://jaysalvat.com/ * --------------------------------------------------------------------------*//* global jQuery, Zepto */(function ($) {    'use strict';    var defaults = {        slide:              0,        delay:              5000,        preload:            false,        preloadImage:       false,        preloadVideo:       false,        timer:              true,        overlay:            false,        autoplay:           true,        shuffle:            false,        cover:              true,        color:              null,        align:              'center',        valign:             'center',        transition:         'fade',        transitionDuration: 1000,        animation:          null,        animationDuration:  'auto',        init:  function () {},        play:  function () {},        pause: function () {},        walk:  function () {},        slides: [            // {               //  src:                null,            //  color:              null,            //  delay:              null,            //  align:              null,            //  valign:             null,            //  transition:         null,            //  transitionDuration: null,            //  animation:          null,            //  animationDuration:  null,            //  cover:              true,            //  videos:             []            // }            // ...        ]    };    var videoCache = {};    var Vegas = function (elmt, options) {        this.elmt         = elmt;        this.settings     = $.extend({}, defaults, $.vegas.defaults, options);        this.slide        = this.settings.slide;        this.total        = this.settings.slides.length;        this.noshow       = this.total < 2;        this.paused       = !this.settings.autoplay || this.noshow;        this.$elmt        = $(elmt);        this.$timer       = null;        this.$overlay     = null;        this.$slide       = null;        this.timeout      = null;        this.transitions  = [];        this.animations   = [];        this.support = {            objectFit:  'objectFit'  in document.body.style,            transition: 'transition' in document.body.style || 'WebkitTransition' in document.body.style,            video:      $.vegas.isVideoCompatible()        };        for (var i = 0; i < document.styleSheets.length; i++) {            var sheet = document.styleSheets[i],                rules;            try {                rules = (sheet.cssRules || sheet.rules);            } catch(e) {                continue;            }                        if (/vegas(\..*?)?(\.min)?\.css$/.test(sheet.href)) {                for (var j = 0; j < rules.length; j++) {                    var rule  = rules[j],                        matchTransition = /vegas\-transition\-([a-z0-9]*)/gi.exec(rule.selectorText),                        matchAnimation  = /vegas\-animation\-([a-z0-9]*)/gi.exec(rule.selectorText);                                    if (matchTransition && matchTransition[1]) {                        if (this.transitions.indexOf(matchTransition[1]) === -1) {                            this.transitions.push(matchTransition[1]);                        }                    }                    if (matchAnimation && matchAnimation[1]) {                        if (this.animations.indexOf(matchAnimation[1]) === -1) {                            this.animations.push(matchAnimation[1]);                        }                    }                }            }        }        if (this.settings.shuffle === true) {            this.shuffle();        }        this._init();    };    Vegas.prototype = {        _init: function () {            var $wrapper,                $overlay,                $timer,                isBody  = this.elmt.tagName === 'BODY',                timer   = this.settings.timer,                overlay = this.settings.overlay,                self    = this;            // Preloading            this._preload();            // Wrapper with content            if (!isBody) {                this.$elmt.css('height', this.$elmt.css('height'));                                $wrapper = $('<div class="vegas-wrapper">')                    .css('overflow', this.$elmt.css('overflow'))                    .css('padding',  this.$elmt.css('padding'));                // Some browsers don't compute padding shorthand                if (!this.$elmt.css('padding')) {                    $wrapper                        .css('padding-top',    this.$elmt.css('padding-top'))                        .css('padding-bottom', this.$elmt.css('padding-bottom'))                        .css('padding-left',   this.$elmt.css('padding-left'))                        .css('padding-right',  this.$elmt.css('padding-right'));                }                this.$elmt.clone(true).children().appendTo($wrapper);                this.elmt.innerHTML = '';            }            // Timer            if (timer && this.support.transition) {                $timer = $('<div class="vegas-timer"><div class="vegas-timer-progress">');                this.$timer = $timer;                this.$elmt.prepend($timer);            }            // Overlay            if (overlay) {                $overlay = $('<div class="vegas-overlay">');                if (typeof overlay === 'string') {                    $overlay.css('background-image', 'url(' + overlay + ')');                }                this.$overlay = $overlay;                this.$elmt.prepend($overlay);            }            // Container            this.$elmt.addClass('vegas-container');            if (!isBody) {                this.$elmt.append($wrapper);            }            setTimeout(function () {                self.trigger('init');                self._goto(self.slide);                if (self.settings.autoplay) {                    self.trigger('play');                }            }, 1);        },        _preload: function () {            var video, img, i;            for (i = 0; i < this.settings.slides.length; i++) {                if (this.settings.preload || this.settings.preloadImages) {                    if (this.settings.slides[i].src) {                        img = new Image();                        img.src = this.settings.slides[i].src;                    }                }                if (this.settings.preload || this.settings.preloadVideos) {                    if (this.support.video && this.settings.slides[i].video) {                        video = this._video(this.settings.slides[i].video);                    }                }            }        },        _random: function (array) {            return array[Math.floor(Math.random() * (array.length - 1))];        },        _slideShow: function () {            var self = this;            if (this.total > 1 && !this.paused && !this.noshow) {                this.timeout = setTimeout(function () {                    self.next();                }, this._options('delay'));             }        },        _timer: function (state) {            var self = this;            clearTimeout(this.timeout);            if (!this.$timer) {                return;            }            this.$timer                .removeClass('vegas-timer-running')                    .find('div')                        .css('transition-duration', '0ms');            if (this.paused || this.noshow) {                return;            }            if (state) {                setTimeout(function () {                   self.$timer                    .addClass('vegas-timer-running')                        .find('div')                            .css('transition-duration', self._options('delay') - 100 + 'ms');                }, 100);            }        },        _video: function (srcs) {            var video,                 source,                cacheKey = srcs.toString();            if (videoCache[cacheKey]) {                return videoCache[cacheKey];            }            if (srcs instanceof Array === false) {                srcs = [ srcs ];            }            video = document.createElement('video');            video.muted = true;            video.preload = true;            srcs.forEach(function (src) {                source = document.createElement('source');                source.src = src;                video.appendChild(source);            });            videoCache[cacheKey] = video;            return video;        },        _options: function (key, i) {            if (i === undefined) {                i = this.slide;            }            if (this.settings.slides[i][key] !== undefined) {                return this.settings.slides[i][key];            }            return this.settings[key];        },        _goto: function (nb) {            if (typeof this.settings.slides[nb] === 'undefined') {                nb = 0;            }            this.slide = nb;            var $slide,                $inner,                $video,                $slides = this.$elmt.children('.vegas-slide'),                src     = this.settings.slides[nb].src,                videos  = this.settings.slides[nb].video,                delay   = this._options('delay'),                align   = this._options('align'),                valign  = this._options('valign'),                color   = this._options('color') || this.$elmt.css('background-color'),                cover   = this._options('cover') ? 'cover' : 'contain',                self    = this,                total   = $slides.length,                video,                img;            var transition         = this._options('transition'),                transitionDuration = this._options('transitionDuration'),                animation          = this._options('animation' ),                animationDuration  = this._options('animationDuration');            if (transition === 'random' || transition instanceof Array) {                if (transition instanceof Array) {                    transition = this._random(transition);                } else {                    transition = this._random(this.transitions);                }            }            if (animation === 'random' || animation instanceof Array) {                if (animation instanceof Array) {                    animation = this._random(animation);                } else {                    animation = this._random(this.animations);                }            }            if (transition && transition !== 'none' && this.transitions.indexOf(transition) < 0) {                console.error("Vegas: Transition " + transition + " doesn't exist.");            }            if (animation && animation !== 'none' && this.animations.indexOf(animation) < 0) {                console.error("Vegas: Animation " + animation + " doesn't exist.");            }            if (transitionDuration === 'auto' || transitionDuration > delay) {                transitionDuration = delay;            }            if (animationDuration === 'auto') {                animationDuration = delay;            }            $slide = $('<div class="vegas-slide"></div>');                        if (this.support.transition && transition) {                $slide.addClass('vegas-transition-' + transition);            }            // Video ?            if (this.support.video && videos) {                video = this._video(videos);                $video = $(video)                    .addClass('vegas-video')                    .css('background-color', color);                if (this.support.objectFit) {                    $video                        .css('object-position', align + ' ' + valign)                        .css('object-fit', cover)                        .css('width',  '100%')                        .css('height', '100%');                } else if (cover === 'contain') {                    $video                        .css('width',  '100%')                        .css('height', '100%');                }                $slide.append($video);            // Image ?            } else {                img = new Image();                $inner = $('<div class="vegas-slide-inner"></div>')                    .css('background-image',    'url(' + src + ')')                    .css('background-color',    color)                    .css('background-position', align + ' ' + valign)                    .css('background-size',     cover);                if (this.support.transition && animation) {                    $inner                        .addClass('vegas-animation-' + animation)                        .css('animation-duration',  animationDuration + 'ms');                }                $slide.append($inner);            }            if (!this.support.transition) {                $slide.css('display', 'none');            }            if (total) {                $slides.eq(total - 1).after($slide);            } else {                this.$elmt.prepend($slide);            }            $slides                .css('transition', 'all 0ms')                .each(function () {                    this.className  = 'vegas-slide';                    if (this.tagName === 'VIDEO') {                        this.className += ' vegas-video';                        }                    if (transition) {                        this.className += ' vegas-transition-' + transition;                        this.className += ' vegas-transition-' + transition + '-in';                    }                }            );            self._timer(false);            function go () {                self._timer(true);                setTimeout(function () {                    if (transition) {                        if (self.support.transition) {                            $slides                                .css('transition', 'all ' + transitionDuration + 'ms')                                .addClass('vegas-transition-' + transition + '-out');                            $slide                                .css('transition', 'all ' + transitionDuration + 'ms')                                .addClass('vegas-transition-' + transition + '-in');                        } else {                            $slide.fadeIn(transitionDuration);                        }                    }                    for (var i = 0; i < $slides.length - 1; i++) {                         $slides.eq(i).remove();                    }                    self.trigger('walk');                    self._slideShow();                }, 100);            }            if (video) {                if (video.readyState === 4) {                    video.currentTime = 0;                    video.play();                    go();                } else {                    video.oncanplay = function () {                        video.play();                        if (!video._started) {                            video._started = true;                            go();                        }                    };                }            } else {                img.src = src;                img.onload = go;            }        },        shuffle: function () {            var temp,                rand;            for (var i = this.total - 1; i > 0; i--) {                rand = Math.floor(Math.random() * (i + 1));                temp = this.settings.slides[i];                this.settings.slides[i] = this.settings.slides[rand];                this.settings.slides[rand] = temp;            }        },        play: function () {            if (this.paused) {                this.paused = false;                this.next();                this.trigger('play');            }        },        pause: function () {            this._timer(false);            this.paused = true;            this.trigger('pause');        },        toggle: function () {            if (this.paused) {                this.play();            } else {                this.pause();            }        },        playing: function () {            return !this.paused && !this.noshow;        },        current: function (advanced) {            if (advanced) {                return {                    slide: this.slide,                    data:  this.settings.slides[this.slide]                };            }            return this.slide;        },        jump: function (nb) {            if (nb < 0 || nb > this.total - 1 || nb === this.slide) {                return;            }            this.slide = nb;            this._goto(this.slide);        },        next: function () {            this.slide++;            if (this.slide >= this.total) {                this.slide = 0;            }            this._goto(this.slide);        },        previous: function () {            this.slide--;            if (this.slide < 0) {                this.slide = this.total - 1;            }            this._goto(this.slide);        },        trigger: function (fn) {            var params = [];            if (fn === 'init') {                params = [ this.settings ];            } else {                params = [                     this.slide,                     this.settings.slides[this.slide]                ];            }            this.$elmt.trigger('vegas' + fn, params);            if (typeof this.settings[fn] === 'function') {                this.settings[fn].apply(this.$elmt, params);            }        },        options: function (key, value) {            var oldSlides = this.settings.slides.slice();            if (typeof key === 'object') {                this.settings = $.extend({}, defaults, $.vegas.defaults, key);            } else if (typeof key === 'string') {                if (value === undefined) {                    return this.settings[key];                }                this.settings[key] = value;             } else {                return this.settings;            }            // In case slides have changed            if (this.settings.slides !== oldSlides) {                this.total  = this.settings.slides.length;                this.noshow = this.total < 2;                this._preload();               }        }    };    $.fn.vegas = function(options) {        var args = arguments,            error = false,            returns;        if (options === undefined || typeof options === 'object') {            return this.each(function () {                if (!this._vegas) {                    this._vegas = new Vegas(this, options);                }            });        } else if (typeof options === 'string') {            this.each(function () {                var instance = this._vegas;                if (!instance) {                    throw new Error('No Vegas applied to this element.');                }                if (typeof instance[options] === 'function' && options[0] !== '_') {                    returns = instance[options].apply(instance, [].slice.call(args, 1));                } else {                    error = true;                }            });            if (error) {                throw new Error('No method "' + options + '" in Vegas.');            }            return returns !== undefined ? returns : this;        }    };    $.vegas = {};    $.vegas.defaults = defaults;    $.vegas.isVideoCompatible = function () {        return !/(Android|webOS|Phone|iPad|iPod|BlackBerry|Windows Phone)/i.test(navigator.userAgent);    };})(typeof jQuery !== 'undefined' ? jQuery :   typeof Zepto  !== 'undefined' ? Zepto  : null);
 |