//
// Mediaplayerapi that creates either a flash media player or windows media player.
// If browser is internet explorer, windows media player will be used.
// All other browsers will use flasg.
// The forcePlayer parameter forces the mediaplayer to create a spesific player. 
//
// var Player = new NIASMediaPlayer(playerName, playerWidth, playerHeight, flashPath, forcePlayer);
//
//
// Example:
//
// <script type="text/javascript" src="js/mediaplayerapi.js"></script>
// <script type="text/javascript">
//
//  var Player = NIASMediaPlayer("MyPlayer", 300, 200, "..\flash\niasmediaplayer.swf", "");
//
//  Player.Play("hej.mp3", false, false);
// 
// </script>
function NIASMediaPlayer(playerName, playerWidth, playerHeight, flashPath, forcePlayer, path, autoplay)
{
	if(playerName == null)
		throw "NIASMediaPlayer.playerName has to be a valid string.";
		
	this._playerName = playerName;
	this._playerWidth = (playerWidth == null) ? 0 : playerWidth;
	this._playerHeight = (playerHeight == null) ? 0 : playerHeight;
	this._flashPath = (flashPath == null) ? "..//flash//niasmediaplayer.swf" : flashPath;
	this._player;
	this._useWM = false;
	this._useFlash = false;
	this._path = path;
	this._autoPlay = autoplay;

	var ForcePlayer = (forcePlayer != null) ? forcePlayer.toString().toLowerCase() : "wm";

	if (ForcePlayer == "wm")
	{
		this._useWM = true;
	}
	else if (ForcePlayer == "flash")
	{
		this._useFlash = true;
	}
	else
	{
		if (navigator.appName.indexOf("Microsoft") != -1)
		{
			this._useWM = true;	
			
		}
		else if (navigator.appName.indexOf("Netscape") != -1)
		{
			this._useFlash = true;
		}
		else if (navigator.appName.indexOf("Opera") != -1)
		{
			this._useFlash = true;
		}
		else
		{
			this._useFlash = true;
		}
	}
	this.CreatePlayer();

	this._player = this.GetPlayer();

//	if (this._path!=null) this.Play(this._path, false, false)
}


NIASMediaPlayer.prototype.GetPlayer = function()
{
	if (navigator.appName.indexOf("Microsoft") != -1)
	{
		return document.all[this._playerName];
	}
	else if (navigator.appName.indexOf("Netscape") != -1)
	{      
		return document.embeds[this._playerName];
	}
	else if (navigator.appName.indexOf("Opera") != -1)
	{
		return document[this._playerName];
	}
	else
	{
		return document.getElementById(this._playerName);
	}
}


NIASMediaPlayer.prototype.Play = function(path, autoPlay, isLooping)
{
	if (path == null)
		throw "MediaPlayerApi.js PlayInthis._player(" + path + "," + isLooping + ") failed because the path is empty. Please contact administrator.";

	var Loop = (isLooping == true) ? true : false;
		
	this.SetAutoplay(autoPlay);

	if (this._useWM == true)
	{
		if (this._player.settings != null)
			this._player.settings.setMode("loop", Loop);
		
		if (this._player.controls)
			this._player.controls.stop();

		this._player.Url = path;
	}
	else
	{
		this._player.StopMedia();
		this._player.Path(path);

		if (autoPlay != false)
		{
			this._player.PlayMedia();
		}
	}
}

NIASMediaPlayer.prototype.Pause = function()
{	 
	if (this._useWM == true)
	{
		if (this._player.controls)
			this._player.controls.pause();
		else
			this._player.pause();
	}
	else
	{

		this._player.PauseMedia();
	}
}

NIASMediaPlayer.prototype.Stop = function()
{	
	if (this._useWM == true)
	{
		if (this._player.controls)
			this._player.controls.stop();
		else
			this._player.stop();
	}
	else
	{
		this._player.StopMedia();
	}
}

