$(document).ready(function() {
	var videoLinks=$("a[href$='.flv'], a[href$='.mp4'], a[href*='youtube.com/watch']");
	if(videoLinks.length>=1){
		strobeVideoObjectID='strobeVideoPlayer';
		videoPlayerOn(videoLinks);
	}

});
function videoPlayerOn(videoLinks){
	//find links to flv adn mp4 files (TODO & youtube videos) then create a videoPlayer and make the links a video PlayList
	//TODO dehardcode PLUGINS_DIR?
	var siteUrl=base_url;
	var pluginDir=siteUrl+'webroot/pathfinder/strobeVideoPlayer/';
	var defaultwidth = 448, defaultheight = 336;
	var videoLinks=$("a[href$='.flv'], a[href$='.mp4'], a[href*='youtube.com/watch']");
	$.fn.strobemediaplayback.defaults.swf = pluginDir+"StrobeMediaPlayback.swf";
		var currentwidth = '';
		var currentheight = '';
		videoLinks.each(function(i){
			var current = $(this);
			var currentstartimage = current.find('img').attr('src'); if (currentstartimage==null) currentstartimage='';
			if(i==0){
				currentwidth = current.find('img').attr('width'); if (currentwidth==null) currentwidth=defaultwidth;
				currentheight = current.find('img').attr('height'); if (currentheight==null) currentheight=defaultheight;
				//TODO AUTOPLAY as CLASS IN LINK
				current.before('<div class="strobeVideoWrapper"><div id="'+strobeVideoObjectID+'"></div></div>');
				current.find('img').css('display','none');
				current.text(current.attr('title'));
				//EMBED THE VIDEO WITH swfobject
				//swfobject.embedSWF(pluginDir+'StrobeMediaPlayback.swf', strobeVideoObjectID, currentwidth, currentheight, '10.1.0', '{}', flashvars, params, attributes);
				var options={
					id: strobeVideoObjectID,
					src: current.attr('href'),
					width: currentwidth,
					height: currentheight,
					poster: currentstartimage,
					endOfVideoOverlay: currentstartimage,
					skin: pluginDir+'skin/skin.xml',
					favorFlashOverHtml5Video: true
				};
				// Using HTMLVideoElement directly
				//var $video = $(".videoElement");
				// Using StrobeMediaPlayback
				videoEl = $("#" + strobeVideoObjectID).strobemediaplayback(options);
				videoEl.bind("complete", videoOnComplete);
				videoEl.bind("play", videoOnPlay);
				videoEl.bind("pause", videoOnPause);
// 				$video.bind("durationchange", onDurationChange);
// 				$video.bind("timeupdate", onTimeUpdate);
			}
		});
		videoLinks.bind("click", videoEl, onPlaylistItemClick);
//TODO Youtube links
}
function onPlaylistItemClick(event)
{
	event.preventDefault();
	var video = event.data[0];
	video.src = this.href;
	video.load();
	video.play();
}

/*
//TODO include mp3 player here?
	}else if (currenthref.toLowerCase().indexOf('.mp3') > 0) {
		current.empty().flash({swf: pluginDir+"/player_mp3_maxi.swf", flashvars: {mp3: currenthref, showslider: '1', width: (currentwidth-10), height: 20, bgcolor1: 'f2f2f2', bgcolor2: 'e6e6e6', buttoncolor: '000000', buttonovercolor: '00ad00'}, wmode: 'transparent', width: (currentwidth-10), height: 20});
		current.prepend("<img class='img-type' src='"+currentstartimage+"' width='"+currentwidth+"' height='"+currentheight+"' /></span>").find('object').addClass('mp3-type').attr('title', currenttitle).wrap("<span class='portfolio-mp3-container' style='top: "+(currentheight-20)+"px; margin-top: 0; margin-left: -"+parseInt((currentwidth-10)/2)+"px;'></span>");
		elementclass = 'portfolio-mp3';
	}
*/



//player='foo';
function videoOnComplete() {
	console.log('Video Complete!');
	//var iniloc=$("a.saltarainicio").attr('href');
	//window.location = iniloc;
	//window.location = "http://www.danielcuberta.com/v2/peliculas";
}
function videoOnPlay() {
	console.log('Video Playing!');
// 	$('header').css('opacity',0);
// 	$('aside').css('opacity',0);
}
function videoOnPause() {
	console.log('Video Paused!');
// 	$('header').css('opacity',100);
// 	$('aside').css('opacity',100);
}
/*function onJavaScriptBridgeCreated(id)
{
	//console.log(id);
	if (player == 'foo'){ //to attach the listener only once
		player = document.getElementById(id);
		//player.bind("complete", onComplete);
		//console.log('was foo: ' +player);
		player.addEventListener("complete", "videoOnComplete");
		player.addEventListener("play", "videoOnPlay");
		player.addEventListener("pause", "videoOnPause");
	}
}*/
