//
// trustcast_clips.js : Support File for JavaScipt to TrustCast communication
//
// Copyright (c) 2003  Trusted Media Networks, Inc. All rights reserved.
//
// Version: 1.0.0.139
//
//
// Modifications:
//			24-Mar-2004 mrainvil
//			Added filename param and member var to clip constructor


// Debug output support.

var g_DebugNever = 4;
var g_DebugError = 3;
var g_DebugWarn = 2;
var g_DebugInfo = 1;
var g_DebugLevel = g_DebugNever;

function DebugOut(level, str) {
	if (level >= g_DebugLevel )
	{
		alert(str);
	}
}

// Clip object.

function clip(title, description,img,file_ref,filename,length,link) {
	this.link = link;
	this.length = length;
	this.filename = filename;
	this.file_ref = file_ref;
	this.title = title;
	this.description = description;
	this.image = img;
	this.pri_path = "uninitialized";
	this.GetPath = clipGetPath;
	this.Play = clipPlay;
	DebugOut(g_DebugInfo, 'Created clip:\n' + 'Title: ' + this.title + '\nDescription: ' + this.description + '\nImage: ' + this.image);
}

function clipGetPath() {
	if (this.pri_path == 'uninitialized') {
		if  (TrustCastAvailable())
		{
			this.pri_path = g_TrustCastPlugin.GetClipPath(window, this.file_ref)
		}
		else {
			DebugOut(g_DebugWarn,'TrustCast unavailable in clipGetPath()');
			this.pri_path = "";
		}
		if(this.pri_path != "")
		{
			//this.pri_path = this.pri_path.replace(/\\/g, "\\\\");
			this.pri_path = "file://" + this.pri_path;
		}
		else {
			this.pri_path=""; //"TrustCast available, but path not found." + this.file_ref
		}
	}
	//DebugOut(g_DebugInfo,'GetPath() returning: ' + this.pri_path);
	return this.pri_path;
}


function clipPlay( strPlayer) {
	var path = "";

	if (!strPlayer)
	{
		DebugOut(g_DebugWarn,"Attempting clipPlay() with null player name.");
	}
	if (this && this.GetPath) {
		path = this.GetPath();
	}
	else {
		DebugOut(g_DebugWarn,"Attempting clipPlay() with null path or obj:" + clip);
	}

	if (path != "")
	{
		if(document.getElementById)
		{
			var playah = document.getElementById(strPlayer);
			if (playah) {
				if (typeof(playah.SetSource) != 'undefined') // Used for RealPlayer
				{
					playah.SetSource(path);
				}
				else // Assume WM
				{
					playah.url = path;
				}
			}
			else {
				DebugOut(g_DebugWarn,"Cannot find player named [" + strPlayer + "] in clipPlay().");
			}
		}
		else {
			DebugOut(g_DebugWarn,"No getElByID in clipPlay().");
		}
	}
}
	


var g_TrustCastPlugin = null;


// Check for install completion without actually starting
//  TrustCast.
function TrustCastInstalled() {

	var iRet = 0;

	try	{
		var ape = new ActiveXObject("TrustCast.Query");

		if(ape.isRunning()) {
			//alert("Installed and running");
			iRet = 2;
		}
		else {
			//alert("Installed but not running");
			iRet = 1;
		}
	}
	catch(e) {
		iRet = 0;
	}
	DebugOut(g_DebugInfo, 'TrustCastInstalled() returning: ' + iRet);
	return iRet;
}



function TrustCastAvailable() {
	if (! g_TrustCastPlugin) {
		try {
			g_TrustCastPlugin = new TrustCastPlugin();
		}
		catch(e) // Error Trap
		{
			DebugOut(g_DebugError, 'Exception creating TrustCast Object in TrustCastAvailable(): ' + e.description);
		}
	}
	if (g_TrustCastPlugin)
	{
		return g_TrustCastPlugin.Available();
	}
	else
		return false;
}

// Takes an array of clips and returns
//  true if at least one of the clips is available.
//
function AnyClipAvailable (aClips) {
	var txt = "";
	if (TrustCastAvailable()) {
		if ((typeof aClips == 'undefined') || !aClips) {
			return false;
		}
		for (var i = 0; i < aClips.length; i++) {
			if ("" != aClips[i].GetPath()) {
				return true;
			}
		}
	}
	return false;
}


