﻿/*******************************************************************************************/
/***************************************** VIDEO PLAYER ************************************/
/*******************************************************************************************/
var player;
var playerID = null;
function playerReadyCallback(obj) {
	player = document.getElementById(playerID);
}

function loadNplay(file) {
	player.sendEvent('LOAD', { file: file });
	player.sendEvent('PLAY', 'true');
}

function VideoPlayer() {
	this.TargetElementId = null;
	this.AllowScriptAccess = 'true';
	this.AllowFullScreen = 'true';
	this.SkinUrl = true;
	this.FileUrl = null;
	this.PlayerUrl = null;
	this.PreviewImageUrl = null;
	this.Width = 100;
	this.Height = 100;
	this.PlayerObject = '';
	this.PlayerPlugins = new VideoPlayerPlugins();

	this.Init = function() {
	    playerID = this.TargetElementId + 'mpl';
	    this.PlayerObject = new SWFObject(this.PlayerUrl, playerID, this.Width, this.Height, '10');
	    this.PlayerObject.addParam('allowscriptaccess', this.AllowScriptAccess == 'true' ? 'always' : '');
	    this.PlayerObject.addParam('allowfullscreen', this.AllowFullScreen);
	    this.PlayerObject.addParam("wmode", "transparent");
	    this.PlayerObject.addVariable('playerready', 'playerReadyCallback');
	    this.PlayerObject.addVariable('skin', this.SkinUrl);
	    var isVzaarVideo = this.FileUrl.toLowerCase().indexOf('vzaar') != -1;
	    if (isVzaarVideo) {
	        var lastSlashIndex = this.FileUrl.lastIndexOf('/');
	        this.PlayerObject.addVariable('streamer', this.FileUrl);
	        this.PlayerObject.addVariable('file', this.FileUrl.substring(lastSlashIndex));
	        this.PlayerObject.addVariable('type', 'http');
	    }
	    else {
	        this.PlayerObject.addVariable('file', this.FileUrl);
	    }
	    if (this.PreviewImageUrl == null || this.PreviewImageUrl == '') {
	        this.PreviewImageUrl = '/App_Themes/Bforex/img/preview.jpg'; //set default preview image
	    }
	    this.PlayerObject.addVariable('image', this.PreviewImageUrl);
	    this.PlayerPlugins.AddVideoPluginVariables(this.PlayerObject);
	    this.PlayerObject.write(this.TargetElementId);
	}

	this.GetDefaultValueIfEmpty = function(val, defVal) {
		return (val == '' || val == null) ? defVal : val;
	}
}

function VideoPlayerPlugins() {
	this.PlayerPlugins = new Array(); // of Plugin
	this.LastPlugin = null;

	this.AddPlugin = function(name) {
		var newPlugin = new Plugin();
		newPlugin.PluginName = name;
		this.PlayerPlugins.push(newPlugin);
		this.LastPlugin = newPlugin;
	}

	this.GetPluginDecleration = function() {
		var result = "";
		var i = 0;
		while (i < this.PlayerPlugins.length) {
			if (result != "") {
				result += ",";
			}
			result += this.PlayerPlugins[i].PluginName + "-1";
			++i;
		}
		return result;
	}

	this.AddVideoPluginVariables = function(player) {
		if (this.PlayerPlugins.length > 0) {
			var decleration = this.GetPluginDecleration();
			player.addVariable('plugins', decleration);
			var i = 0;
			while (i < this.PlayerPlugins.length) {
				this.PlayerPlugins[i].AddPlayerVariable(player);
				++i;
			}
		}
	}
}

function Plugin() {
	this.PluginName = null;
	this.PluginVariables = new Array();
	this.PluginValues = new Array();

	this.AddVariable = function(key, value) {
		this.PluginVariables.push(key);
		this.PluginValues.push(value);
	}

	this.AddPlayerVariable = function(player) {
		var i = 0;
		while (i < this.PluginVariables.length) {
			var varName = this.PluginName + '.' + this.PluginVariables[i];
			player.addVariable(varName, escape(this.PluginValues[i]));
			++i;
		}
	}
}

/***************************************** END OF VIDEO PLAYER ************************************/
