//------------------------------------------------------
// The VideoPlayer class
VideoPlayer.prototype = new CBSObject();
VideoPlayer.prototype.constructor = VideoPlayer;
VideoPlayer.prototype.numbers = "1234567890";
function VideoPlayer(playerRoot, display, width, height) {
  this.superClass = CBSObject.prototype;
  this.superClass.constructor.call(this);
	this.objectType = "VideoPlayer";
	_objects[this.objectId] = this;

	this.format = null;
	this.defaultFormat = null;
	this.possibleFormats = "";
	this.installedFormats = "";
	this.supportedFormats = "";

	if (width) this.width = width;
	else this.width = 320;
	if (height) this.height = height;
	else this.height = 240;
	if (display) this.display = display;
	else this.display = "";
	this.frameWidth = this.width;
	this.frameHeight = this.height;
		
	if (playerRoot) this.playerRoot = playerRoot;
	else this.playerRoot = "/common/vplayer4";
	this.videoRoot = "http://dev.cgi.cbs.com/vplayer4/play.pl";
	this.videoImageRoot = "http://dev.cbs.com/common/vplayer4/php/image.php";
	this.imageRoot = this.playerRoot + "/images";
	this.videoUrl = null;
	this.videoImageUrl = null;
	
	this.defaultImage = "http://www.cbsnews.com/images/2006/08/28/image1939838.jpg";
	
	this.disablePlaylist = false;
	
	this.displayControls = 0;
	this.displayStatus = 1;
	this.autoResize = 0;
		
	this.redraw = false;
	
	this.params = new Array();
	this.params["feat"] = "vplayer";
	this.params["adtype"] = "pre";
	
	this.currentVideo = null;
	this.cache = new VideoCache(this);
	this.playlist = new VideoPlaylist(this);
	this.defaultPlaylist = new VideoPlaylist(this);
	this.skipToVideo = false;
	
	this.volume = 100;
	this.state = "stop";
	this.playState = "none";
	
	this.videoType = null;
	
	this.capabilities = null;
	
	this.siteName = 'CBS.com';
	this.bgColor = "#003366";
	this.bgImage = "http://wwwimage.cbsnews.com/common/vplayer2/images/newImages/bg_video.gif"
	
	this.iFrame = null;
}


//--------------------------------------------------------
// Calls a command in the frame by getting button in frame and calling onclick
VideoPlayer.prototype._executeFrameCommand = function(command) {
	this.log("command", "_executeFrameCommand " + command);
	var iFrame = this.getIFrameDocument();
	if (iFrame == null) return false;
	var m = "_executeFrameCommand(" + command;
	if (!iFrame.forms.controlForm) return false;
	for (var i=1; i<arguments.length; i++) {
		var param = iFrame.forms.controlForm["param" + i];
		if (param) param.value = arguments[i];
		m += ", " + arguments[i];
	}
	this.log("command", m + ")");
	var playCommand = iFrame.forms.controlForm[command + 'Command'];
	if (playCommand) {
		this.log("command", "onclick");
		playCommand.onclick();
		return true;
	} else {
		return false;
	}
}



//--------------------------------------------------------
// Returns the play state
VideoPlayer.prototype.getPlayState = function() {
	return this.playState;
}
// Is a video playing?
VideoPlayer.prototype.isPlaying = function() {
	//if (this.state == "play") return true;
	return (this.playState == "playing");
}
// Is a video playing?
VideoPlayer.prototype.isStopped = function() {
	this.log("stopped", "isStopped " + this.state + " " + this.playState);
	if (this.state == "stop") return true;
	return ((this.playState == "none") || (this.playState == "stopped") || (this.playState == "ended"));
}
// Is a video paused?
VideoPlayer.prototype.isPaused = function() {
	this.log("status", "isPaused " + this.playState);
	if (this.state == "pause") return true;
	return (this.playState == "paused");
}
// Is a video buffering?
VideoPlayer.prototype.isBuffering = function() {
	this.log("status", "isBuffering " + this.playState);
	return (this.playState == "buffering");
}