function TrustCastCheck() {

	var txt = "";

	if (!TrustCastAvailable()) {
		txt= g_NoPluginText;
	}
	else {
		if (!TrustCastClipsAvailable()) {
			txt = g_NoContentText;
		}
	}

	if (txt) {
		DebugOut(g_DebugInfo,"Error text in TrustCastCheck():" + txt);
		SetBGImage(g_BGImage);

		var dv = document.getElementById("vid_div");
		if (dv) {
			dv.innerHTML = txt;
		}
		else { 
			DebugOut(g_DebugWarn,"Unable to find vid_div in TrustCastCheck().");
		}
	}
	return ("" == txt);
}

function SetBGImage(image_name) {
	return;
}


function TrustCastPlugin(sRegScript) {  // Constructor

	this.sRegScript = sRegScript;
	this.oTC = null;
	this.sInterface = "";
	this.Available = tcpAvailable;
	this.GetClipPath = tcpGetClipPath;
	this.IsUserSignedUp = tcpIsUserSignedUp;
	this.GetInfoString = tcpGetInfoString;
	this.GetWebID = tcpGetWebID;
	this.GetVersion = tcpGetVersion;
	this.UpgradeRequired = tcpUpgradeRequired;
	this.SignUpUser = tcpSignUpUser;
	this.AddList = tcpAddList;
	this.AddServer = tcpAddServer;
	this.CheckContent = tcpCheckContent;
	this.CreateObj = tcpCreateObj;

	this.CreateObj();
}

function tcpCreateObj() {
	try
	{
		this.oTC = new ActiveXObject("Trustcast.TMNPlugin");
		if(this.oTC != null)
		{
			this.sInterface = "Trustcast.TMNPlugin";
			DebugOut(g_DebugInfo, 'Created TrustCast ActiveXObject using TMNPlugin interface');
		}
	}
	catch(e)
	{
		DebugOut(g_DebugInfo, 'Exception creating TMNPlugin object in TrustCastPlugin(): ' + e.message);
	}
	if (this.oTC == null)
	{
		try
		{
			this.oTC = new ActiveXObject("TrustCast.IDRetrievalPlugin");
			if(this.oTC != null)
			{
				this.sInterface = "TrustCast.IDRetrievalPlugin";
				DebugOut(g_DebugInfo, 'Created TrustCast ActiveXObject using IDRetrievalPlugin interface');
			}
		}
		catch (e)
		{
				DebugOut(g_DebugWarn, 'Unable to create object in TrustCastPlugin(): ' + e.message);
		}
	}
}

function tcpGetInfoString() {

	var sRet = "";

	if (this.sInterface == "")
	{
		this.CreateObj();
	}

	if (this.sInterface == "TrustCast.IDRetrievalPlugin")
	{
		sRet = this.oTC.getWebID(this.sRegScript);
	}
	else
	{
		sRet = this.oTC.getWebID(window,this.sRegScript);
	}

	return sRet;
}

function tcpGetWebID() {
	
	var s2=this.GetInfoString().split('_');
	return(s2[0]);
}

function tcpGetVersion() {
	var s2=this.GetInfoString().split('_');
	return(s2[1]);
}

function tcpVerToFloat(sVer) {
	return parseFloat(sVer.substr(5));
}

// Return true if this client is older than the
// version specified.
function tcpUpgradeRequired(sMinVersion) {
	if (tcpVerToFloat(this.GetVersion()) < tcpVerToFloat(sMinVersion))
	{
		return true;
	}
	else
	{
		return false;
	}
}
function tcpSignUpUser(sListName, sListDesc, sListID, sServerDesc , sAddURL, sSignedURL, sEmail)
{
	var bRet = true;

	if (this.GetWebID() == 'UnknownServerID')
	{
		var sAdd = this.AddServer(sServerDesc, sListDesc, sEmail, sSignedURL, sListID);
		if (sAdd != 'success') {
			DebugOut(g_DebugWarn, 'AddServer did not return success: [' + sAdd + ']');
			bRet = false;
		}
	}
	else {
		this.AddList(sAddURL, sListID);
		this.CheckContent(4); //4-sec delay
	}
	return bRet
}

