/*
	Media Player Manager Class
	last updated 2009-08-07
*/

function MediaPlayerManager(mediaPlayer, viewingID) {
	
	// member variables/constructor
	this.mediaPlayer = mediaPlayer;
	this.isWM9 = (this.mediaPlayer && this.mediaPlayer.enabled && this.mediaPlayer.controls!=null);
	this.isWM = this.isWM9 || (this.mediaPlayer && this.mediaPlayer.Enabled);
	this.isFlash = swfLoaded; // depends on eyflashplayerapi.js
	if (!this.isWM && !this.isFlash) {
		try {
			this.isRM = this.mediaPlayer.GetPlayState()>=0;
		} catch (e) {
			this.isRM = false;
		}
	}
	this.isIE = navigator.userAgent.indexOf("MSIE")>=0;
	this.playedSuccessfully = false;
	this.statusLog = null;
	this.viewingID = viewingID;
	this.testForProgramID = 0;
	this.eventLogStopped = false;
	this.statusLogID = 1;
	this.duration = 0;
	this.timerStart = 0;
	this.timerStop = 0;
	this.timerIsOn = false;
	this.playlist = null;
	
	// Real Player initialization
		if (this.isRM) this.mediaPlayer.SetWantErrors(true);
	
	// private constants
		var EVENT_CONNECTING = 1;
		var EVENT_BUFFERING = 2;
		var EVENT_PLAYING = 3;
		var EVENT_PAUSED = 4;
		var EVENT_STOPPED = 5;
		var EVENT_ERROR = 6;
		var EVENT_SCRIPT = 7;
		var EVENT_PAGE_SEND = 8;
		var EVENT_VOLUME_CHANGE = 9;
		var EVENT_MUTE = 10;
		var EVENT_FULL_SCREEN = 11;
		var EVENT_CLOSED = 12;
		var EVENT_LOADING_ASX = 13;
		var EVENT_LOADING_NSC = 14;
		var EVENT_LOCATING_SERVER = 15;
		var EVENT_OPENING = 16;
		var EVENT_OPEN = 17;
		var EVENT_BEGIN_PLAYING = 18;
		var EVENT_WAITING = 19;
		var EVENT_CONTACTING = 20;
		var EVENT_SEEKING = 21;	
		var EVENT_WARNING = 22;
		var EVENT_PRESS_PLAY = 23;
		var EVENT_PRESS_PAUSE = 24;
		var EVENT_PRESS_STOP = 25;
		var EVENT_GO_TO_MARKER = 26;
		var EVENT_START_TIMER = 27;
		var EVENT_STOP_TIMER = 28;
		var EVENT_GO_TO_CLIP = 29;
	
	// action methods
	this.open = function(streamURL) {
		if (this.isWM) {
			if (this.isWM9) {
				this.mediaPlayer.URL = streamURL;
			} else {
				this.mediaPlayer.Open(streamURL);
			}
			this.addEventToLog(EVENT_OPEN, null, "<url>"+streamURL+"</url>");
		} else if (this.isRM) {
			this.mediaPlayer.SetSource(streamURL);
			this.addEventToLog(EVENT_OPEN, null, "<url>"+streamURL+"</url>");
		}
	}
	this.play = function() {
		if (this.mediaPlayer && !this.isPlaying()) {
			if (this.isWM9) this.mediaPlayer.controls.play();
			else if (this.WM) this.mediaPlayer.Play();
			else if (this.isRM && this.mediaPlayer.CanPlay()) this.mediaPlayer.DoPlay();
			this.addEventToLog(EVENT_PRESS_PLAY);
			this.playingBegins();
		}
	}
	this.pause = function() {
		if (this.mediaPlayer && this.isPlaying()) {
			if (this.isWM9) this.mediaPlayer.controls.pause();
			else if (this.WM) this.mediaPlayer.Pause();
			else if (this.isRM && this.mediaPlayer.CanPause()) this.mediaPlayer.DoPause();
			this.addEventToLog(EVENT_PRESS_PAUSE);
			this.stopTimer();
		}
	}
	this.stop = function() {
		if (this.mediaPlayer && this.isPlaying()) {
			if (this.isWM9) {
				this.mediaPlayer.controls.stop();
				this.mediaPlayer.controls.currentPosition = 0;
			} else if (this.WM) {
				this.mediaPlayer.Stop();
				this.mediaPlayer.CurrentPosition = 0;
			} else if (this.isRM && this.mediaPlayer.CanStop()) this.mediaPlayer.DoStop();
			this.addEventToLog(EVENT_PRESS_STOP);
			this.stopTimer();
		}
	}
	this.goToMarkerNumber = function(markerNumber) {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				if (this.mediaPlayer.controls.isAvailable("currentMarker")) {
					if (this.getMarkerCount()>=markerNumber && markerNumber>0) {
						this.addEventToLog(EVENT_GO_TO_MARKER, markerNumber);
						this.mediaPlayer.controls.currentMarker = markerNumber;
					} else this.mediaPlayer.controls.currentPosition = 0;
				}
			} else if (this.isWM && this.isIE) {
				if (this.mediaPlayer.CanSeekToMarkers && this.getMarkerCount()>=markerNumber) {
					this.addEventToLog(EVENT_GO_TO_MARKER, markerNumber);
					this.mediaPlayer.CurrentMarker = markerNumber;
				}
			} else if (this.isRM) {
				if (this.mediaPlayer.GetCanSeekToMarkers() && this.getMarkerCount()>=markerNumber) {
					this.addEventToLog(EVENT_GO_TO_MARKER, markerNumber);
					this.mediaPlayer.SetCurrentMarker(markerNumber);
				}
			}
			this.playingBegins();
		}
	}
	// go to a clip number within the playlist; return true if it is different than the currently-playing clip
	this.goToClipNumber = function(clipNumber) {
		var clipChanged = false;
		try {
			if (this.mediaPlayer) {
				if (this.isWM9) {
					if (this.playlist!=null && clipNumber>0 && clipNumber<=this.playlist.length) {
						if (!this.mediaPlayer.currentMedia.isIdentical(this.playlist[clipNumber-1])) {
							this.addEventToLog(EVENT_GO_TO_CLIP, clipNumber);
							this.mediaPlayer.currentMedia = this.playlist[clipNumber-1];
							clipChanged = true;
						}
					}
				} else if (this.isWM && this.isIE) {
				} else if (this.isRM) {
				}
				this.playingBegins();
			}
		} catch (e) { }
		return(clipChanged);
	}
	// toggle media between playing and paused/stopped
	// and return name for new state: play, pause, or stop
	this.togglePlay = function() {
		if (this.isPlaying()) {
			if (this.canPause()) {
				this.pause();
				return("pause");
			} else {
				this.stop();
				return("stop");
			}
		} else {
			this.play();
			return("play");
		}
	}
	// toggle volume between loud, quiet, and mute states
	this.toggleVolume = function() {
		var volume = this.getVolume();
		if (volume==100) {
			this.setVolume(60);
			return("medium");
		} else if (volume==60) {
			this.setVolume(30);
			return("soft");
		} else if (volume==30) {
			this.setVolume(0);
			return("mute");
		} else if (volume==0) {
			this.setVolume(100);
			return("loud");
		}
	}
	this.setVolumeByName = function(volumeNickname) {
		switch (volumeNickname) {
			case "loud": this.setVolume(100); break;
			case "medium": this.setVolume(60); break;
			case "soft": this.setVolume(30); break;
			case "muted": this.setVolume(0); break;
		}
	}
	this.volumeUp = function() {
		this.setVolume(this.getVolume()+10);
	}
	this.volumeDown = function() {
		this.setVolume(this.getVolume()-10);
	}
	// set the volume using a number from 0 to 100 (100 is maximum)
	this.setVolume = function(volumeLevel) {
		volumeLevel = Math.max(volumeLevel, 0);
		volumeLevel = Math.min(volumeLevel, 100);
		if (this.mediaPlayer) {
			if (this.isWM9) {
				this.mediaPlayer.settings.volume = volumeLevel; 
				this.mediaPlayer.settings.mute = (volumeLevel==0);
			} else if (this.isWM) {
				this.mediaPlayer.Volume = -((100 - volumeLevel) * 50);//inverse and scale
				this.mediaPlayer.Mute = (volumeLevel==0);
			} else if (this.isRM) {
				this.mediaPlayer.SetVolume(volumeLevel);
				this.mediaPlayer.SetMute(volumeLevel==0);
			}
			if (volumeLevel==0) this.addEventToLog(EVENT_MUTE);
			else this.addEventToLog(EVENT_VOLUME_CHANGE, volumeLevel);
		}
	}
	this.getVolume = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				if (this.mediaPlayer.settings.mute) return(0);
				else return(this.mediaPlayer.settings.volume);
			} else if (this.isWM) {
				if (this.mediaPlayer.Mute) return(0);
				else return(Math.max(0, this.mediaPlayer.Volume/50+100));
			} else if (this.isRM) {
				if (this.mediaPlayer.GetMute()) return(0);
				else return(this.mediaPlayer.GetVolume());
			}
		} else return(0);
	}
	this.showFullScreen = function() {
		if (this.mediaPlayer && this.canPlayFullScreen()) {
			if (this.isWM9) {
				this.mediaPlayer.fullScreen = true;
				this.addEventToLog(EVENT_FULL_SCREEN);
			} else if (this.isWM && this.IE) {
				this.mediaPlayer.DisplaySize = 3;
				this.addEventToLog(EVENT_FULL_SCREEN);
		} else if (this.isRM) {
				this.mediaPlayer.SetFullScreen();
				this.addEventToLog(EVENT_FULL_SCREEN);
			}
		}
	}
	this.showVideo = function(mode) {
		if (this.mediaPlayer && this.canPlayFullScreen()) {
			if (!mode) mode = "none";
			if (this.isWM9) this.mediaPlayer.uiMode = mode;
		} else if (this.isWM && this.IE) {
		} else if (this.isRM) {
		}
	}
	this.hideVideo = function() {
		this.showVideo("invisible");
	}
	
	// information methods
	this.getStreamURL = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				return(this.mediaPlayer.URL);
			} else if (this.isWM) {
				return(this.mediaPlayer.Filename);
			} else if (this.isRM) {
				return(this.mediaPlayer.GetSource());
			} else return(0);
		} else return(0);
	}
	this.getVolume = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				return(this.mediaPlayer.settings.volume);
			} else if (this.isWM) {
				return(this.mediaPlayer.Mute ? 0 : Math.max(-50, this.mediaPlayer.Volume)+100);
			} else if (this.isRM) {
				return(this.mediaPlayer.getVolume());
			} else return(0);
		} else return(0);
	}
	this.isAutoStart = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				return(this.mediaPlayer.settings.autoStart);
			} else if (this.isWM) {
				return(this.mediaPlayer.AutoStart);
			} else if (this.isRM) {
				return(this.mediaPlayer.GetAutoStart());
			} else return(false);
		} else return(false);
	}
	this.isAudioOnly = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				return(this.mediaPlayer.currentMedia.imageSourceHeight==0);
			} else if (this.isWM) {
				return(this.mediaPlayer.ImageSourceHeight==0);
			} else if (this.isRM) {
				return(this.mediaPlayer.GetClipHeight()==0);
			} else return(false);
		} else return(false);
	}
	this.isPlaying = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				return( this.mediaPlayer.playState == 3);
			} else if (this.isWM) {
				return( (this.isIE ? this.mediaPlayer.PlayState : this.mediaPlayer.GetPlayState()) == 2);
			} else if (this.isRM) {
				return(this.mediaPlayer.GetPlayState()!=0 && this.mediaPlayer.GetPlayState()!=4);
			} else return(false);
		} else return(false);
	}
	this.isPaused = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				return( this.mediaPlayer.playState == 2);
			} else if (this.isWM) {
				return( (this.isIE ? this.mediaPlayer.PlayState : this.mediaPlayer.GetPlayState()) == 1);
			} else if (this.isRM) {
				return(this.mediaPlayer.GetPlayState()==4);
			} else return(false);
		} else return(false);
	}
	this.canPause = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				return(this.mediaPlayer.controls.isAvailable('pause'));
			} else if (this.isWM) {
				return(this.isIE ? this.mediaPlayer.CanSeek : this.mediaPlayer.GetCanSeek());
			} else if (this.isRM) {
				return(this.mediaPlayer.CanPause());
			} else return(false);
		} else return(false);
	}
	this.canPlayFullScreen = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				return(true);
			} else if (this.isWM) {
				return(true);
			} else if (this.isRM) {
				return(true);
			} else return(false);
		} else return(false);
	}
	// return duration of current clip in seconds
	this.getDuration = function() {
		if (this.isWM9) return(this.mediaPlayer.currentMedia.duration)
		else if (this.isWM && this.isIE) {
			return(this.mediaPlayer.Duration);
		} else if (this.isRM) {
			return(this.mediaPlayer.GetLength()*1000);
		} else return(0);
	}
	this.getBitrate = function() {
		if (this.isWM9) return(this.mediaPlayer.network.bitRate)
		else if (this.isWM && this.isIE) {
			return(this.mediaPlayer.Bandwidth);
		} else if (this.isRM) {
			return(this.mediaPlayer.GetBandwidthCurrent());
		} else return("unknown");
	}
	this.getProtocol = function() {
		if (this.isWM9) {
			if (this.mediaPlayer.network.sourceProtocol=="asfm") return("multicast");
			else return(this.mediaPlayer.network.sourceProtocol);
		} else if (this.isWM && this.isIE) {
			switch (this.mediaPlayer.SourceProtocol) {
				case 1: return("multicast");
				case 2: return("multicast");
				case 3: return("UDP");
				case 4: return("TCP");
				case 5: return("MSDB");
				case 6: return("HTTP");
				case 7: return("local file");
				default: return("unknown");
			}
		} else if (this.isRM) {
			return(this.mediaPlayer.GetSourceTransport(1));
		} else return("unknown");
	}
	this.getBufferingCount = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) return(this.mediaPlayer.network.bufferingCount);
			else if (this.isWM && this.isIE) return(this.mediaPlayer.BufferingCount);
			else return(0);
		} else return(0);
	}
	this.getCurrentMarker = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				if (this.mediaPlayer.controls.isAvailable("currentMarker")) return(this.mediaPlayer.controls.currentMarker);
				else return(0);
			} else if (this.isWM && this.isIE) {
				if (this.mediaPlayer.CanSeekToMarkers) return(this.mediaPlayer.CurrentMarker);
				else return(0);
			} else if (this.isRM) {
				if (this.mediaPlayer.GetCanSeekToMarkers()) return(this.mediaPlayer.GetCurrentMarker());
				else return(0);
			} else return(0);
		} else return(0);
	}
	this.getCurrentClipNumber = function() {
		var cn = 0;
		if (this.mediaPlayer) {
			if (this.isWM9) {
				for (cn=0; cn<this.playlist.length; cn++) {
					if (this.mediaPlayer.currentMedia.isIdentical(this.playlist[cn])) return(cn+1);
				}
			}
		}
		return(cn);
	}
	this.getClipCount = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				return(this.playlist.length);
			} else return(1);
		} else return(0);
	}
	this.getMarkerCount = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) return(this.mediaPlayer.currentMedia.markerCount);
			else if (this.isWM && this.isIE) return(this.mediaPlayer.MarkerCount);
			else return(0);
		} else return(0);
	}
	this.getMarkerName = function(markerNum) {
		if (this.mediaPlayer) {
			if (this.isWM9) return(this.mediaPlayer.currentMedia.getMarkerName(markerNum));
			else if (this.isWM && this.isIE) return(this.mediaPlayer.MarkerName(markerNum));
			else return(0);
		} else return(0);
	}
	this.getMarkerTime = function(markerNum) {
		if (this.mediaPlayer) {
			if (this.isWM9) return(this.mediaPlayer.currentMedia.getMarkerTime(markerNum));
			else if (this.isWM && this.isIE) return(this.mediaPlayer.MarkerTime(markerNum));
			else return(0);
		} else return(0);
	}
	
	this.playingBegins = function() {
		if (this.mediaPlayer && !this.timerIsOn) {
			if (this.playlist==null) this.saveMediaPlaylist();
			this.startTimer();
			//this.playedSuccessfully = true;
			this.addEventToLog(EVENT_PLAYING, null
				, "<playing><filename>"+this.getFilename(this.getStreamURL())
				+"</filename><protocol>"+this.getProtocol()
				+"</protocol><bitrate>"+this.getBitrate()+"</bitrate></playing>");
		}
	}
	
	// event trapping
	this.trapBuffering = function(isBuffering) {
		if (isBuffering) {
			this.addEventToLog(EVENT_BUFFERING);
		} else {
			this.playingBegins();
		}
	}
	this.trapScriptEvent = function(command,argument) {
		this.playingBegins();
		this.addEventToLog(EVENT_SCRIPT, null, "<script><command>"+command+"</command><arguments>"+argument+"</arguments></script>");
	}
	this.trapMarkerHit = function(markerNum) {
		this.playingBegins();
		/*this.addEventToLog(EVENT_MARKER, null, "<marker><number>"+markerNum+"</number></script>");*/
	}
	this.trapOpenStateChange = function(oldState,newState) {
		if (this.isWM9) {
			switch (newState) {
				case 0:
					this.addEventToLog(EVENT_CLOSED);
					break;
				case 2:
				case 9:
					this.addEventToLog(EVENT_LOCATING_SERVER);
					break;
				case 3:
				case 10:
					this.addEventToLog(EVENT_CONTACTING);
					break;
				case 4:
				case 11:
					this.addEventToLog(EVENT_LOADING_ASX);
					break;
				case 5:
				case 12:
					this.addEventToLog(EVENT_OPENING);
					break;
				case 13:
					this.addEventToLog(EVENT_OPEN);
					this.playingBegins();
					break;
				case 8:
					return("MediaChanging");
			}
		} else if (this.WM) {
			switch (newState) {
				case 0: this.addEventToLog(EVENT_CLOSED); break;
				case 1: this.addEventToLog(EVENT_LOADING_ASX); break;
				case 2: this.addEventToLog(EVENT_LOADING_NSC); break;
				case 3: this.addEventToLog(EVENT_LOCATING_SERVER); break;
				case 4: this.addEventToLog(EVENT_CONTACTING); break;
				case 5: this.addEventToLog(EVENT_OPENING); break;
				case 6: this.addEventToLog(EVENT_OPEN); break;
			}
		}
	}
	// returns value indicating state
	this.trapPlayStateChange = function(oldState,newState) {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				switch (newState) {
					case 1:
					case 8:
						this.addEventToLog(EVENT_STOPPED);
						this.stopTimer();
						return("stopped");
						break;
					case 2:
						this.addEventToLog(EVENT_PAUSED);
						this.stopTimer();
						return("paused");
						break;
					case 3:
						this.playedSuccessfully = true;
						this.addEventToLog(EVENT_BEGIN_PLAYING);
						this.startTimer();
						return("playing");
						break;
					case 6:
						this.addEventToLog(EVENT_BUFFERING);
						this.stopTimer();
						return("buffering");
						break;
					case 7: this.addEventToLog(EVENT_WAITING);
						return("waiting");
						break;
				}
			} else if (this.isWM) {
				switch (newState) {
					case 0:
						this.addEventToLog(EVENT_STOPPED);
						this.stopTimer();
						return("stopped");
						break;
					case 1:
						this.addEventToLog(EVENT_PAUSED);
						this.stopTimer();
						return("paused");
						break;
					case 2:
						this.playedSuccessfully = true;
						this.addEventToLog(EVENT_BEGIN_PLAYING);
						this.startTimer();
						return("playing");
						break;
					case 3:
						this.addEventToLog(EVENT_WAITING);
						return("waiting");
						break;
					case 8:
						this.addEventToLog(EVENT_CLOSED);
						this.stopTimer();
						return("stopped");
						break;
				}
			} else if (this.isRM) {
				switch (newState) {
					case 0: this.addEventToLog(EVENT_STOPPED);
						this.stopTimer();
						return("stopped");
						break;
					case 1: this.addEventToLog(EVENT_CONTACTING);
						return("contacting");
						break;
					case 2: this.addEventToLog(EVENT_BUFFERING);
						return(true);
						return("buffering");
						break;
					case 3:
						this.playedSuccessfully = true;
						this.addEventToLog(EVENT_PLAYING, null
							,"<playing><entry>"+this.mediaPlayer.GetTitle()
							+"</entry><filename>"+this.getFilename(this.getStreamURL())
							+"</filename><protocol>"+this.getProtocol()
							+"</protocol><bitrate>"+this.getBitrate()+"</bitrate></playing>");
						this.startTimer();
						return("playing");
						break;
					case 4:
						this.addEventToLog(EVENT_SEEKING);
						return("seeking");
						break;
				}
			}
		}
	}
	this.trapError = function(errorMessage,url,lineNumber) {
		if (url!=null) {
			this.addEventToLog(EVENT_ERROR, null
				,"<error type=\"script\">"
				+"<message>"+errorMessage+"</message>"
				+(url!=null ? "<url>"+url+"</url>" : "")
				+(lineNumber!=null ? "<linenumber>"+lineNumber+"</linenumber>" : "")
				+"</error>");
		} else if (this.mediaPlayer) {
			if (this.isWM9) {
				for (var i=0; i<this.mediaPlayer.error.errorCount; i++) {
					this.addEventToLog(EVENT_ERROR, null
						,"<error type=\"wm9\">"
						+"<code>"+this.mediaPlayer.error.item(i).errorCode+"</code>"
						+"<description>"+this.mediaPlayer.error.item(i).errorDescription+"</description>"
						+"<url>"+this.getStreamURL()+"</url>"
						+"</error>");
				}
			} else if (this.isWM) {
				if (this.mediaPlayer.hasError) {
					this.addEventToLog(EVENT_ERROR, null
						,"<error type=\"wm\">"
						+"<code>"+this.mediaPlayer.ErrorCode+"</code>"
						+"<description>"+this.mediaPlayer.ErrorDescription+"</description>"
						+"<message>"+errorMessage+"</message>"
						+"<url>"+this.getStreamURL()+"</url>"
						+"</error>");
				}
			} else if (this.isRM) {
					this.addEventToLog(EVENT_ERROR, null
						,"<error type=\"real\"><message>"+errorMessage+"</message></error>");				
			}
		}
	}
	this.trapWarning = function(type,parameter,description) {
		if (this.mediaPlayer && this.WM) {
			this.addEventToLog(EVENT_WARNING, null
				,"<warning><type>"+type
				+"</type><parameter>"+parameter
				+"</parameter><description>"+this.mediaPlayer.ErrorDescription
				+"</description><url>"+this.getStreamURL()+"</url></warning>");
		}
	}

	// event log functions
	this.getCurrentDateAndTime = function() {
		var d = new Date();
		return(this.getAs2Digits(d.getMonth()+1)+"/"+this.getAs2Digits(d.getDate())+"/"+d.getFullYear()+" "
					+this.getAs2Digits(d.getHours())+":"+this.getAs2Digits(d.getMinutes())+":"+this.getAs2Digits(d.getSeconds()));
	}
	this.startTimer = function() {
		if (this.timerIsOn==false) {
			this.timerStart = (new Date()).getTime();
			this.timerIsOn = true;
			this.addEventToLog(EVENT_START_TIMER, this.timerStart);
		}
	}
	this.stopTimer = function() {
		if (this.timerIsOn) {
			this.timerStop = (new Date()).getTime();
			this.duration += Math.max(0,this.timerStop - this.timerStart);
			this.addEventToLog(EVENT_STOP_TIMER, this.timerStop);
			this.timerIsOn = false;
		}
	}
	this.getStatusLogXml = function() {
		return( this.getEventLogStartingXml() + (this.statusLog!=null ? this.statusLog : "") + this.getEventLogEndingXml() );
	}
	this.clearStatusLog = function() {
		this.statusLog = "";
	}
	this.startEventLog = function(viewingID, testForProgramID) {
		if (viewingID) this.viewingID = viewingID;
		if (testForProgramID) this.testForProgramID = testForProgramID;
		this.statusLog = "";
	}
	this.addEventToLog = function(eventTypeID, detailNumber, detailXml) {
		if (this.statusLog==null) this.startEventLog();
		this.statusLog += "<event id=\""+this.statusLogID+"\" time=\""+this.getCurrentDateAndTime()+"\" eventtypeid=\""+eventTypeID+"\"";
		if (detailNumber!=null) this.statusLog += ">"+detailNumber+"</event>\n";
		else if (detailXml!=null) this.statusLog += ">"+detailXml.replace("&","&amp;")+"</event>\n";
		else this.statusLog += "/>\n";
		this.statusLogID++;
	}
	this.getEventLogStartingXml = function() {
		return("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<medialog>\n"
			+(this.viewingID!=null && this.viewingID>0 ? "<viewing id=\""+this.viewingID+"\"/>\n" : "")
			+(this.testForProgramID!=null && this.testForProgramID>0 ? "<testForProgram id=\""+this.testForProgramID+"\"/>\n" : "")
			+"<events>\n");
	}
	this.getEventLogEndingXml = function() {
		var endingXml = "</events>\n";
		endingXml += "<result value=\""+this.playedSuccessfully+"\"/>\n";
		if (this.timerIsOn) {
			endingXml += "<duration value=\""+(this.duration+Math.max(0,(new Date()).getTime() - this.timerStart))+"\"/>\n";
		} else {
			endingXml += "<duration value=\""+this.duration+"\"/>\n";
		}
		endingXml += "</medialog>\n";
		return(endingXml);
	}
	
	// private methods
	this.getAs2Digits = function(inputNumber) {
		var s = String(inputNumber);
		return(s.length==1 ? "0"+s : s);
	}
	this.getFilename = function(url) {
		if (url) {
			var lastSlash = url.lastIndexOf("/");
			if (lastSlash>=0) return(url.substring(lastSlash+1,url.length));
			else return(url);
		} else return(null);
	}
	this.saveMediaPlaylist = function() {
		if (this.mediaPlayer) {
			if (this.isWM9) {
				this.playlist = []
				for (var m=0; m<this.mediaPlayer.currentPlaylist.count; m++) {
					this.playlist[this.playlist.length] = this.mediaPlayer.currentPlaylist.item(m);
				}
			}
		}
	}

	// assume Flash movies are playing automatically
	if (this.isFlash) this.playingBegins();
		
	return(this);
}

var swfLoaded = false;