//--------------------------------------------------------
// Ask user to choose format
VideoPlayer.prototype.chooseFormat = function(showImage, playVideo, startingSegment, startingTime) {
	this.log("format", "chooseFormat " + showImage + " " + playVideo);
	this.stop();
	this.format = null;
	if (!showImage) showImage = "";
	if (!playVideo) playVideo = "";
	var url = this.playerRoot + "/html/choose_iframe.html?id=" + this.objectId + "&showImage=" + showImage + "&playVideo=" + playVideo;
	if (startingSegment)
		 url += "&startingSegment=" + startingSegment;
	if (startingTime)
		 url += "&startingTime=" + startingTime;
	var iFrame = this.getIFrameDocument();
	iFrame.src = url;
	iFrame.location.href = url;
}
//--------------------------------------------------------
// Ask user to choose format
VideoPlayer.prototype.uninstalledFormat = function(showImage, playVideo, startingSegment, startingTime) {
	this.log("format", "uninstalledFormat " + showImage + " " + playVideo);
	this.stop();
	this.format = null;
	if (!showImage) showImage = "";
	if (!playVideo) playVideo = "";
	var url = this.playerRoot + "/html/uninstalled_iframe.html?id=" + this.objectId + "&showImage=" + showImage + "&playVideo=" + playVideo;
	if (startingSegment)
		 url += "&startingSegment=" + startingSegment;
	if (startingTime)
		 url += "&startingTime=" + startingTime;
	var iFrame = this.getIFrameDocument();
	iFrame.src = url;
	iFrame.location.href = url;
}
//--------------------------------------------------------
// Ask user to choose format
VideoPlayer.prototype.unsupportedFormat = function(showImage, playVideo, startingSegment, startingTime) {
	this.log("format", "unsupportedFormat " + showImage + " " + playVideo);
	this.stop();
	this.format = null;
	if (!showImage) showImage = "";
	if (!playVideo) playVideo = "";
	var url = this.playerRoot + "/html/unsupported_iframe.html?id=" + this.objectId + "&showImage=" + showImage + "&playVideo=" + playVideo;
	if (startingSegment)
		 url += "&startingSegment=" + startingSegment;
	if (startingTime)
		 url += "&startingTime=" + startingTime;
	var iFrame = this.getIFrameDocument();
	iFrame.src = url;
	iFrame.location.href = url;
}

//--------------------------------------------------------
// Format for playing videos
VideoPlayer.prototype.setFormat = function(format, play, startingSegment, startingTime) {
	this.log("format", "set Format " + format + " " + play);
	this.videoUrl = null;
	this.format = format;
	if ((!play) || ((!this.hasEvent("beforePlay")) || (this._fireEvent("beforePlay")))) {
		var iFrame = this.getIFrameDocument();
		if (iFrame) {
			var url = this.playerRoot + '/html/' + format + "_iframe.html?id=" + this.objectId + "&ord=" + Math.random() * 100000;
			if (play) {
				url += "&play=1";
				this.state = "play";
			}
			if (startingSegment)
				 url += "&startingSegment=" + startingSegment;
			if (startingTime)
				 url += "&startingTime=" + startingTime;
			iFrame.src = url;
			iFrame.location.href = url;
			this.log("format", "setFormat " + url);
		}
	}
	this._fireEvent("onFormatChanged", format);
}
VideoPlayer.prototype.getFormat = function() {
	return this.format;
}

//--------------------------------------------------------
// Default format
VideoPlayer.prototype.setDefaultFormat = function(format) {
	this.log("format", "set DefaultFormat " + format);
	setCookie("vp4_format", format, "Fri, 01-Jan-2010 00:00:01 GMT", '/', 2);
	this.defaultFormat = format;
	this._fireEvent("onDefaultFormatChanged", format);
}
VideoPlayer.prototype.getDefaultFormat = function() {
	return this.defaultFormat;
}



//------------------------------------------------------
// Get the video object from all possible param choices
VideoPlayer.prototype.newVideo = function() {
	return new Video(this);
}

