!(function($, window, document, navigator) {
'use strict';
/**
* Name of the plugin
* @private
*/
var pluginName = 'vide';
/**
* Default settings
* @private
*/
var defaults = {
volume: 1,
playbackRate: 1,
muted: true,
loop: true,
autoplay: true,
position: '50% 50%',
posterType: 'detect',
resizing: true
};
/**
* Is iOs?
* @private
*/
var isIOS = /iPad|iPhone|iPod/i.test(navigator.userAgent);
/**
* Is Android?
* @private
*/
var isAndroid = /Android/i.test(navigator.userAgent);
/**
* Parse a string with options
* @private
* @param {String} str
* @returns {Object|String}
*/
function parseOptions(str) {
var obj = {};
var delimiterIndex;
var option;
var prop;
var val;
var arr;
var len;
var i;
// Remove spaces around delimiters and split
arr = str.replace(/\s*:\s*/g, ':').replace(/\s*,\s*/g, ',').split(',');
// Parse a string
for (i = 0, len = arr.length; i < len; i++) {
option = arr[i];
// Ignore urls and a string without colon delimiters
if (option.search(/^(http|https|ftp):\/\//) !== -1 ||
option.search(':') === -1)
{
break;
}
delimiterIndex = option.indexOf(':');
prop = option.substring(0, delimiterIndex);
val = option.substring(delimiterIndex + 1);
// If val is an empty string, make it undefined
if (!val) {
val = undefined;
}
// Convert a string value if it is like a boolean
if (typeof val === 'string') {
val = val === 'true' || (val === 'false' ? false : val);
}
// Convert a string value if it is like a number
if (typeof val === 'string') {
val = !isNaN(val) ? +val : val;
}
obj[prop] = val;
}
// If nothing is parsed
if (prop == null && val == null) {
return str;
}
return obj;
}
/**
* Parse a position option
* @private
* @param {String} str
* @returns {Object}
*/
function parsePosition(str) {
str = '' + str;
// Default value is a center
var args = str.split(/\s+/);
var x = '50%';
var y = '50%';
var len;
var arg;
var i;
for (i = 0, len = args.length; i < len; i++) {
arg = args[i];
// Convert values
if (arg === 'left') {
x = '0%';
} else if (arg === 'right') {
x = '100%';
} else if (arg === 'top') {
y = '0%';
} else if (arg === 'bottom') {
y = '100%';
} else if (arg === 'center') {
if (i === 0) {
x = '50%';
} else {
y = '50%';
}
} else {
if (i === 0) {
x = arg;
} else {
y = arg;
}
}
}
return { x: x, y: y };
}
/**
* Search a poster
* @private
* @param {String} path
* @param {Function} callback
*/
function findPoster(path, callback) {
var onLoad = function() {
callback(this.src);
};
$('').load(onLoad);
$('
').load(onLoad);
$('
').load(onLoad);
$('
').load(onLoad);
}
/**
* Refresh current settings
* @private
* @param {Vide} vide
*/
function refreshSettings(vide) {
vide.$video.prop({
autoplay: vide.settings.autoplay,
loop: vide.settings.loop,
volume: vide.settings.volume,
muted: vide.settings.muted,
playbackRate: vide.settings.playbackRate
});
}
/**
* Vide constructor
* @param {HTMLElement} element
* @param {Object|String} path
* @param {Object|String} options
* @constructor
*/
function Vide(element, path, options) {
this.$element = $(element);
// Parse path
if (typeof path === 'string') {
path = parseOptions(path);
}
// Parse options
if (!options) {
options = {};
} else if (typeof options === 'string') {
options = parseOptions(options);
}
// Remove an extension
if (typeof path === 'string') {
path = path.replace(/\.\w*$/, '');
} else if (typeof path === 'object') {
for (var i in path) {
if (path.hasOwnProperty(i)) {
path[i] = path[i].replace(/\.\w*$/, '');
}
}
}
this.settings = $.extend({}, defaults, options);
this.path = path;
this.init();
}
/**
* Initialization
* @public
*/
Vide.prototype.init = function() {
var vide = this;
var position = parsePosition(vide.settings.position);
var sources;
var poster;
// Set styles of a video wrapper
vide.$wrapper = $('