// ==UserScript==
// @name	XenForo - Prevent Video Preload 
// @namespace	Makaze
// @description	Prevents the browser from pre-downloading embedded videos. 
// @include	http://www.emudesc.com/*
// @grant	none
// @version	1.0.2
// ==/UserScript==

var MakazeScriptStyles,
styleElem,
text,
i = 0,
z = 0,
videoHosts = [/www.youtube.com/, /www.dailymotion.com/];

function createElement(type, callback) {
	var element = document.createElement(type);

	callback(element);

	return element;
}

function replaceVideo(vid) {
	var j = 0;

	vid.setAttribute('data-src', vid.src);
	vid.removeAttribute('src');

	vid.parentNode.insertBefore(createElement('a', function(a) {
		a.className = 'EmbeddedContentLink';
		a.href = 'javascript:void(0)';
		a.title = 'Click to reveal';

		a.appendChild(createElement('span', function(code) {
			code.className = 'EmbeddedIcon';
			code.appendChild(document.createTextNode('</>'));
		}));

		a.appendChild(document.createTextNode(' ' + text));
		
		a.onclick = function(event) {
			event.target.nextSibling.src = event.target.nextSibling.getAttribute('data-src');

			event.target.nextSibling.style.display = 'block';

			a.remove();
		};
	}), vid);

	vid.style.display = 'none';
}


if (document.documentElement.id === 'XenForo') {
	//Language
	var lang = document.documentElement.lang;
	if(lang == 'es-ES' || lang == 'es-LA'){
		text = 'Contenido Incrustado';
	} else {
		text = 'Embedded Content';
	}
	
	// Styling

	if (document.getElementById('MakazeScriptStyles') == null) {
		MakazeScriptStyles = createElement('style', function(style) {
			style.id = 'MakazeScriptStyles';
			style.type = 'text/css';
		});
		document.head.appendChild(MakazeScriptStyles);
	}

	styleElem = document.getElementById('MakazeScriptStyles');

	if (styleElem.hasChildNodes()) {
		styleElem.childNodes[0].nodeValue += '\n\n';
	} else {
		styleElem.appendChild(document.createTextNode(''));
	}

	styleElem.childNodes[0].nodeValue +=
		'.EmbeddedContentLink {\n' +
			'padding: 5px ! important;\n' +
			 //'background-color: #f8f8f8;\n' +
			'border-radius: 3px ! important;\n' +
		'}\n\n' +

		'.EmbeddedIcon {\n' +
			'font-family: consolas,monospace;\n' +
			'font-size: 150%;\n' +
			'color: #f24;\n' +
			'font-weight: bold;\n' +
			'text-shadow: 0px 1px #444;\n' +
		'}';

	for (i = 0; i < document.getElementsByTagName('iframe').length; i++) {
		for(z = 0; z < videoHosts.length; z++){
			if(document.getElementsByTagName('iframe')[i].src.match(videoHosts[z]) && !document.getElementsByTagName('iframe')[i].hasAttribute('data-src')) {
				replaceVideo(document.getElementsByTagName('iframe')[i]);
			}
		}
	}

	document.addEventListener('DOMNodeInserted', function(event) {
		var vids = event.target.parentNode.getElementsByTagName('iframe');

		for (i = 0; i < vids.length; i++) {
			if (vids[i].className === 'EmbeddedVideo' && !vids[i].hasAttribute('data-src')) {
				replaceVideo(vids[i]);
			}
		}
	}, false);
}