//------------------------------------------------------
// Get the video object from all possible param choices
VideoPlayer.prototype.getVideo = function(param1, param2, param3) {
	if (this.cache) {
		return this.cache.getVideo(param1, param2, param3);
	} else {
		if (typeof(param1) == "object") {
			return param1;
		} else if (this.numbers.indexOf(param1.substring(0, 1)) == -1) {
			return new Video(this.videoPlayer, null, param3, param1, param2);
		} else {
			if ((this.videoType) && (this.numbers.indexOf(param1.substring(param1.length-1)) != -1)) {
				param1 += this.videoType;
			}
			return new Video(this.videoPlayer, param1, param2);
		}
	}
}

//--------------------------------------------------------
// Adds a param to the video url
VideoPlayer.prototype.setParam = function(name, value) {
	this.params[name] = value;
	this.videoUrl = null;
}
//--------------------------------------------------------
// Returns a param on the video url
VideoPlayer.prototype.getParam = function(name) {
	return this.params[name];
}

//--------------------------------------------------------
// Returns a video url 
VideoPlayer.prototype.getVideoUrl = function(param) {
	if (this.videoUrl == null) {
		this.videoUrl = this.videoRoot + ((this.videoRoot.indexOf('?') == -1) ? '?' : '&');
		if (this.format == "live") this.videoUrl += "type=wmv";
		else this.videoUrl += "type=" + this.format;
		this.videoUrl += "&adtype=none";
		this.videoUrl += "&width=" + this.width;
		this.videoUrl += "&height=" + this.height;
		for (var paramName in this.params) {
			this.videoUrl += "&" + paramName + "=" + this.params[paramName];
		}
		if (this.startSegment) {
			this.videoUrl += "&sgmt=" + this.startSegment;
			this.startSegment = null;
		}
		if (this.startTime) {
			this.videoUrl += "&time=" + this.startTime;
			this.startTime = null;
		}
	}
	if (this.numbers.indexOf(param.substring(0, 1)) == -1) {
		return this.videoUrl + "&url=" + escape(param) + "&ord=" + Math.random() * 100000;
	} else {
		return this.videoUrl + "&id=" + param + "&ord=" + Math.random() * 100000;;
	}
}
//--------------------------------------------------------
// Returns a video image url 
VideoPlayer.prototype.getImageUrl = function(param) {
	if (param == null) {
		return this.defaultImage;
	} else if (this.videoImageUrl == null) {
		this.videoImageUrl = this.videoImageRoot + ((this.videoImageRoot.indexOf('?') == -1) ? '?' : '&');
		this.videoImageUrl += "size=" + this.width + 'x' + this.height;
	}
	if (this.numbers.indexOf(param.substring(0, 1)) == -1) {
		return param;
	} else {
		return this.videoImageUrl + "&id=" + param;
	}
}