function tcpAddServer(s_server, s_issue, s_email, s_signedURL, s_product)
{
	var res = '';

	if (s_server == '') {res = 'Null server name in tcpAddServer().';}
	if (s_issue == '') {res = 'Null issue name in tcpAddServer().';}
	if (s_email == '') {s_email = 'portal';}
	if (s_signedURL == '') {res = 'Null signedURL in tcpAddServer().';}
	if (s_product == '')	{res = 'Null product key in tcpAddServer().';}

	if (res != '')
	{
		DebugOut(g_DebugError, 'Input error in tcpAddServer: ' + res);
	}
	else
	{
		if (typeof(this.oTC.addServer) != 'undefined')
		{
			try
			{
				DebugOut(g_DebugInfo, 'Calling addServer() with s_server=' + s_server + ' s_signedURL=' + s_signedURL + ' s_email=' + s_email + ' s_issue=' + s_issue + ' s_product=' + s_product);
				res = g_TrustCastPlugin.oTC.addServer(window,s_server,s_signedURL,s_email,s_issue,s_product);
				if (res != 'success')
				{
					DebugOut(g_DebugError, 'addServer() call failed.  Msg: [' + res + ']');
				}
			}
			catch(e) // Error Trap
			{
				DebugOut(g_DebugError, 'Exception adding server: ' + e.description);
			}

		}
		else
		{
			DebugOut(g_DebugError, 'Error in tcpAddServer: addServer is undefined.');
		}
	}
	return res;
}

function tcpAddList(sReg, sListID) {
	var sWID = this.GetWebID();
	var sURL = sReg + '?W=' + sWID + '&P=' +sListID;

	var i = new Image();

	DebugOut(g_DebugInfo, 'AddList() now hitting: [' + sURL + ']');

	try {
		i.src = sURL;
	}
	catch (e) {
		DebugOut(g_DebugInfo, 'Exception setting img src in tcpAddList: ' + e.description);
	}
}

function tcpCheckContent( iDelay) {
	if (typeof iDelay == 'undefined' || iDelay == '')
	{
		iDelay = 0;
	}
	return this.oTC.doWake(window, this.sRegScript, iDelay);
}


function tcpAvailable() {
	if (this.oTC == null)
	{
		this.CreateObj();
	}
	return (this.oTC != null);
}

function tcpGetClipPath(window, sFileRef) {
	var sRet = "";

	if (this.oTC != null)
	{
		if (typeof(this.oTC.getContentPath) != 'undefined')
		{
			sRet = this.oTC.getContentPath(window, sFileRef)
			DebugOut(g_DebugInfo,'getContentPath() returned [' + sRet + '] for [' + sFileRef + ']');
		}
		else
		{
			if (typeof(this.oTC.getIssuePath) != 'undefined')
			{
				sRet = this.oTC.getIssuePath(window, sFileRef)
				DebugOut(g_DebugInfo,'getIssuePath() returned [' + sRet + '] for [' + sFileRef + ']');
			}
			else
			{
				DebugOut(g_DebugError,'Unable to retrieve path with either interface in tcpGetClipPath()');
			}
		}
	}
	else
	{
		DebugOut(g_DebugWarn,'TC object is null in tcpGetClipPath();');
	}
	return sRet;
}

function tcpIsUserSignedUp (sProvider, sList) {
	if (this.Available())
	{
		if ("" == sProvider || "" == sList)
		{
			DebugOut(g_DebugWarn,'UserSignedUp() called with empty provider or list name.');
			return false;
		}
		if ("" == this.sRegScript)
		{
			DebugOut(g_DebugWarn,'UserSignedUp() called, but plugin object was created without server reg string.');
			return false;
		}
		if (typeof(this.oTC.checkSubscription) != 'undefined')
		{
			return ("" != this.oTC.checkSubscription(window,this.sRegScript,sProvider,sList));
		}
	}
	return false;
}


var g_MinRealVersion = 8; // The number for RP 8

function RealAvailable() {
	var oReal = null;
	var minVer = 0;

	if (0 == arguments.length)
		minVer = g_MinRealVersion;
	else
		minVer = arguments[0];

	try {
		oReal = new ActiveXObject("rmocx.RealPlayer G2 Control");
	}
	catch(e) // Error Trap
	{
		DebugOut(g_DebugError, 'Exception creating RealPlayer ActiveX Object: ' + e.description);
	}
	if (null == oReal)
		return false;
	try
	{
		if (ParseRealVersion(oReal.GetVersionInfo()) >= minVer)
		{
			return true;
		}
	}
	catch(e) // Error Trap
	{
		DebugOut(g_DebugError, 'Exception calling GetVersionInfo() for RealPlayer ActiveX Object: ' + e.description);
	}
	return false;
}

