/**
 * @class This object handles low level video functionality for the RealPlayer platform.
 * @property {Boolean} rate The playback speed of the video (used for RealPlayer only)
 * @property {PeriodicalExecuter} executer Periodical executer to handle fast forwarding
 * @author erik 2/2/2008
 */
Sureflix.Plugins.RealPlayerVideo = Class.create(Sureflix.Plugins.Video, {


	initialize: function($super, interval, speeds) {
		$super(interval, speeds);
		this.platform = Sureflix.Plugins.Detector.platforms["RealPlayer"];
		this.rate = 1;
		this.executer = null;
		this.nearEnd = false;
	},


	getEmbedHtml: function($super, extraQSParams) {
		var str;
		Sureflix.Plugins.Detector.detectBrowser();

		if (Sureflix.Plugins.Detector.browser == "Explorer" && Sureflix.Plugins.Detector.OS == "Windows") {
			var str = '' +
			'<OBJECT ' +
			'id="#{elementID}" ' +
			'name="#{elementID}" ' +
			'classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" ' +
			'width="#{width}" ' +
			'height="#{height}" ' +
			'>\n' +
			'	<param name="NOJAVA" value="false">\n' +
			'	<param name="CONTROLS" value="ImageWindow">\n' +
			'	<param name="IMAGESTATUS" value="false">\n' +
			'	<param name="AUTOSTART" value="true">\n' +
			'</OBJECT>';
		} else {
			var str = '' +
			'	<EMBED ' +
			'	id="#{elementID}" ' +
			'	name="#{elementID}" ' +
			'	type="audio/x-pn-realaudio-plugin"' +
			'	width="#{width}" ' +
			'	height="#{height}" ' +
			'	nojava="false" ' +
			'	controls="ImageWindow" ' +
			'	imagestatus="false" ' +
			'	autostart="true" ' +
			'	>\n'
		}

		return str;
	},


	setMedia: function($super, mediaUrl) {
		try {
			this.nearEnd = false;
			this.mediaUrl = mediaUrl;
			this.vid.SetSource(mediaUrl);
			this.play();
			this.vid.SetWantKeyboardEvents(true);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	getPlayState: function($super) {
		var playStateCode;
		var playStateText;

		try {
			playStateCode = this.vid.GetPlayState();

			switch (playStateCode) {
				case 0: playStateText = this.nearEnd ? "Video Ended" : "Stop";
					break;
				case 1: playStateText = "Connecting";
					break;
				case 2: playStateText = "Buffering";
					break;
				case 3: playStateText = "Play";
					break;
				case 4: playStateText = "Pause";
					break;
				case 5: playStateText = "Seeking";
					break;
				default: playStateText = "RM PlayState " + playStateCode;
			}
			if (this.rate > 1) {
				playStateText = "Fast Forward"
			}
			//Detect Video Ended


		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}

		return (playStateText);
	},


	canPause: function($super) {
		try {
			//return (this.vid.CanPause());
			return Sureflix.Utilities.getMethodValue(this.vid, 'CanPause', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	canPlay: function($super) {
		try {
			//return (this.vid.CanPlay());
			return Sureflix.Utilities.getMethodValue(this.vid, 'CanPlay', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	canStop: function($super) {
		try {
			//return (this.vid.CanStop());
			return Sureflix.Utilities.getMethodValue(this.vid, 'CanStop', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	pause: function($super) {
		try {
			this.vid.DoPause();
			//return Sureflix.Utilities.getMethodValue(this.vid, 'DoPause', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	play: function($super) {
		try {
			//this.vid.SetImageStatus(false);
			//this.vid.DoPlay();
			return Sureflix.Utilities.getMethodValue(this.vid, 'DoPlay', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	stop: function($super) {
		try {
			//this.vid.DoStop();
			return Sureflix.Utilities.getMethodValue(this.vid, 'DoStop', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	getRate: function($super) {
		try {
			//return this.rate;
			return Sureflix.Utilities.getPropertyValue(this, 'rate', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	setRate: function($super, rate) {
		rate = parseFloat(rate);

		try {
			this.rate = rate;

			if (this.executer != null) {
				this.executer.stop();
				this.executer = null;
				this.vid.DoPlay();
				this.vid.SetImageStatus(true);
			}

			if (rate > 1) {
				this.executer = new PeriodicalExecuter(function(pe) { pe.video.doPeriodicalFF() }, this.interval / 1000);
				this.executer.video = this;
				this.vid.SetImageStatus(false);
			}
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	doPeriodicalFF: function() {
		if (this.rate <= 1) {
			this.setRate(1);
			return;
		}

		var pos = this.vid.GetPosition();
		pos += this.interval * this.rate;

		if (pos >= this.vid.GetLength()) {
			this.setRate(1);
			this.vid.DoStop();
			return;
		} else {
			this.vid.SetPosition(pos);
		}
		this.vid.DoPlay();
		this.vid.DoPause();
	},


	getDownloadProgress: function($super) {
		return (0);
	},


	getConnectionBandwidth: function($super) {
		try {
			//return this.vid.GetConnectionBandwidth();
			return Sureflix.Utilities.getMethodValue(this.vid, 'GetConnectionBandwidth', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	getAverageBandwidth: function($super) {
		try {
			//return this.vid.GetBandwidthAverage();
			return Sureflix.Utilities.getMethodValue(this.vid, 'GetBandwidthAverage', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
			return 0;
		}
	},


	getCurrentBandwidth: function($super) {
		try {
			//return this.vid.GetBandwidthCurrent();
			return Sureflix.Utilities.getMethodValue(this.vid, 'GetBandwidthCurrent', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
			return 0;
		}
	},


	getFullScreen: function($super) {
		try {
			//return this.vid.GetFullScreen();
			return Sureflix.Utilities.getMethodValue(this.vid, 'GetFullScreen', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	setFullScreen: function($super) {
		try {
			this.vid.SetFullScreen();
		} catch (e) {
			// we don't want to throw an error if they click 
			// fullscreen and the movie isn't playing yet
			alert(FullScreenMessage);
		}
	},


	getMute: function($super) {
		try {
			//return this.vid.GetMute();
			return Sureflix.Utilities.getMethodValue(this.vid, 'GetMute', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	setMute: function($super, mute) {
		try {
			this.vid.SetMute(mute);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	getPacketsMissing: function($super) {
		try {
			//return this.vid.GetPacketsMissing();
			return Sureflix.Utilities.getMethodValue(this.vid, 'GetPacketsMissing', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	getBuffering: function($super) {
		try {
			if (this.vid.GetBufferingTimeElapsed() <= 0) {
				return "";
			} else {
				return Math.round(this.vid.GetBufferingTimeRemaining() / 1000) + "s";
			}
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	getPlatformName: function($super) {
		return ("RealPlayer");
	},


	getVersionInfo: function($super) {
		try {
			//return this.vid.GetVersionInfo();
			return Sureflix.Utilities.getMethodValue(this.vid, 'GetVersionInfo', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	getLength: function($super) {
		//wm gets length in seconds, rm gets length in milliseconds
		//this function always returns the length of the clip in seconds

		try {
			return this.vid.GetLength() / 1000;
		} catch (e) {
			return 0;
		}
	},


	getPosition: function($super) {
		try {
			if (this.vid.GetPosition() > 0) {
				this.nearEnd = (this.vid.GetLength() - this.vid.GetPosition()) < 4000;
			}

			//getPosition() should always return seconds, so convert milliseconds to seconds
			return this.vid.GetPosition() / 1000;
		} catch (e) {
			return 0;
		}
	},


	setPosition: function($super, seconds) {
		try {
			this.vid.SetPosition(seconds * 1000);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	getVolume: function($super) {
		try {
			//return this.vid.GetVolume();
			return Sureflix.Utilities.getMethodValue(this.vid, 'GetVolume', null);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	setVolume: function($super, volume) {
		try {
			if (volume < 0) {
				volume = 0;
			} else if (volume > 100) {
				volume = 100;
			}

			this.vid.SetVolume(volume);
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	isPaused: function($super) {
		try {
			return this.vid.GetPlayState() == 4;
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	isPlaying: function($super) {
		try {
			return this.vid.GetPlayState() == 3;
		} catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	isStopped: function($super) {
		try {
			return this.vid.GetPlayState() == 0;
		}
		catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	}


});