//--------------------------------------------------------
// Play Video Video
// Calls can be
// play()
// play(video, startingSegment, startingTime)
// play(videoUrl, imageUrl, format, startingSegment, startingTime)
// play(videoId, format, startingSegment, startingTime)
VideoPlayer.prototype.play = function(param1, param2, param3, param4, param5) {
	this.log("command", "play " + this.format + " " + param1);
	this.state = "play";

	var startingSegment;
	var startingTime;

	var video;
	if (param1 == null) {
		if (this.getCurrentVideo() == null) {
			this.log("command", "No current video");
			return;
		}
		if (this.isPaused()) {
			this.log("command", "paused");
			return this._executeFrameCommand("play");
		}
		video = this.getCurrentVideo();
		startingSegment = param2;
		startingTime = param3;
	} else { 
		video = this.getVideo(param1, param2, param3);
		if (typeof(param1) == "object") {
			startingSegment = param2;
			startingTime = param3;
		} else if (this.numbers.indexOf(param1.substring(0, 1)) == -1) {
			startingSegment = param4;
			startingTime = param5;
		} else {
			startingSegment = param3;
			startingTime = param4;
		}
	}
	this.log("command", "play " + video.videoId);
	this.log("format", "play " + video.format);
	
	// Set the current video
	video.clear();
	video.startingSegment = startingSegment || 0;
	video.startingTime = startingTime || 0;
	this.setCurrentVideo(video);

	if ((video.format.indexOf("wmv") >= 0) && (video.format.indexOf("flv") < 0))
		video.format += "|flv";

	// Does video exist in multiple formats?
	var playFormat;
	var id = parseInt(video.videoId);
	if (id < 10) {
		playFormat = "live";
	} else if (video.format.indexOf('|') >= 0) {
		if (this.format == null) {
			return this.chooseFormat(false, true, startingSegment, startingTime);
		} else if (video.format.indexOf(this.defaultFormat) != -1) {
			playFormat = this.defaultFormat;
		} else if (video.format.indexOf(this.format) != -1) {
			playFormat = this.format;
		} else {
			return this.chooseFormat(false, true, startingSegment, startingTime);
		}
	} else { 
		if (this.installedFormats.indexOf(video.format) >= 0) {
			playFormat = video.format;
		} else {
			return this.uninstalledFormat(false, true, startingSegment, startingTime);
		}
	}
	this.log("format", "play " + playFormat + " " + this.format);

	video.playFormat = playFormat;
	video.currentPIDNum = video.startingSegment;
	this.playState = "loading";
	//this.onVideoOpened(video);
	
	if ((!this.hasEvent("beforePlay")) || (this._fireEvent("beforePlay"))) {
		if ((playFormat != "live") && (!video.loaded || !video.pidLoaded || !video.pids || (playFormat != "rm" && !video.pids[playFormat]) || (playFormat != "rm" && video.pids[playFormat] == ""))) {
			video.load();
			this.log("loading", "Video not loaded, waiting");
			setTimeout("_objects['" + this.objectId + "'].play();", 100);
		} else {
			this.redraw = false;
			if (true) {
				this._executeFrameCommand("stop");
				if (playFormat == 'live') {
					this.setFormat(playFormat, true, startingSegment, startingTime);
				} else {
					this.setFormat(playFormat, true, startingSegment, startingTime);
				}
			} else {
				if ((playFormat != this.format) || (this.redraw)) {
					this.redraw = false;
					this.setFormat(playFormat, true, startingSegment, startingTime);
				} else {
					//return this._executeFrameCommand("play", video.mediaId, video.getImageUrl(), startingTime);
					if (playFormat == "rm") {
						return this._executeFrameCommand("play", video.mediaId, video.getImageUrl(), startingTime);
					} else {
						return this._executeFrameCommand("play", video.pids[playFormat][video.currentPIDNum], video.getImageUrl(), startingTime);
					}
				}
			}
		}
	}
}


//--------------------------------------------------------
// Stop 
VideoPlayer.prototype.stop = function() {
	this.log("command", "stop");
	this.state = "stop";
	// Clear the current video
	this.setCurrentVideo(null);
	return this._executeFrameCommand("stop");
}

//--------------------------------------------------------
// Pause
VideoPlayer.prototype.pause = function() {
	this.log("command", "pause");
	this.state = "pause";
	return this._executeFrameCommand("pause");
}
//--------------------------------------------------------
// Rewind
VideoPlayer.prototype.rewind = function() {
	this.log("command", "rewind");
	var video = this.getCurrentVideo();
	if (!this.capabilities.scripting[this.format]) {
		this.stop();
		this.play(video);
	} else if ((this.isPlaying()) && (video) && (video.position > 2)) {
		return this._executeFrameCommand("restart");
	} else {
		if (video != null) {
			if (video.currentPIDNum > 0) {
				return this.play(null, video.currentPIDNum-1);
			}
		}
		if (this.playlist.getPreviousVideo()) {
			this.play(this.playlist.previousVideo());
		} else if (this.defaultPlaylist.getPreviousVideo()) {
			this.play(this.defaultPlaylist.previousVideo());
		} else {
			return this._executeFrameCommand("restart");
		}
	}
}
//--------------------------------------------------------
// Forward
VideoPlayer.prototype.forward = function() {
	this.log("command", "forward");
	var video = this.getCurrentVideo();
	if (video != null) {
		this.log("pid", "forward " + video.currentPIDNum + " " + video.numSegments);
		if (video.currentPIDNum < (video.numSegments-1)) {
			return this.play(null, video.currentPIDNum+1);
		}
	}
	var video = this.playlist.nextVideo();
	if (video) {
		return this.play(video);
	}
	video = this.defaultPlaylist.nextVideo();
	if (video) {
		return this.play(video);
	}
	return this.stop();
}

