
// =================================================================================================
//
// videoLoader: abstracts the process of loading video away from the player, allowing support 
// 		for multiple players without duplicate code.  this file loads accessory files relative
//		to this script, and the name of this script MUST be 'videoLoader.js'.
//
// =================================================================================================

function videoLoader (params) {

	this.validParams = false;
	
	this.cwd = '';
	
	// location of videojs/flowplayer files must be relative to this file
	this.files = {
		videojs_script : "html5/video.js",
		videojs_style :  "html5/video-js.css",
		flowplayer_script : "flowplayer/flowplayer-3.2.4.min.js",
		flowplayer_swf : "flowplayer/flowplayer-3.2.5.swf"
	};
	
	this.src = null;
	this.src_mobile = null;
	this.width = 0;
	this.height = 0;
	this.container = null;
	this.src_backup = null;
	this.autoplay = false;

	if (typeof jQuery == 'undefined') {
		console.error("jQuery library not present. videoLoader will abort");
		return;
	}
	
	for (var key in params) {
		this[key] = params[key];
	}
	
	// set script base dir
	var scripts = $('script');
	for (var i in scripts) {	
		if (scripts[i].src && scripts[i].src.match(/videoLoader.js/i)) {
			var src = scripts[i].src;
			if ((queryPos = src.indexOf('?')) != -1) {
				src = src.substring(queryPos + 1);
			}
			this.cwd = src.substring(0, src.lastIndexOf('/'));
			break;
		}
	}
		
	for (var key in this.files) {
		if (!this.files[key].match(/^http/i)) {
			this.files[key] = this.cwd + '/' + this.files[key];
		}
	}
	
	if (typeof this.container == null) {
		console.error("no container provided for videoLoader");
		return;
	}
	
	if (typeof this.container == 'string') {
		this.container = document.getElementById(this.container);
	}
	
	if (this.width == 0) {
		this.width = $(this.container).width();
	}
	
	if (this.height == 0) {
		this.height = $(this.container).height();
	}
	
	if (this.src == null) {
		console.error("no source video file provided to video loader");
		return;
	}
	
	if (this.src_mobile == null) {
		this.src_mobile = this.src;
	}
	
	this.validParams = true;
}


videoLoader.prototype.load = function () {
	var self = this;
	if(this.isMobile()) {
		this.loadStyle(this.files['videojs_style']);
		$.getScript(this.files['videojs_script'], function () {
			self._loadVideoJs();
		});	  
	} else {
		$.getScript(this.files['flowplayer_script'], function () {
			self._loadFlowplayer();
		});					
	}	
}

videoLoader.prototype.play = function () {
	if ($('#videojs').length > 0) {
		$('#videojs')[0].play();
	} else {
		$f("videoContainer").play(); 
	}
}

videoLoader.prototype.stop = function () {
	if ($('#videojs').length > 0) {
		$('#videojs')[0].pause();
	} else {
		$f("videoContainer").stop(); 
	}
}

videoLoader.prototype._loadVideoJs = function () {
	if (!$(this.container).hasClass('video-js-box')) {
		$(this.container).addClass('video-js-box');
	}

	$(this.container).html(
  	"<video " + 
			"id='videojs' " + 
			"width=" + this.width + " " +
			"height=" + this.height + " " +
			"class='video-js' " +
			"controls " + 
			(this.autoplay ? 'autoplay ' : '') +
		">" +
  		"<source src='" + this.src_mobile + "' type='video/mp4' />" +
  	"</video>"			  
  );
  VideoJS.setup("videojs");
}

videoLoader.prototype._loadFlowplayer = function () {
	var self = this;
	flowplayer(
		this.container,
		{	
			src: this.files['flowplayer_swf'], 
			version : [9, 115]
		}, {	
			clip: { 
				url: this.src,
				scaling: 'orig',
				autoplay: this.autoplay
			},
			onError : function (errorCode, errorMsg) {
				if (errorCode == 200 && self.src_backup != null) {
					this.getClip().update({url: self.src_backup });
					setTimeout(function() {	$f().play(); }, 0);
				}
			}						
		}
	);
}



videoLoader.prototype.isMobile = function () {
	//return !!document.createElement('video').canPlayType;
	// 2011-05-06: need to support nook and other uncommon platforms

	var isMobile = 
		navigator.userAgent.match(/iPhone/i) || 
		navigator.userAgent.match(/iPod/i) || 
		navigator.userAgent.match(/Android/i);
	return isMobile;
}

videoLoader.prototype.loadStyle = function (url) {
	var link = $("<link>");
	link.attr({
		type: 'text/css',
		rel: 'stylesheet',
		href: url
	});
	$("head").append(link); 
}

