/**
 * @class This object handles low level video functionality for the Windows Media platform.
 * @author erik 2/2/2008
 */
Sureflix.Plugins.WindowsMediaVideo = Class.create(Sureflix.Plugins.Video, {
	
	initialize: function($super, interval, speeds) {
		$super(interval, speeds);
		this.platform = Sureflix.Plugins.Detector.platforms["WindowsMedia"];
		this.nearEnd = false;
	},
	
	
	setMedia: function ($super, mediaUrl) {
		try {
			this.nearEnd = false;
			this.mediaUrl = mediaUrl;
			this.vid.URL = mediaUrl;
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},


	getEmbedHtml: function($super, extraQSParams) {
		var detector = Sureflix.Plugins.Detector;
		detector.detectBrowser();

		if (detector.browser == "Firefox" && detector.OS == "Windows") {
			return this.port25ObjectHtml();
		} else if (detector.browser == "Explorer" && detector.OS == "Windows") {
			return this.windowsMediaObjectHtml();
		} else {
			return this.windowsMediaEmbedHtml();
		}
	},
	
	
	port25ObjectHtml: function() {
		var str =	'<object ' +
					'id="#{elementID}" ' +
					'type="application/x-ms-wmp" ' +
					//'data="#{mediaUrl}" ' +
					'width="#{width}" ' +
					'height="#{height}" ' +
					'">\n' +
					//'	<param name="URL" value="#{mediaURL}" />\n' +
					'	<param name="uiMode" value="none" />\n' +
					'	<param name="autoStart" value="true" />\n' +
					'	<param name="stretchToFit" value="true" />\n' +
					'	<param name="windowlessVideo" value="true" />\n' +
					'</object>\n';
		
		return str;
	},
	
	
	/** 
	 * Returns the HTML template for the Windows Media object.
	 */
	windowsMediaObjectHtml: function() {
		var str =	'<object ' +
					'id="#{elementID}" ' +
					'classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" ' +
					'width="#{width}" ' +
					'height="#{height}" ' +
					'>\n' +
					'	<param name="uiMode" value="none">\n' +
					'	<param name="autoStart" value="true">\n' +
					'	<param name="stretchToFit" value="true">\n' +
					'</object>\n';
		
		return str;
	},
	
	/**
	 * Returns the HTML template for the Windows Media embed.
	 */
	windowsMediaEmbedHtml: function () {
		var str =	'' +
					'<EMBED ' +
					'id="#{elementID}" ' +
					'name="#{elementID}" ' +
					'type="application/x-mplayer2"' +
					'width="#{width}" ' +
					'height="#{height}" ' +
					'uiMode="none" ' +
					'autostart="true" ' +
					'stretchToFit="true" ' +
					'>\n'
	
		return str;
	},	
	
	
	getPlayState: function($super) {
		var playStateCode;
		var playStateText;
		
		try {
			playStateCode = this.vid.playState;
			
			switch (playStateCode) {
				case 0:		playStateText = "Other";
							break;
				case 1:		playStateText = this.nearEnd ? "Video Ended" : "Stop";
							break;
				case 2:		playStateText = "Pause";
							break;
				case 3:		playStateText = "Play";
							break;
				case 4:		playStateText = "Fast Forward";
							break;
				case 5:		playStateText = "Fast Rewind"; // not used
							break;
				case 6:		playStateText = "Buffering";
							break;
				case 7:		playStateText = "Connecting"; //Waiting
							break;
				case 8:		playStateText = "Video Ended"; //Media Ended
							break;
				case 9:		playStateText = "Connecting"; //Transitioning
							break;
				case 10:	playStateText = "Ready"; //Ready
							break;
				case 11:	playStateText = "Connecting"; //Reconnecting
							break;
				default:	playStateText = "WM PlayState " + playStateCode;
			}
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	
		return(playStateText);
	},
	
	canPause: function($super) {
		try {
			return (this.vid.controls.isAvailable('Pause'));
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	

	canPlay: function($super) {
		try {
			return (this.vid.controls.isAvailable('Play'));
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	
	canStop: function($super) {
		try {
			return (this.vid.controls.isAvailable('Stop'));
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	

	pause: function($super) {
		try {
			//this.vid.controls.pause();
			return Sureflix.Utilities.getMethodValue(this.vid.controls, 'pause', null);
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	

	play: function($super) {
		try {
			//this.vid.controls.play();
			return Sureflix.Utilities.getMethodValue(this.vid.controls, 'play', null);
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	

	stop: function($super) {
		try {
			//this.vid.controls.stop();
			return Sureflix.Utilities.getMethodValue(this.vid.controls, 'stop', null);
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	
	
	getRate: function($super) {
		try {
			//return this.vid.settings.rate;
			return Sureflix.Utilities.getPropertyValue(this.vid.settings, 'rate', null);
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	

	setRate: function($super, rate) {
		rate = parseFloat(rate);
	
		try {
			this.vid.settings.rate = rate;
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	
	
	getDownloadProgress: function($super) {
		try {
			if (this.vid.network.downloadProgress) {
				dp = this.vid.network.downloadProgress;
				if ((dp == 100) && (downloadProgress <= 0)) 
					return (0)
				else return (dp);
			} else {
				return (0);
			}
		} catch(e) {
			return(0);	// if anything fails, return 0
		}
	},
	

	getConnectionBandwidth: function($super) {
		try {
			//return this.vid.network.bitRate;
			return Sureflix.Utilities.getPropertyValue(this.vid.network, 'bitRate', null);
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	

	getAverageBandwidth: function($super) {
		try {
			//return this.vid.network.bandWidth;
			return Sureflix.Utilities.getPropertyValue(this.vid.network, 'bandWidth', null);
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	
	
	getCurrentBandwidth: function($super) {
		try {
			//return this.vid.network.bandWidth;
			return Sureflix.Utilities.getPropertyValue(this.vid.network, 'bandWidth', null);
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	
	getFullScreen: function($super) {
		try {
			//return this.vid.fullScreen;
			return Sureflix.Utilities.getPropertyValue(this.vid, 'fullScreen', null);
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	

	setFullScreen: function($super) {
		try {
			this.vid.fullScreen = true;
		} catch(e) {
			// we don't want to throw an error if they click 
			// fullscreen and the movie isn't playing yet
			//*** FullScreenMessage phrase missing
			alert(FullScreenMessage);
		}
	},
	

	getMute: function($super) {
		try {
			//return this.vid.settings.mute;
			return Sureflix.Utilities.getPropertyValue(this.vid.settings, 'mute', null);
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},		
	
	setMute: function($super, mute) {
		try {
			this.vid.settings.mute = mute;
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	

	getPacketsMissing: function($super) {
		try {
			//return this.vid.network.lostPackets;
			return Sureflix.Utilities.getPropertyValue(this.vid.network, 'lostPackets', null);
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	
	
	getBuffering: function($super) {
		try {
			//return this.vid.network.bufferingProgress;
			return Sureflix.Utilities.getPropertyValue(this.vid.network, 'bufferingProgress', null) + "%";
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
			

	getPlatformName: function($super) {
		try {
			return "Windows Media";
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
			

	getVersionInfo: function($super) {
		try {
			//return this.vid.versionInfo;
			return Sureflix.Utilities.getPropertyValue(this.vid, 'versionInfo', null);
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	

	getLength: function($super) {
		try {
			if (this.vid.currentMedia != null) {
				return this.vid.currentMedia.duration;
			} else {
				return 0;
			}
		} catch(e) {
			return 0;
		}
	},
	

	getPosition: function($super) {
		try {
			if (this.vid.controls.currentPosition > 0) {
				this.nearEnd = (this.vid.currentMedia.duration - this.vid.controls.currentPosition) < 4;
			}
			return this.vid.controls.currentPosition;
		} catch (e) {
			return 0;
		}
	},
	
	
	setPosition: function($super, seconds) {
		seconds = parseFloat(seconds);
		try {
			this.vid.controls.currentPosition = seconds;
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
		

	getVolume: function($super) {
		try {
			//return this.vid.settings.volume;
			return Sureflix.Utilities.getPropertyValue(this.vid.settings, 'volume', 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.settings.volume = volume;
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	

	isPaused: function($super) {
		try {
			return this.vid.playState == 2;
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
	

	isPlaying: function($super) {
		try {
			return this.vid.playState == 3;
		} catch(e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	},
		

	isStopped: function($super){
		try {
			return this.vid.playState == 1;
		}
		catch (e) {
			Sureflix.Error.ErrorHandler.consoleError(e);
		}
	}
	
	
});