//--------------------------------------------------------
// Volume
VideoPlayer.prototype.setVolume = function(volume) {
	this.log("command", "setVolume");
	this.volume = volume;
	return this._executeFrameCommand("setVolume", volume);
	this._fireEvent("onVolumeChanged", volume);
}
VideoPlayer.prototype.getVolume = function() {
	return this.volume;
}


//--------------------------------------------------------
// Position
VideoPlayer.prototype.setPosition = function(position) {
	this.log("command", "setPosition " + position);
	if ((typeof(position) != "number") && (position.substring(position.length-1) == "%")) {
		var video = this.getCurrentVideo();
		if (video == null) return;
		var segment = video.getCurrentSegment();
		var percent = position.substring(0, position.length-1);
		position = percent * segment.duration / 100;
	}
	return this._executeFrameCommand("setPosition", position);
	this._fireEvent("setPosition", position);
}
VideoPlayer.prototype.getPosition = function() {
	var video = this.getCurrentVideo();
	if (!video) return 0;
	if (video.segments.length == 0) return 0;
	var pos = video.segments[video.segments.length-1].position;
	if (!pos) return 0;
	return pos;
}



//--------------------------------------------------------
// Full screen
VideoPlayer.prototype.setFullscreen = function() {
	return this._executeFrameCommand("fullscreen");
}





//---------------------------------------------------------------
// Callbacks
// Video opened
VideoPlayer.prototype.onVideoOpened = function(video) {
	this.log("videoCallback", "onVideoOpened ");
	video.state = "opened";
	this._fireEvent("onVideoOpened", video);

	// If loaded from XML, then already set
	if (video.loaded) {
		this.onVideoSet(video);
	}
}
// Video set
VideoPlayer.prototype.onVideoSet = function(video) {
	this.log("videoCallback", "onVideoSet ");
	video.state = "set";
	video.loaded = true;
	this._fireEvent("onVideoSet", video);
}
// Video started
VideoPlayer.prototype.onVideoStarted = function(video) {
	this.log("videoCallback", "onVideoStarted ");
	video.state = "started";
	this._fireEvent("onVideoStarted", video);
}
// Video closed
VideoPlayer.prototype.onVideoClosed = function(video) {
	this.log("videoCallback", "onVideoClosed ");
	video.state = "closed";
	this._fireEvent("onVideoClosed", video);
	video.state = null;
	if (video == this.getCurrentVideo()) {
		if (this.state != "stop") {
			var segment = video.getCurrentSegment();
			if ((segment) && (segment.maxPosition > segment.duration - 2)) {
				//if (!this.disablePlaylist)
				this.forward();
			} else {
				if (segment) {
					this.log("videoCallback", "segment not done " + segment.maxPosition + " " + segment.duration);
				} else {
					this.log("videoCallback", "no segment");
				}
			}
		} else {
			this.log("videoCallback", "stop " + this.state);
		}	
	} else {
		this.log("videoCallback", "not current");
	}
}
// Video loaded from XML
VideoPlayer.prototype.onVideoLoaded = function(video) {
	this._fireEvent("onVideoPropertyChanged", video);  // Legacy
	this._fireEvent("onVideoLoaded", video);

	// If in open state, 
	if (video.state == "opened") {
		this.onVideoSet(video);
	}
}

// Segment opened
VideoPlayer.prototype.onSegmentOpened = function(segment) {
	this.log("segmentCallback", "onSegmentOpened ");
	segment.state = "opened";
	this._fireEvent("onSegmentOpened", segment);
}
// Segment set
VideoPlayer.prototype.onSegmentSet = function(segment) {
	this.log("segmentCallback", "onSegmentSet ");
	segment.state = "set";
	this._fireEvent("onSegmentSet", segment);
}
// Segment started
VideoPlayer.prototype.onSegmentStarted = function(segment) {
	this.log("segmentCallback", "onSegmentStarted ");
	segment.state = "started";
	this._fireEvent("onSegmentStarted", segment);
}
// Segment closed
VideoPlayer.prototype.onSegmentClosed = function(segment) {
	this.log("segmentCallback", "onSegmentClosed ");
	segment.state = "closed";
	this._fireEvent("onSegmentClosed", segment);
	segment.state = null;
}

