
var mElementID = "";
var mMimeType = "";
var mPayload = "";
var mCurrentSrc = "";

var mErrand="";
var mSpeaker="";
var mUpcomingSpeaker="";
var mNextErrand="";

var mVideoSource = "";
var mMediaPath = "";

var is_wm = false;
var is_real = false;
var is_qt = false;

var uAgent = navigator.userAgent.toLowerCase();

// ---------------------------------------------------------------------
// Init 
//
// This method is called from the SCRIPT.HTM page that is loaded from the
// Media Player URL script command. 
//
// URL is the SCRIPT.HTM document.location value
// ---------------------------------------------------------------------

function Init(URL)
{
	UpdateFromParameters(URL);
	UpdatePage(mElementID, mMimeType, mPayload);
}

// ---------------------------------------------------------------------
// GetParameters 
//
// This method extracts the parameters from the provided URL. 
//
// ---------------------------------------------------------------------

function GetParameters(URL)
{
	var StrArray = String(URL).split("?");
	return String(StrArray[1]);
}

// ---------------------------------------------------------------------
// UpdateFromParameters 
//
// This method retreives the parameters from the provided URL. Parameters 
// are separated by the & character. 
//
// ---------------------------------------------------------------------

function UpdateFromParameters(URL)
{
	var parameters =  GetParameters(URL);
	var StrArray = parameters.split("&");
	
	mElementID = String(StrArray[0]);
	mMimeType =  String(StrArray[1]).toLowerCase();
	mPayload =  String(StrArray[2]);	// 2004-02-17 Removed unescape
}

// ---------------------------------------------------------------------
// UpdatePage 
//
// This method updates the page depending on the provided parameters. 
//
// ElementID adresses the specific element on the page to update.
// MimeType is the elements media type.
// Payload is the actual payload.
//
// Syntax for the parameters string: ElementID&MimeType&Payload
// 
// ---------------------------------------------------------------------

function UpdatePage(ElementID, MimeType, Payload)
{
	if(ElementID == "undefined") return;
	if(MimeType == "undefined") return;
	if(Payload == "undefined") return;
	
	if(MimeType.indexOf("image/") > -1){
		SetImage(ElementID, Payload);
	}
	else if(MimeType.indexOf("text/") > -1){
		SetText(ElementID, Payload);
	}
	else{
		//Ignore this MIME type
	}
	
	
}

// ---------------------------------------------------------------------
// SetImage 
//
// This method updates an image on the page. 
//
// ElementID adresses the specific element on the page to update.
// Payload is the actual payload.
//
// ---------------------------------------------------------------------

function SetImage(ElementID, Payload)
{
	if(Payload.indexOf("http://") == -1){
		Payload = mMediaPath + Payload;
	}
	
	if(mCurrentSrc == Payload + "/" + ElementID) return;
	mCurrentSrc = Payload + "/" + ElementID;
	
	eval("document." + ElementID + ".src = \"" + Payload + "?" + new String(Math.round(Math.random() * 1000)) + "\"");

}

// ---------------------------------------------------------------------
// SetText 
//
// This method updates a text field on the page. 
//
// ElementID adresses the specific element on the page to update.
// Payload is the actual payload.
//
// ---------------------------------------------------------------------

function SetText(ElementID, Payload)
{	
	// Replace %A with line break
	var re = /%0A/g;
//	Payload = unescape(Payload.replace(re, "<br>"));
	 mErrand=unescape(mErrand.replace(re, "<br>"));
    mSpeaker=unescape(mSpeaker.replace(re, "<br>"));
    mUpcomingSpeaker=unescape(mUpcomingSpeaker.replace(re, "<br>"));
    mNextErrand=unescape(mNextErrand.replace(re, "<br>"));	
    
  if(mErrand != "undefined")  mErrand="<B>"+ mErrand;
   if(mSpeaker != "undefined")  mSpeaker="<B>"+mSpeaker;
  if(mUpcomingSpeaker != "undefined")   mUpcomingSpeaker="<B>"+mUpcomingSpeaker;
   if(mNextErrand != "undefined")  mNextErrand="<B>"+mNextErrand;
    
    
    mErrand=mErrand.replace("rende: ", "rende:</B><br>");
    mSpeaker=mSpeaker.replace("Talare: ", "Talare:</B><br>");
    mUpcomingSpeaker=mUpcomingSpeaker.replace("Kommande talare: ", "Kommande talare:</B><br>");
    mNextErrand=mNextErrand.replace("rende: ", "rende:</B><br>");
	
	var nav4Layer = eval("document." + ElementID);				// Adress your text element for Navigator 4.5 here
	
	if(nav4Layer != null){
		// Use Netscape layers to update the text 
		//nav4Layer.document.write("<font class=\"Style1\"><center>" + Payload + "</center></font>");
		//nav4Layer.document.close();
	}
	else{
		//var docElement = document.getElementById(ElementID);	// Adress your text element for other compliant browsers here
		//if(docElement != null){
			// Use innerHTML to update the text (not a W3C standard, but it works for IE and NS6.2)
		//	if(docElement.innerHTML != Payload) docElement.innerHTML = Payload;
		if(mErrand != "undefined") {
		var docElement = document.getElementById("textErrand");	// Adress your text element for other compliant browsers here
		if(docElement != null){
			// Use innerHTML to update the text (not a W3C standard, but it works for IE and NS6.2)
			if(docElement.innerHTML != mErrand) docElement.innerHTML = mErrand;
			}
			}
if(mSpeaker != "undefined") {
	var docElement2 = document.getElementById("textSpeaker");	// Adress your text element for other compliant browsers here
		if(docElement2 != null){
			// Use innerHTML to update the text (not a W3C standard, but it works for IE and NS6.2)
			if(docElement2.innerHTML != mSpeaker) docElement2.innerHTML = mSpeaker;
			}
			}
if(mUpcomingSpeaker != "undefined") {
	var docElement3 = document.getElementById("textUpcomingSpeaker");	// Adress your text element for other compliant browsers here
		if(docElement3 != null){
			// Use innerHTML to update the text (not a W3C standard, but it works for IE and NS6.2)
			if(docElement3.innerHTML != mUpcomingSpeaker) docElement3.innerHTML = mUpcomingSpeaker;
			}
			}
if(mNextErrand != "undefined") {
	var docElement4 = document.getElementById("textNextErrand");	// Adress your text element for other compliant browsers here
		if(docElement4 != null){
			// Use innerHTML to update the text (not a W3C standard, but it works for IE and NS6.2)
			if(docElement4.innerHTML != mNextErrand) docElement4.innerHTML = mNextErrand;

		}
		}
	}
	
	//Handle Special Payload
	
}