function ParseRealVersion (strVer) {
	var ray = strVer.split('.');
	if (ray.length >= 4) {
		return ray[2];
	}
	return 0;
}


g_NoRealPlayerText = "This page requires RealPlayer 8.0 or later.  To download the latest version of RealPlayer, visit <a href='http://www.real.com/'>www.real.com</a>.";

function RealCheck() {

	var txt = "";

	if (!RealAvailable()) {
		txt= g_NoRealPlayerText;
	}
	if (txt) {
		DebugOut(g_DebugInfo,"Error text in RealCheck():" + txt);
		SetBGImage(g_BGImage);

		var dv = document.getElementById("vid_div");
		if (dv) {
			dv.innerHTML = txt;
		}
		else { 
			DebugOut(g_DebugWarn,"Unable to find vid_div in RealCheck().");
		}
	}

	return ("" == txt);
}

function chkBrowser() {
		var agt=navigator.userAgent.toLowerCase();
		var is_major = parseInt(navigator.appVersion);
		var is_minor = parseFloat(navigator.appVersion);
		var is_win32 = (agt.indexOf("win95")!=-1) || (agt.indexOf("windows 95")!=-1) ||
		(agt.indexOf("win98")!=-1) || (agt.indexOf("windows 98")!=-1) ||
		(agt.indexOf("winnt")!=-1) || (agt.indexOf("windows nt")!=-1) ||
		(agt.indexOf("wince")!=-1) || (agt.indexOf("windows ce")!=-1) ||
		((is_major >= 4) && (navigator.platform == "Win32")) ||
		(agt.indexOf("win32")!=-1) || (agt.indexOf("32bit")!=-1);

		result = 'needBrwsr';

		if (is_win32 == false) return 'needOS';

		if (agt.indexOf("msie") != -1)
				if (is_major < 4) return 'ie3';
				else if (is_major >= 4) return 'ie4up';

		// Note: Opera and WebTV spoof Navigator.
		var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
								&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
								&& (agt.indexOf('webtv')==-1));

		if (is_nav == true)
				if  (is_minor >= 5.00)
								return 'nav600up';
				if  (is_minor >= 4.06)
								return 'nav406up';
		return result;
}


function IsSupportedPlatform() {
	var txt = chkBrowser();
	// for now, Navigator variants and non-Win platforms are out...
	if (txt.indexOf('need') != -1 || txt.indexOf('nav') != -1)
		return false;
	else
		return true;
}


//
// Denegrated functions...
//

function PlayFirstClip() {
	if ((typeof g_playlist != 'undefined') && (g_playlist.GetPath() != "")) {
		Play (g_playlist);
	}
	else {
		for (var i=0; i < g_clips.length; i++) {
			if (g_clips[i].GetPath() != "") {
				PlayTrustCast(g_clips[i]);
				break;
			}
		}
	}
}

var g_strPlayerName = "Player";

function PlayTrustCast(clip)
{
	var path = "";

	if (clip && clip.GetPath) {
		path = clip.GetPath();
	}
	else {
		DebugOut(g_DebugWarn,"Attempting PlayTrustCast() with null path or obj:" + clip);
	}
	if (path != "") {
		if(document.getElementById)
		{
			var playah = document.getElementById(g_strPlayerName);
			if (playah) {
				if (playah.SetSource) // Used for RealPlayer
				{
					playah.SetSource(path);
				}
				else // Assume WM
				{
					playah.url = path;
				}
				playah.style.visibility = "visible";
				var backg = document.getElementById("bgImage");
				if (backg)
				{
					backg.background = "";
				}
				else {
					DebugOut(g_DebugInfo,"No bgImage in PlayTrustCast().");
				}
			}
			else {
				DebugOut(g_DebugWarn,"No Playah in PlayTrustCast().");
			}
		}
		else {
			DebugOut(g_DebugWarn,"No getElByID in PlayTrustCast().");
		}
	}
}


// Returns true if at least one of the clips is available.
function TrustCastClipsAvailable() {
	var txt = "";
	if (TrustCastAvailable()) {
		if ((typeof g_playlist != 'undefined') && ("" !=g_playlist.GetPath())) {
			return true;
		}
		if (typeof g_clips != 'undefined') {
			for (var i = 0; i < g_clips.length; i++) {
				if ("" != g_clips[i].GetPath()) {
					return true;
				}
			}
		}
	}
	return false;
}

