//<!--
// ---------------------------------------------------------------------
// Compatible with StreamSync 2.0. 2006-11-27.
// Please notice. This code is provided only as an example of how to
// layout and program a video player for use with StreamSync.
// ---------------------------------------------------------------------

// ---------------------------------------------------------------------
//	These are the data that you might want to change.

//	width = the width of your videoplayer (Wmp)
//	height = the height of your videoplayer (Wmp)
//	ShowControls = 1 means you want the view the standardbuttons on your player (play/pause, volume etc).
//		If you set this to 1 you will have to add 27 to height.
//	ShowStatusBar = 1 means you want the view the statusbar on your player (shows info about the stream).
//		If you set this to 1 you will have to add 23 to height.
//	ShowTracker = This can only be set together with ShowControls. 1 means you want the view the tracker on your player (enables to scroll in a ondemand stream).
//		If you set this to 1 you will have to add 19 to height.
// ---------------------------------------------------------------------

var VideoWidth = 320;
var VideoHeight = 309;
var ShowControls = 1;
var ShowStatusBar = 1;
var ShowTracker = 1;


// ---------------------------------------------------------------------


var mElementID = "";
var mMimeType = "";
var mPayload = "";
var mCurrentImgSrc = "";
var mCurrentFlashMovie = "";

var mVideoSource = "";
var mMediaPath = "";

var is_wm = false;
var is_real = false;

var flash_active = false;
var uAgent = navigator.userAgent.toLowerCase();

// ---------------------------------------------------------------------
// 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);
}

// ---------------------------------------------------------------------
// 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

	//alert("parameters=\"" + parameters +"\"\nmElementID=\"" + mElementID +"\"\nmMimeType=\"" + mMimeType +"\"\nmPayload=\"" + mPayload +"\"\n");
}

// ---------------------------------------------------------------------
// 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);		// This script uses the IMAGE tag to show pictures. 2006-11-27
	}
	else if(MimeType.indexOf("text/url") > -1){	
		SetUrlLink(ElementID, Payload);
	}
	else if(MimeType.indexOf("text/") > -1){
		SetText(ElementID, Payload);
	}
	else if(MimeType.indexOf("application/x-shockwave-flash") > -1){
		PlayFlashMovie(ElementID, Payload);
	}
	else{
		//Ignore this MIME type
	}
	
}

// ---------------------------------------------------------------------
// StopFlashMovie 
//
// This method stops the playback of any current flash animation. 
//
// ---------------------------------------------------------------------

function StopFlashMovie()
{
	// Hide the Flash player if it is active
	if(flash_active == true){
		// Play a non existent movie to stop the playback
		PlayFlashMovie("flash", "bogus.swf");
		// Make the DIV hidden
		flashDiv.style.visibility = "hidden";
		// Flash is not currently active
		flash_active = false;
	}
}

// ---------------------------------------------------------------------
// PlayFlashMovie 
//
// This method start the playback of a flash animation. 
//
// ElementID is not used.
// Payload is the URL pointing to the flash animation.
//
// ---------------------------------------------------------------------

function PlayFlashMovie(ElementID, Payload)
{
	if(Payload.indexOf("http://") == -1){
		Payload = mMediaPath + Payload;
	}
	
	// Do not restart the movie if it is already playing
	if(mCurrentFlashMovie == Payload) return;
	mCurrentFlashMovie = Payload;
	
	WriteFlashPlayer(Payload);	
}

// ---------------------------------------------------------------------
// 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)
{

	StopFlashMovie();	// Stop any playing Flash movie
	
	if(Payload.indexOf("http://") == -1){
		Payload = mMediaPath + Payload;
	}
	
	// Do not reload the image if it is already displayed
	if(mCurrentImgSrc == Payload + "/" + ElementID) return;
	mCurrentImgSrc = 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>"));
	
	var docElement = document.getElementById(ElementID);	// Adress your text element for other compliant browsers here	
	if(docElement == null) return;
	
	// Use innerHTML to update the text 
	if(docElement.innerHTML != Payload) docElement.innerHTML = Payload;
}

// ---------------------------------------------------------------------
// SetUrlLink 
// Syntax: href+target+name
// 2005-11-30: Handling text/url Mime type is not stable.
// ---------------------------------------------------------------------

function SetUrlLink(ElementID, Payload)
{
	// Split the Payload	
	var AnchorParts = Payload.split("+");
	if(AnchorParts == null) return; // No AnchorParts found

	var docElement = document.getElementById(ElementID);	// Adress your text element for other compliant browsers here
	if(docElement == null) return;
	
	var strAnchor = "<a href=" + unescape(AnchorParts[0]) + " target=" + AnchorParts[1] + ">" + unescape(AnchorParts[2]) + "</a>"

	// Use innerHTML to update the text 
	if(docElement.innerHTML != strAnchor) docElement.innerHTML = strAnchor;
}