// State changed
VideoPlayer.prototype.onPlayStateChanged = function(state) {
	this.log("stateCallback", "onPlayStateChanged " + state);
	if (this.playState != state) {
		this.playState = state;
		this._fireEvent("onPlayStateChanged", state);
	}
}
// Position changed
VideoPlayer.prototype.onPositionChanged = function(pos, dur) {
	this.log("positionCallback", "onPositionChanged " + pos + "/" + dur);
	if (this.currentVideo)
		this._fireEvent("onPositionChanged", pos, dur);
}
// Buffering 
VideoPlayer.prototype.onBuffering = function(percent) {
	this.log("bufferingCallback", "onBuffering " + percent);
	this._fireEvent("onBuffering", percent);
}
// Command
VideoPlayer.prototype.onCommand = function(type, param) {
	this.log("bufferingCallback", "onCommand " + type + " " + param);
	this._fireEvent("onCommand", type, param);
}






//--------------------------------------------------------------------
// Detect installed players
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function ControlVersion()
{
	var version;
	var axo;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			
			// default to the first public version
			version = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");

		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
}

// JavaScript helper required to detect Flash Player PlugIn version information
function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;			
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
			var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}

// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
	versionStr = GetSwfVer();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];

        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}


var detectResult = false; 
VideoPlayer.prototype.detectIEPlugin = function(classID) { 
	document.write('<SCRIPT LANGUAGE=VBScript>\n on error resume next \n detectResult = IsObject(CreateObject("' + classID + '"))</SCRIPT>\n'); 
	return detectResult; 
}
VideoPlayer.prototype.detectNSPlugin = function(classID) { 
	try {
		return ((navigator.mimeTypes != null) && (navigator.mimeTypes[classID]) && (navigator.mimeTypes[classID].enabledPlugin != null));
	} catch (e) {
		return false;
	}
}
VideoPlayer.prototype.combine = function(array) {
	var string = "";
	var first = true;
	for (var i in array) {
		if (array[i]) {
			if (!first) {
				string += "|";
			} else {
				first = false;
			}
			string += i;
		}
	}
	return string;
}
VideoPlayer.prototype.detectCapabilities = function() {
	this.log("detect", "detectCapabilities");
	try {
		// Default to false
		this.capabilities = new Object();
		this.capabilities.formats = new Array();
		this.capabilities.formats["rm"] = false;
		this.capabilities.formats["wmv"] = false;
		this.capabilities.formats["flv"] = false;
		this.capabilities.scripting = new Array();
		this.capabilities.scripting["rm"] = false;
		this.capabilities.scripting["wmv"] = false;
		this.capabilities.scripting["flv"] = false;

		// Detect browser
		var ua = navigator.userAgent.toLowerCase();
		if (ua.indexOf("msie") != -1) this.capabilities.browser = "ie";
		else if (ua.indexOf("safari") != -1) this.capabilities.browser = "safari";
		else if (ua.indexOf("firefox") != -1) this.capabilities.browser = "firefox";
		else if (navigator.appName.indexOf("Netscape") != -1) this.capabilities.browser = "netscape";

		if ((ua.indexOf("win") != -1) || (ua.indexOf("32bit") != -1)) this.capabilities.os = "windows";
		else if (ua.indexOf("mac") != -1) this.capabilities.os =  "mac";
		if (this.capabilities.browser == "ie") {
			var start = ua.indexOf("msie ") + 5;
			var end = ua.indexOf(";", start);
			this.capabilities.browserVersion = parseFloat(ua.substring(start, end));
		} else {
			this.capabilities.browserVersion = -1;
		}
	
		// Detect players
		if (this.capabilities.browser == "ie") {
			this.capabilities.formats["rm"] = this.capabilities.scripting["rm"] = this.detectIEPlugin("rmocx.RealPlayer G2 Control.1");
			this.capabilities.formats["wmv"] = this.capabilities.scripting["wmv"] = this.detectIEPlugin("MediaPlayer.MediaPlayer.1");
			if ((this.capabilities.os == "mac") || (this.capabilities.browserVersion < 5)) {
				this.capabilities.scripting["wmv"] = false;
			}
		} else {
			this.capabilities.formats["rm"] = this.capabilities.scripting["rm"] = this.detectNSPlugin("audio/x-pn-realaudio-plugin");
			this.capabilities.formats["wmv"] = this.detectNSPlugin("application/x-mplayer2");
			this.capabilities.scripting["wmv"] = false;
			if (this.capabilities.browser == "safari")
				this.capabilities.scripting["rm"] = false;
		}
		if (DetectFlashVer(8, 0, 0)) {
			this.capabilities.formats["flv"] = this.capabilities.scripting["flv"] = true;
		}
		
		// No more real
		//this.capabilities.formats["rm"] = this.capabilities.scripting["rm"] = false;
		
		// Get install formats
		this.installedFormats = this.combine(this.capabilities.formats);

		// Get possible formats
		this.possibleFormats = this.combine(this.capabilities.scripting);
		if (this.possibleFormats == "") {
			var formats = new Array();
			formats["rm"] = this.capabilities.formats["rm"];
			if ((this.capabilities.os != "mac") && (this.capabilities.browser != "firefox")) {
				formats["wmv"] = this.capabilities.formats["wmv"];
			}
			formats["flv"] = this.capabilities.formats["flv"];
			this.possibleFormats = this.combine(formats);
		}
		
		// Get supported formats
		if (this.capabilities.os == "mac") {
			this.supportedFormats = "flv";
		} else if (this.capabilities.browser == "ie") {
			this.supportedFormats = "wmv|flv";
		} else if (this.capabilities.browser == "firefox") {
			this.supportedFormats = "flv";
		} else {
			this.supportedFormats = "flv";
		}
		//this.supportedFormats = "wmv|flv";
	} catch (e) {
		this.log("error", "detectCapabilities " + e);
	}
	this._fireEvent("onCapabilitiesChanged", this.capabilities);
}
//--------------------------------------------------------------------
// Disable the ability to script a format
VideoPlayer.prototype.disableScripting = function(format) {
	this.log("capabilites", 'disableScripting ' + format);
	this.capabilities.scripting[format] = false;
	this._fireEvent("onCapabilitiesChanged", this.capabilities);
}