NIASMediaPlayer.prototype.SetSamiLanguage = function(languageId)
{
	var LanguageId = parseInt(languageId);
		
	if (isNaN(LanguageId) == false)
	{
		if (this._useWM == true)
		{
			if (this._player.closedCaption != null)
			{
				try
				{
					this._player.closedCaption.SAMILang = parseInt(languageId);
				}
				catch(E)
				{
				}
			}
		}
		else
		{
			this._player.SetSamiLanguageId(languageId);
		}
	}
	else
	{
		throw "MediaPlayerApi.js SetSamiLanguageForDefaultMediaPlayer(" + languageId + ") failed because the languageid is not a number. Please contact administrator.";
	}
}


NIASMediaPlayer.prototype.GetPlaystate = function()
{	
	if (this._useWM == true)
	{
		return this._player.playState;
	}
	else
	{
		return this._player.GetPlaystate();
	}
}

NIASMediaPlayer.prototype.SetAutoplay = function(autoPlay)
{	
	var Auto = (autoPlay == false) ? false : true;
		
	if (this._useWM == true)
	{
		if (this._player.Settings)
			this._player.Settings.autoStart = Auto;
	}
	else
	{
		this._player.SetAutoplay(Auto);
	}
}

NIASMediaPlayer.prototype.CreatePlayer = function()
{
    var ObjectString;
	if (this._useWM)
	{
		ObjectString = "<object id=\"" + this._playerName + "\" name=\"" + this._playerName + "\" url=\"" + this._path + "\" width=\"" + this._playerWidth + "\" height=\"" + this._playerHeight + "\" classid=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\" standby=\"Loading Windows Media Player components...\" type=\"application/x-oleobject\">";
		ObjectString += "<embed id=\"" + this._playerName + "\" name=\"" + this._playerName + "\" src=\"" + this._path + "\" type=\"application/x-mplayer2\" autostart=\"" + this._autoPlay + "\" showcontrols=\"1\" filename=\"3\" showstatusbar=\"1\" enablecontextmenu=\"1\" height=\"" + this._playerHeight + "\" width=\"" + this._playerWidth + "\"  pluginspage=\"http://www.microsoft.com/Windows/Downloads/Contents/Products/mediaPlayer/\"/>";
		ObjectString += "</object>";

        var FrameName = this._playerName.replace("Player", "Frame");
        if (document.getElementById(FrameName))
        {
            document.getElementById(FrameName).innerHTML = ObjectString;
        }
        else
            document.write(ObjectString);
	}
	else if (this._useFlash)
	{
		ObjectString = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,12,36\" width=\"" + this._playerWidth + "\" height=\"" + this._playerHeight + "\" id=\"" + this._playerName + "\" name=\"" + this._playerName + "\">";
		ObjectString += "<param name=\"allowScriptAccess\" value=\"always\" />";
		ObjectString += "<param name=\"movie\" value=\"" + this._flashPath + "\" />";
		ObjectString += "<param name=\"quality\" value=\"high\" />";
		ObjectString += "<param name=\"scale\" value=\"showall\" />";
		ObjectString += "<param name=\"salign\" value=\"T\" />";
		ObjectString += "<embed id=\"" + this._playerName + "\" name=\"" + this._playerName + "\" src=\"" + this._flashPath + "\" quality=\"high\" bgcolor=\"#ffffff\" width=\"" + this._playerWidth + "\" height=\"" + this._playerHeight + "\" align=\"middle\" allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\" scale=\"showall\" salign=\"T\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />";
		ObjectString += "</object>";

        var FrameName = this._playerName.replace("Player", "Frame");
        if (document.getElementById(FrameName))
        {
            document.getElementById(FrameName).innerHTML = ObjectString;
        }
        else
            document.write(ObjectString);
	}
	else
	{
		throw "mediaplayerapi.js - Mediaplayer " + this._playerName + " was told to create an unsupported mediaplayer.";
	}

}	