// ---------------------------------------------------------------------
// 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]);
}

// ---------------------------------------------------------------------
// 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(VideoSource.substring(0,3)=="mms"){
		is_wm = 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("&");
	docLocation = unescape(StrArray[0]);
	mVideoSource = unescape(StrArray2[0]);
	mMediaPath = unescape(StrArray2[1]);

}

// ---------------------------------------------------------------------
// Returns the videosource to open by the player
// ---------------------------------------------------------------------

function GetVideoSource()
{
	return mVideoSource;
}

// ---------------------------------------------------------------------
// Invoke the player that fit the provided URL
// ---------------------------------------------------------------------

function WritePlayer(URL)
{

	ParseURL(URL);
	DetectPlayerType(GetVideoSource());
	
	if(is_real){
		// Write out the Real Player
		WriteRealPlayer2(GetVideoSource());
	}
	else if(is_wm){
		// Write out the Windows Media Player
		WriteWindowsMediaPlayer2(GetVideoSource());
	}
}

// ---------------------------------------------------------------------
// Methods for writing out embeded players
// ---------------------------------------------------------------------

function WriteWindowsMediaPlayer2(VideoSource) {
   
   // add extra height values depending of extra functions
    if (ShowControls == 1) { VideoHeight += 27; }
    if (ShowStatusBar == 1) { VideoHeight += 23; }
    if (ShowTracker == 1) { VideoHeight += 19; }

	is_wm = true;
	
	var ObjectTag = "<OBJECT ID=\"MediaPlayer\" name=\"MediaPlayer\" WIDTH=\""+VideoWidth+"\" HEIGHT=\""+VideoHeight+"\" CLASSID=\"CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95\" CODEBASE=\"http://www.microsoft.com/ntserver/netshow/download/en/nsmp2inf.cab#Version=5,1,51,415\" 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=\"" + ShowControls + "\">";
	ObjectTag += "<PARAM NAME=\"ShowStatusBar\" VALUE=\"" + ShowStatusBar + "\">";
	ObjectTag += "<PARAM NAME=\"AutoSize\" VALUE=\"0\">";
	ObjectTag += "<PARAM NAME=\"ControlType\" VALUE=\"0\">";
	ObjectTag += "<PARAM NAME=\"ShowTracker\" VALUE=\"" + ShowTracker + "\">";
	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=\"" + ShowControls + "\" ShowStatusBar=\"" + ShowStatusBar + "\" AutoSize=\"0\" width=\""+VideoWidth+"\" height=\""+VideoHeight+"\" DefaultFrame=\"script\" ShowTracker=\"" + ShowTracker + "\">";
	ObjectTag += "</OBJECT>";

	//videoDiv.innerHTML = ObjectTag;
	document.write(ObjectTag);
}

function WriteRealPlayer2(VideoSource) 
{	
	is_real = true;

	var ObjectTag = "<OBJECT ID=\"MediaPlayer\" CLASSID=\"clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA\" WIDTH=\""+VideoWidth+"\" HEIGHT=\""+VideoHeight+"\">";
	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=\""+VideoWidth+"\" height=\""+VideoHeight+"\" controls=\"Imagewindow\" autostart=\"true\" nolabels=\"true\">";
	ObjectTag +="</OBJECT><br>";
	
	//videoDiv.innerHTML = ObjectTag;
	document.write(ObjectTag);
}

function WriteFlashPlayer(FlashSource) 
{
	flash_active = true;
	
	FlashWidth = 400;
    FlashHeight = 300;
    
	var ObjectTag = "<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" ";
	ObjectTag += "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\" ";
	ObjectTag += "WIDTH=\"" + FlashWidth + "\" HEIGHT=\"" + FlashHeight + "\" id=\"FlashPlayer\"> ";
	ObjectTag += "<PARAM NAME=movie VALUE=\"" + FlashSource + "\"> ";
	ObjectTag += "<PARAM NAME=quality VALUE=high> ";
	ObjectTag += "<PARAM NAME=bgcolor VALUE=#FFFFFF> "; // 8C8C79
	ObjectTag += "<EMBED src=\"" + FlashSource + "\" quality=high bgcolor=#FFFFFF WIDTH=\"" + FlashWidth + "\" HEIGHT=\"" + FlashHeight + "\" ";
	ObjectTag += "NAME=\"FlashPlayer\" ALIGN=\"\" TYPE=\"application/x-shockwave-flash\" ";
	ObjectTag += "PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\"> ";
	ObjectTag += "</EMBED> ";
	ObjectTag += "</OBJECT> ";

	flashDiv.innerHTML = ObjectTag;
	
	flashDiv.style.visibility = "VISIBLE";
}


//--> 