//--------------------------------------------------------------------
// Detect startup format
VideoPlayer.prototype.detectFormat = function() {
	this.log("detect", "detectFormat");
	if (!this.capabilities)
		this.detectCapabilities();
	
	var format = getCookie("vp4_format");
	if (!format) {
		format = "flv";
		//if (this.possibleFormats == "")
		//	format = null;
		//else if (this.possibleFormats.indexOf("|") >= 0)
		//	format = null;
		//else
		//	format = this.possibleFormats;
	}
	this.format = format;
	this.defaultFormat = format;
	return format;
}



//--------------------------------------------------------------------
// Inserts the iframe into the document
VideoPlayer.prototype.printIFrame = function(width, height, div) {
	this.print(width, height, div);
}
VideoPlayer.prototype.print = function(width, height, div) {
	this.log("display", "printIFrame");
	if (width) this.width = width;
	if (height) this.height = height;
	this.frameWidth = this.width;
	this.frameHeight = this.height;
	if (div) {
		div.innerHTML = '<iframe id="videoPlayerIFrame' + this.objectId + '" height="' + this.frameHeight + '" width="' + this.frameWidth + '" scrolling="no" frameborder="0"></iframe>';
	} else {
		document.write('<iframe id="videoPlayerIFrame' + this.objectId + '" height="' + this.frameHeight + '" width="' + this.frameWidth + '" scrolling="no" frameborder="0"></iframe>');
	}
	if (this.format)
		this.setFormat(this.format);
	else
		this.chooseFormat(true, true);
}
// Returns the iframe
VideoPlayer.prototype.getIFrame = function() {
	if (this.iFrame == null) {
		this.iFrame = document.getElementById("videoPlayerIFrame" + this.objectId);
	}
	return this.iFrame;
}
// Returns the document for the iframe
VideoPlayer.prototype.getIFrameDocument = function() {
	var iFrame = this.getIFrame();
	if (iFrame == null) {
		return null;
	}
	return iFrame.contentWindow.document;
}
// Resize the iframe
VideoPlayer.prototype.resize = function(width, height) {
	if ((this.frameWidth == width) && (this.frameHeight == height))
		return;
		
	this.frameWidth = width;
	this.frameHeight = height;
	
	var iFrame = this.getIFrame();
	if (iFrame == null) return;
	
	iFrame.width = this.frameWidth;
	iFrame.height = this.frameHeight;
	this._fireEvent("onResize", this.frameWidth, this.frameHeight);
}



