﻿;(function($) {
	
// If the App scope is not available, add it
$.app = $.app || {};
	
$.fn.extend({
	videoplayer: function(options) {
		var args = Array.prototype.slice.call(arguments, 1);
		return this.each(function() {
			if (typeof options == "string") {
				var videoplayer = $.data(this, "app-videoplayer");
				videoplayer[options].apply(videoplayer, args);
				// INIT with optional options
			} else if (!$(this).is(".app-videoplayer"))
				$.data(this, "app-videoplayer", new $.app.videoplayer(this, options));
		});
	}
});
$.app.videoplayer = function (container, options) {

	this.element = container;
	this.instance = $.app.videoplayer.manager.addPlayer(this);
	this.initialized = false;
	this.enabled = true;
	this.ready = false;
	this.muted = false;
	this.playing = false;
	this.paused = false;
	this.duration = 0;
	this.time = 0;
	$(container).addClass("app-videoplayer");
	
	$('<div class="videoBackdrop">').appendTo(this.element);
	$('<div class="videoPlayer">').appendTo(this.element);
	$('<div class="videoHtmlLayer">').appendTo(this.element);
	this.drawSWF();
	
};
$.extend($.app.videoplayer.prototype, {
	drawSWF: function (id) {
		var swfNode = [];
		var params = {allowScriptAccess:'always', bgcolor:'#000000', wmode:'transparent'};
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
				swfNode = [];
				swfNode.push('<embed type="application/x-shockwave-flash" src="http://vp.mgnetwork.net/nebula.swf" width="100%" height="100%"');
				swfNode.push('id="'+ this.instance +'" name="'+ this.instance +'"');
				for(var key in params){ swfNode.push([key] +'="'+ params[key] +'"'); }
				swfNode.push('flashvars="eventDelegate=$.app.videoplayer.delegateEvent&id=' + this.instance + '"');
				swfNode.push('/>');
				$('#' + this.element.id + ' .videoPlayer').html(swfNode.join(' '));
		} else {
				swfNode = [];
				swfNode.push('<object id="'+ this.instance +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%">');
				swfNode.push('<param name="movie" value="http://vp.mgnetwork.net/nebula.swf" />');
				for(var key in params) { swfNode.push('<param name="'+ key +'" value="'+ params[key] +'" />'); }
				swfNode.push('<param name="flashvars" value="eventDelegate=$.app.videoplayer.delegateEvent&id='+ this.instance + '" />');
				swfNode.push("</object>");
				$('#' + this.element.id + ' .videoPlayer')[0].innerHTML = swfNode.join("\n");
		}
		
	},
	play: function (flvSrc) { // method for requesting playback of passed source
		if (flvSrc !== '' && flvSrc !== null) {
			this.getInstance()[this.instance + "_play"](flvSrc);
		} else {
			throw new Error('Unrecognized video source.');
		}
	},
	pause: function () { // method for toggling pause video playback
		if (this.playing) {
			this.getInstance()[this.instance + "_pause"]();
		} else {
			throw new Error('Unable to fulfill pause request; no video playing.');
		}
	},
	stop: function () { // method for stopping video playback
		if (this.playing) {
			this.getInstance()[this.instance + "_stop"]();
			this.playing = false;
		} else {
			throw new Error('Unable to fulfill stop request; no video playing.');
		}
	},
	seek: function (offset) { // method for seeking to passed offset
		if (this.playing) {
			this.getInstance()[this.instance + "_seek"](offset);
		} else {
			throw new Error('Unable to fulfill seek request; no video playing.');
		}
	},
	mute: function () { // method for toggling mute
		this.getInstance()[this.instance + "_mute"]();
		this.muted = !this.muted;
	},
	getVolume: function () { // method for getting volume level
		return this.getInstance()[this.instance + "_getVolume"]();
	},
	setVolume: function (lvl) { // method for setting volume level
		this.getInstance()[this.instance + "_setVolume"](lvl);
	},
	getInstance: function () { //method used to return instance to Flash
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[this.instance];
		} else {
			return document[this.instance];
		}
	}
});
$.extend($.app.videoplayer, {
	manager: {
		__players:[],
		addPlayer: function (plyr) {
			var ranNum = new Date().getTime();
			ranNum = ranNum.toString().substr(-5);
			var nLen = $.app.videoplayer.manager.__players.push(plyr);
			var nName = "$" + ranNum + "_videoplayerinstance_" + nLen;
			return nName;
		}
	},
	delegateEvent: function (target, eventType, additionalInfo) {
		var key;
		for (var i = 0; i<$.app.videoplayer.manager.__players.length; i++) {
			var player = $.app.videoplayer.manager.__players[i];
			if (player.instance == target) {
				if (typeof(additionalInfo) !== "undefined") {
					for (key in additionalInfo) {
						if (additionalInfo.hasOwnProperty(key)) {
							player[key] = additionalInfo[key];
						}
					}
				}
				$(player.element).trigger(eventType, [additionalInfo]);
				break;
			}
		}
	}
});
 
})(jQuery);