// ---------------------------------------------------------------------
// HandleCommandScript 
//
// The provided CommandScript describes the multimedia element to
// display in the browser.
//
// Syntax for the ScriptComand string: #ElementID#MimeType#Payload
//
// Windows + IE only!
//
// ---------------------------------------------------------------------

function HandleCommandScript(ScriptType, CommandScript) {
	
	if(ScriptType == "URL") return;
		
	// Turn off URL scripts
	if(document.MediaPlayer.InvokeURLs) document.MediaPlayer.InvokeURLs = false;
		
	// Parse the ScriptCommand string
	ParseCommandScript(CommandScript);
		
	// Update the element on the web page 
	UpdatePage(mElementID, mMimeType, mPayload);
}

// ---------------------------------------------------------------------
// ParseCommandScript 
//
// Updates the webpage according to the provided layout element object. 
//
// LayoutElement is a description of the multimedia element.
// ---------------------------------------------------------------------

function ParseCommandScript(ScriptCommand){

	var Attributes = ScriptCommand.split("#");
	if(Attributes == null) return; // No attributes found
	mElementID = String(Attributes[1]);
	mMimeType =  String(Attributes[2]).toLowerCase();
	mPayload =  String(Attributes[3]);
	//--Hantering för separera punkter
	if(mPayload != "undefined") 
	{
	var Payload = mPayload.split("<BR>");
	mErrand=String(Payload[0]);
	mSpeaker=String(Payload[1]);
	mUpcomingSpeaker=String(Payload[2]);
	mNextErrand=String(Payload[4]);
	}
	//else
	//{
	//mErrand="";
	//mSpeaker="";
	//mUpcomingSpeaker="";
	//mNextErrand="";
	//}
	
}

// ---------------------------------------------------------------------
// Detect from the provided VideoSource which player to use
// ---------------------------------------------------------------------

function DetectPlayerType(VideoSource)
{
	// Extract the extension from the extracted media path
	var Extension = GetExtension(VideoSource);
		
	// Extract the right type of player from the extracted extension
	if(".ram;.rm;".indexOf(Extension) > -1){
		is_real = true;
	}
	else if(".asx;.wmv;asf;avi;".indexOf(Extension) > -1){
		is_wm = true;
	}
	else if(".mov;mpg;mpeg;".indexOf(Extension) > -1){
		is_qt = true;
	}
}

// ---------------------------------------------------------------------
// Extract extension from the provided MediaPath
// ---------------------------------------------------------------------

function GetExtension(MediaPath)
{
	var nLastDot = MediaPath.lastIndexOf(".");
	if(nLastDot != -1){
		var Extension = MediaPath.substring(nLastDot+1, MediaPath.length);
		return (Extension);
	}
}

// ---------------------------------------------------------------------
// Extract parameters from the document.location variable
// ---------------------------------------------------------------------

function ParseURL(URL)
{
	var StrArray = String(URL).split("?");
	var StrArray2 = String(StrArray[1]).split("&");
	
	mVideoSource = unescape(StrArray2[0]);
	mMediaPath = unescape(StrArray2[1]);
	
	//alert("mVideoSource = " + mVideoSource);
	//alert("mMediaPath = " + mMediaPath);
}

// ---------------------------------------------------------------------
// Returns the videosource to open by the player
// ---------------------------------------------------------------------

function GetVideoSource()
{
	return mVideoSource; 


mVideoSource;
}

// ---------------------------------------------------------------------
// Invoke the player that fit the provided URL
// ---------------------------------------------------------------------

