/*! lightgallery - v1.2.0 - 2015-08-26
* http://sachinchoolur.github.io/lightgallery/
* copyright (c) 2015 sachin n; licensed apache 2.0 */
(function($, window, document, undefined) {
'use strict';
var defaults = {
videomaxwidth: '855px',
youtubeplayerparams: false,
vimeoplayerparams: false,
videojs: false
};
var video = function(element) {
this.core = $(element).data('lightgallery');
this.$el = $(element);
this.core.s = $.extend({}, defaults, this.core.s);
this.videoloaded = false;
this.init();
return this;
};
video.prototype.init = function() {
var _this = this;
// event triggered when video url found without poster
_this.core.$el.on('hasvideo.lg.tm', function(event, index, src, html) {
_this.core.$slide.eq(index).find('.lg-video').append(_this.loadvideo(src, 'lg-object', true, index, html));
if (html) {
if (_this.core.s.videojs) {
try {
videojs(_this.core.$slide.eq(index).find('.lg-html5').get(0), {}, function() {
if (!_this.videoloaded) {
this.play();
}
});
} catch (e) {
console.error('make sure you have included videojs');
}
} else {
_this.core.$slide.eq(index).find('.lg-html5').get(0).play();
}
}
});
// set max width for video
_this.core.$el.on('onaferappendslide.lg.tm', function(event, index) {
_this.core.$slide.eq(index).find('.lg-video-cont').css('max-width', _this.core.s.videomaxwidth);
_this.videoloaded = true;
});
var loadonclick = function($el) {
// check slide has poster
if ($el.find('.lg-object').hasclass('lg-has-poster')) {
// chack already video element present
if (!$el.hasclass('lg-has-video')) {
$el.addclass('lg-video-palying lg-has-video');
var _src;
var _html;
var _loadvideo = function(_src, _html) {
$el.find('.lg-video').append(_this.loadvideo(_src, '', false, _this.core.index, _html));
if (_html) {
if (_this.core.s.videojs) {
try {
videojs(_this.core.$slide.eq(_this.core.index).find('.lg-html5').get(0), {}, function() {
this.play();
});
} catch (e) {
console.error('make sure you have included videojs');
}
} else {
_this.core.$slide.eq(_this.core.index).find('.lg-html5').get(0).play();
}
}
};
if (_this.core.s.dynamic) {
_src = _this.core.s.dynamicel[_this.core.index].src;
_html = _this.core.s.dynamicel[_this.core.index].html;
_loadvideo(_src, _html);
} else {
_src = _this.core.$items.eq(_this.core.index).attr('data-src');
_html = _this.core.$items.eq(_this.core.index).attr('data-html');
_loadvideo(_src, _html);
}
var $tempimg = $el.find('.lg-object');
$el.find('.lg-video').append($tempimg);
// @todo loading icon for html5 videos also
// for showing the loading indicator while loading video
if (!$el.find('.lg-video-object').hasclass('lg-html5')) {
$el.removeclass('lg-complete');
$el.find('.lg-video-object').on('load.lg error.lg', function() {
$el.addclass('lg-complete');
});
}
} else {
var youtubeplayer = $el.find('.lg-youtube').get(0);
var vimeoplayer = $el.find('.lg-vimeo').get(0);
var html5player = $el.find('.lg-html5').get(0);
if (youtubeplayer) {
youtubeplayer.contentwindow.postmessage('{"event":"command","func":"playvideo","args":""}', '*');
} else if (vimeoplayer) {
try {
$f(vimeoplayer).api('play');
} catch (e) {
console.error('make sure you have included froogaloop2 js');
}
} else if (html5player) {
if (_this.core.s.videojs) {
try {
videojs(html5player).play();
} catch (e) {
console.error('make sure you have included videojs');
}
} else {
html5player.play();
}
}
$el.addclass('lg-video-palying');
}
}
};
if (_this.core.docss()) {
_this.core.$el.on('onslideclick.lg.tm', function() {
var $el = _this.core.$slide.eq(_this.core.index);
loadonclick($el);
});
} else {
// for ie 9 and bellow
_this.core.$slide.on('click.lg', function() {
loadonclick($(this));
});
}
_this.core.$el.on('onbeforeslide.lg.tm', function(event, previndex) {
var $videoslide = _this.core.$slide.eq(previndex);
var youtubeplayer = $videoslide.find('.lg-youtube').get(0);
var vimeoplayer = $videoslide.find('.lg-vimeo').get(0);
var html5player = $videoslide.find('.lg-html5').get(0);
if (youtubeplayer) {
youtubeplayer.contentwindow.postmessage('{"event":"command","func":"pausevideo","args":""}', '*');
} else if (vimeoplayer) {
try {
$f(vimeoplayer).api('pause');
} catch (e) {
console.error('make sure you have included froogaloop2 js');
}
} else if (html5player) {
if (_this.core.s.videojs) {
try {
videojs(html5player).pause();
} catch (e) {
console.error('make sure you have included videojs');
}
} else {
html5player.pause();
}
}
//$videoslide.addclass('lg-complete');
});
_this.core.$el.on('onafterslide.lg.tm', function(event, previndex) {
_this.core.$slide.eq(previndex).removeclass('lg-video-palying');
});
};
video.prototype.loadvideo = function(src, addclass, noposter, index, html) {
var video = '';
var autoplay = 1;
var a = '';
var isvideo = this.core.isvideo(src, index) || {};
// enable autoplay for first video if poster doesn't exist
if (noposter) {
if (this.videoloaded) {
autoplay = 0;
} else {
autoplay = 1;
}
}
if (isvideo.youtube) {
a = '?wmode=opaque&autoplay=' + autoplay + '&enablejsapi=1';
if (this.core.s.youtubeplayerparams) {
a = a + '&' + $.param(this.core.s.youtubeplayerparams);
}
video = '';
} else if (isvideo.vimeo) {
a = '?autoplay=' + autoplay + '&api=1';
if (this.core.s.vimeoplayerparams) {
a = a + '&' + $.param(this.core.s.vimeoplayerparams);
}
video = '';
} else if (isvideo.html5) {
var fl = html.substring(0, 1);
if (fl === '.' || fl === '#') {
html = $(html).html();
}
video = html;
}
return video;
};
video.prototype.destroy = function() {
this.videoloaded = false;
};
$.fn.lightgallery.modules.video = video;
})(jquery, window, document);