/*
* Vide - v0.3.0
* Easy as hell jQuery plugin for video backgrounds.
* http://vodkabears.github.io/vide/
*
* Made by Ilya Makarov
* Under MIT License
*/
!(function($, window, document, navigator) {
"use strict";
/**
* Vide settings
* @private
*/
var pluginName = "vide",
defaults = {
volume: 1,
playbackRate: 1,
muted: true,
loop: true,
autoplay: true,
position: "50% 50%",
posterType: "detect",
resizing: true
},
// is iOs?
isIOS = /iPad|iPhone|iPod/i.test(navigator.userAgent),
// is Android?
isAndroid = /Android/i.test(navigator.userAgent);
/**
* Parse string with options
* @param {String} str
* @returns {Object|String}
* @private
*/
function parseOptions(str) {
var obj = {},
delimiterIndex,
option,
prop, val,
arr, len,
i;
// remove spaces around delimiters and split
arr = str.replace(/\s*:\s*/g, ":").replace(/\s*,\s*/g, ",").split(",");
// parse string
for (i = 0, len = arr.length; i < len; i++) {
option = arr[i];
// Ignore urls and 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 string value if it is like a boolean
if (typeof val === "string") {
val = val === "true" || (val === "false" ? false : val);
}
// convert 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 position option
* @param {String} str
* @returns {Object}
* @private
*/
function parsePosition(str) {
str = "" + str;
// default value is a center
var args = str.split(/\s+/),
x = "50%", y = "50%",
len, arg,
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 poster
* @param {String} path
* @param {Function} callback
* @private
*/
function findPoster(path, callback) {
var onLoad = function() {
callback(this.src);
};
$("").load(onLoad);
$("
").load(onLoad);
$("
").load(onLoad);
$("
").load(onLoad);
}
/**
* 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 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,
position = parsePosition(vide.settings.position),
sources,
poster;
// Set video wrapper styles
vide.$wrapper = $("