function WritePlayer(URL)
{
	ParseURL(URL);
	DetectPlayerType(GetVideoSource());
	
	if(is_real){
		// Write out the Real Player
		WriteRealPlayer(GetVideoSource());
	}
	else if(is_wm){
		// Write out the Windows Media Player
		WriteWindowsMediaPlayer(GetVideoSource());
	}
	else if(is_qt){
		// Write out the Quick Time Player
		WriteQuicktimePlayer(GetVideoSource());
	}
}

// ---------------------------------------------------------------------
// Methods for writing out embeded players
// ---------------------------------------------------------------------

function WriteWindowsMediaPlayer(VideoSource) 
{
	is_wm = true;
	
	width = 320;
    height = 240;
        
	var ObjectTag = "<OBJECT ID=\"MediaPlayer\" name=\"MediaPlayer\" WIDTH=\""+width+"\" HEIGHT=\""+height+"\" CLASSID=\"CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95\" CODEBASE=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112\" standby=\"Loading Microsoft Media Player components...\" type=\"application/x-oleobject\">";
	ObjectTag += "<PARAM NAME=\"AutoStart\" VALUE=\"1\">";
	ObjectTag += "<PARAM NAME=\"FileName\" VALUE=\"" + VideoSource + "\">";
	ObjectTag += "<PARAM NAME=\"ShowControls\" VALUE=\"1\">";
	ObjectTag += "<PARAM NAME=\"ShowStatusBar\" VALUE=\"1\">";
	ObjectTag += "<PARAM NAME=\"AutoSize\" VALUE=\"0\">";
	ObjectTag += "<PARAM NAME=\"ControlType\" VALUE=\"0\">";
	ObjectTag += "<PARAM NAME=\"ShowTracker\" VALUE=\"0\">";
	ObjectTag += "<PARAM NAME=\"InvokeURLs\" VALUE=\"1\">";
	ObjectTag += "<EMBED Type=\"video/x-ms-asf-plugin\" pluginspage=\"http://www.microsoft.com/windows/mediaplayer/download/default.asp\" src=\"" + VideoSource + "\" name=\"MediaPlayer\" AutoStart=\"1\" ShowControls=\"0\" AutoSize=\"0\" width=\""+width+"\" height=\""+height+"\" DefaultFrame=\"script\" ShowTracker=\"0\">";
	ObjectTag += "</OBJECT>";



	document.write(ObjectTag);
}

function WriteRealPlayer(VideoSource) 
{	
	is_real = true;
	
	width = 320;
	height = 240;
	
	var ObjectTag = "<OBJECT ID=\"MediaPlayer\" CLASSID=\"clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA\" WIDTH=\""+width+"\" HEIGHT=\""+height+"\">";
	ObjectTag +="<PARAM NAME=\"SRC\" VALUE=\"" + VideoSource + "\">";
	ObjectTag +="<PARAM NAME=\"CONTROLS\" VALUE=\"Imagewindow\">";
	ObjectTag +="<PARAM NAME=\"AUTOSTART\" VALUE=\"TRUE\">";
	ObjectTag +="<PARAM NAME=\"NOLABELS\" VALUE=\"TRUE\">";
	ObjectTag +="<PARAM NAME=\"RESET\" VALUE=\"FALSE\">";
	ObjectTag +="<PARAM NAME=\"CONSOLE\" VALUE=\"console1\">";
	ObjectTag +="<PARAM NAME=\"AUTOGOTOURL\" VALUE=\"TRUE\">";
	ObjectTag +="<embed name=\"MediaPlayer\" CONSOLE=\"console1\" type=\"audio/x-pn-realaudio-plugin\" src=\"" + VideoSource + "\" width=\""+width+"\" height=\""+height+"\" controls=\"Imagewindow\" autostart=\"true\" nolabels=\"true\">";
	ObjectTag +="</OBJECT><br>";
	
	document.write(ObjectTag);
}

function WriteQuicktimePlayer(VideoSource)		// Must use player controls. Cannot use custom controls.
{	
	
	is_qt = true;
	
	width = 320;
    height = 240 + 16;
    
	var ObjectTag = "<OBJECT id=\"MediaPlayer\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\" height=\"" + height + "\" width=\"" + width + "\" classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\">";
	ObjectTag += "<param name=\"src\" value=\"" + VideoSource + "\">";
	ObjectTag += "<param name=\"CONTROLLER\" value=\"TRUE\">";
	ObjectTag += "<param name=\"SCALE\" value=\"ASPECT\">";
	ObjectTag += "<EMBED NAME=\"MediaPlayer\" SCALE=\"ASPECT\" BORDER=\"0\" CONTROLLER=\"true\" TYPE=\"video/quicktime\" src=\""+VideoSource+"\" WIDTH=\""+width+"\" HEIGHT=\""+height+"\" AUTOPLAY=\"true\" BGCOLOR=\"#FFFFFF\" PLUGINSPAGE=\"http://www.apple.com/quicktime/download/\">";
	ObjectTag += "</OBJECT>";
	
	document.write(ObjectTag);
}	

//--> 