//--------------------------------------------------------
// Sets the image in the iframe
VideoPlayer.prototype.setImage = function(imageUrl) {
	this._executeFrameCommand("setImage", imageUrl);
}

//--------------------------------------------------------
// Gets the current video
VideoPlayer.prototype.getCurrentVideo = function() {
	if (this.currentVideo == null) {
		this.setCurrentVideo(this.playlist.getCurrentVideo());
		if (this.currentVideo == null) {
			this.setCurrentVideo(this.defaultPlaylist.getCurrentVideo());
		}
	}
	return this.currentVideo;
}
//--------------------------------------------------------
// Sets the current video
VideoPlayer.prototype.setCurrentVideo = function(param1, param2, param3) {
	var old = this.currentVideo;
	if (param1 == null) {
		this.currentVideo = null;
	} else {
		this.currentVideo = this.getVideo(param1, param2, param3);
	}
	if (this.currentVideo != null) {
		if (!this.currentVideo.equals(old)) {
			this.log("video", "current video changed " + (old ? old.videoId : 0) + " " + (this.currentVideo ? this.currentVideo.videoId : 0));
			var video = this.playlist.getCurrentVideo();
			if ((!video) || (!video.equals(this.currentVideo))) {
				if (this.skipToVideo) {
					this.playlist.skipToVideo(this.currentVideo);
				} else {
					this.playlist.placeVideo(this.currentVideo);
				}
			}
			this.setImage(this.currentVideo.getImageUrl());
			this.defaultPlaylist.removeVideo(this.currentVideo);
			this._fireEvent("onCurrentVideoChanged", this.currentVideo);
		}
	} else if (old != null) {
		//this.setImage(this.defaultImage);
		this.defaultPlaylist.removeVideo(this.currentVideo);
		this._fireEvent("onCurrentVideoChanged", this.currentVideo);
	}
}
//--------------------------------------------------------
// Adds a video to the playlist
VideoPlayer.prototype.addVideo = function(param1, param2, param3) {
	if ((this.format) && (!this.capabilities.scripting[this.format])) {
		return false;
	}
	this.playlist.addVideo(param1, param2, param3);
	this.defaultPlaylist.removeVideo(param1, param2, param3);
	if ((!this.skipToVideo) && (this.playlist.pos == -1)) {
		this.playlist.pos = 0;
	}
	return true;
}
//--------------------------------------------------------
// Adds a video to the default playlist
VideoPlayer.prototype.addDefaultVideo = function(param1, param2, param3) {
	if (!this.findVideo(param1, param2, param3)) {
		this.defaultPlaylist.addVideo(param1, param2, param3);
	}
}
//--------------------------------------------------------
// Removes the video from the playlist
VideoPlayer.prototype.removeVideo = function(param1, param2, param3) {
	this.playlist.removeVideo(param1, param2, param3);
	this.defaultPlaylist.removeVideo(param1, param2, param3);
}
//--------------------------------------------------------
// Returns the next video in the playlist
VideoPlayer.prototype.getNextVideo = function() {
	var video = this.playlist.getNextVideo();
	if (video == null) {
		return this.defaultPlaylist.getNextVideo();
	}
	return video;
}

