var MediaPlayer = Class.create();

MediaPlayer.prototype = {
	
	control: null
	,displayEl: null
	,playing: false
	,timer: null
	
	,initialize: function(mediaUri, displayEl, controlContainerEl) {
		
		// create the control
		var control = document.createElement('object');
		// control.setAttribute('height', 300);
		// control.setAttribute('width', 300);
		control.setAttribute('classid', 'CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6');
		
		if (controlContainerEl) {
			controlContainerEl.appendChild(control);
		} else {
			document.body.appendChild(control);
		}
		
		// and configure it
		control.settings.autoStart = false;
		control.uiMode = 'invisible';
		
		this.control = control;
		this.displayEl = displayEl;
		this.playing = false;
		this.timer = null;
		
		this.display('Gestopt');
		
		var obj = this;
		
		control.attachEvent(
			"PlayStateChange"
			,function() { 
					
				switch (control.playState) {
					case 10:	// Ready to begin playing.
					case 1:		// Playback of the current media item is stopped.
						obj.display("Gestopt");
						break;
						
					case 3:		// The current media item is playing.
						obj.monitorTime();
						break;
						
					case 6:		// The current media item is getting additional data from the server.
						obj.monitorBuffer();
						break;
						
					case 7:		// Connection is established, but the server is not sending data. Waiting for session to begin.
						obj.display('Verbinden');
						break;
				}	
			}
		);
		
		control.attachEvent(
			"OpenStateChange"
			,function() {
				
				switch (control.openState) {
					case 10: // Connecting to the server that holds the media item.
					case 12: // Media item has been retrieved and is now being opened.
						obj.display("Verbinden");
						break;	
				}
			}
		);
		
		var Media = control.newMedia(mediaUri);
		this.control.currentMedia = Media;	
	}
	
	,setUiMode: function(uiMode) {
		this.control.uiMode = uiMode;
	}
	
	,setVolume: function(volume) {
		this.control.Settings.volume = volume;
	}
	
	,getControl: function() {
		return this.control;
	}
	
	,display: function(str) {
		if (this.displayEl)
			this.displayEl.innerHTML = str;	
	}
	
	,play: function() {
		this.control.controls.play();
		
		if (this.onPlay)
			this.onPlay(this);
	}
	
	,stop: function() {
		this.control.controls.stop();
		
		if (this.onStop)
			this.onStop(this);
	}
	
	// calbacks
	,onStop: null
	,onStart: null
	
	// monitor methods
	,monitorBuffer: function() {
		this.updateBufferMonitor();
	}
	
	,updateBufferMonitor: function() {
		if (this.control.playState != 6)
			return;
			
		this.display('Laden ' + this.control.network.bufferingProgress + '%');
		
		// while buffering
		if (this.control.playState == 6) {
			var self = this;
			this.timer = setTimeout(
				function() { 
					self.updateBufferMonitor(); 
				}, 
				300
			);
		}
	}
	
	,monitorTime: function() {
		this.updateTimeMonitor();
	}
	
	,updateTimeMonitor: function() {
		
		if (this.control.playState != 3)
			return;
		
		var posStr = this.control.controls.currentPositionString;
		var durationStr = this.control.currentMedia.durationString;
		
		if (! posStr)
			posStr = '00:00';
			
		var str = posStr + '/' + durationStr;
					
		this.display(str);
		
		// while playing
		if (this.control.playState == 3)
			var self = this;
			this.timer = setTimeout(
				function() {
					self.updateTimeMonitor();
				}, 
				500
			);
	
	}
	
};