/*
 * Example how to preload HTML5 video on the iPad (iOS 3.2+)
 * @author Miller Medeiros
 * Released under WTFPL
 */

var vid = document.createElement('video');
vid.src = 'EnchantedMobileVideoFinal.mp4';
document.getElementById('video-holder').appendChild(vid);

//this function should be called on a click event handler otherwise video won't start loading
function initVideo(){
	vid.play(); //start loading, didn't used `vid.load()` since it causes problems with the `ended` event

	if(vid.readyState !== 4){ //HAVE_ENOUGH_DATA
		vid.addEventListener('canplaythrough', onCanPlay, false);
		vid.addEventListener('load', onCanPlay, false); //add load event as well to avoid errors, sometimes 'canplaythrough' won't dispatch.
		setTimeout(function(){
			vid.pause(); //block play so it buffers before playing
		}, 1); //it needs to be after a delay otherwise it doesn't work properly.
	}else{
		//video is ready
	}
}

function onCanPlay(){
	vid.removeEventListener('canplaythrough', onCanPlay, false);
	vid.removeEventListener('load', onCanPlay, false);
	//video is ready
	vid.play();
}
