/*
 * WindowsMediaPlayer
 * copyright 2005 Axisto Media Ltd. All rights reserved.
 * by Andreas Rimbe, Axisto Media Ltd (andreas.rimbe@axisto.com, http://www.axisto.com/)
 *
 * v1.5 - 06-05-2005
 *
 * Creates a windows media player object
 *
 * Usage:
 *
 *	wmp = new FlashObject("divWhereCreateMP", width, height);
 *	wmp.setUrl("");
 *
 */
WindowsMediaPlayer = function(divId, id, width, height, version)
{
	this.id = id ? id : "mp";
	this.divId = divId ? divId : "video";
	this.audioOnlyImageId = 'audioonly';

	this.autoStart = true;
	this._wmp64PreRollTime = 0;
	this.currentPosition = 0;
	this.bufferProgress = 0;
	this.eventTimer = null;
	this.eventUpdateInterval = 500;
	this.isNSPlugin = false;			// NS<5 and WMP 6.4
	this.isNSApplet = false;			// NS > 6 and WMP v9+
	this.isWMP64 = false;
	this.playState = 0;
	
	this.useCallbacks = true;
	
	this.version = version ? version.toLowerCase() : "";

	if(this.version == 'wmp_64')
	{
		this.isWMP64 = true;
	}
	else if(this.version == 'wmp_plugin')
	{
		this.isNSPlugin = true;
	}
	else if(this.version == 'wmp_applet')
	{
		this.isNSApplet = true;
	}
	this.width = width ? width : 320;
	this.height = height ? height : 240;
	this.origHeight = this.height;
	this.uiMode = "";
	this.volume = 70;
	//do detection
	this.wmpInfo = detectWMP();
	if(this.wmpInfo.installed)
	{
		if(this.wmpInfo.versionInfo == "6.4")
		{
			this.isWMP64 = true;
		}
		else if(this.wmpInfo.versionInfo == "PluginVersion")
		{
			this.isNSPlugin = true;
		}
	}
	else
	{
		//fix for ff on win
		if(navigator.userAgent.indexOf('Windows') != -1 && isFF)
		{
			this.wmpInfo.installed = true;
			this.isNSPlugin = false;
			this.isNSApplet = false;
			this.isWMP64 = false;
		}
		else
		{
			alert("You have selected Windows Media Player option, which is not installed on your system. Please select another player or alternatively download and install Windows Media Player from http://www.microsoft.com/windows/windowsmedia/mp10/default.aspx ");
		}
	}
}
WindowsMediaPlayer.prototype.write = function()
{
	if(this.wmpInfo.installed)
	{
		var s = this.createHtml();
		writeLayer(this.divId, null, s);
		this.mp = findObj(this.id);
	}
}
WindowsMediaPlayer.prototype.destroy = function()
{
	if(this.eventTimer != null)
	{
		clearTimeout(this.eventTimer);
		this.eventTimer = null;
	}
	this.mp = findObj(this.id);

	if(isIE && this.mp != null)
	{
		this.stop();
		var p = findObj(this.divId);
		try
		{
			p.removeChild(this.mp);
		}
		catch(e)
		{
		}
		this.mp = null;
	}
	else
	{
		this.mp = null;
	}
}
WindowsMediaPlayer.prototype.createHtml = function()
{
	var strWMP = "";

	if(isIE)
	{
		if(this.isWMP64)
		{
			strWMP = '<object id="'+this.id+'" classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95"';
			strWMP += ' width="'+this.width+'" height="'+this.height+'"';
			strWMP += '	STANDBY="Loading Windows Media Player components..."';
			strWMP += '	TYPE="application/x-oleobject" VIEWASTEXT>';
			strWMP += '<param name="AutoStart" value="'+this.autoStart.toString()+'">';
			strWMP += '<param name="AutoSize" value="1">';
			strWMP += '<param name="DisplaySize" value="0">';

			if(this.uiMode == "full" || this.uiMode == "audio")
			{
				strWMP += '<param name="ShowControls" value="1">';
				strWMP += '<param name="ShowStatusBar" value="1">';
			}
			else
			{
				strWMP += '<param name="ShowControls" value="0">';
				strWMP += '<param name="ShowStatusBar" value="0">';
			}
			
			if(this.currentPosition > 0)
			{
				strWMP += '<param name="CurrentPosition" value="' + this.currentPosition + '">';
			}
			strWMP += '<param name="Volume" value="-300">';
			strWMP += '</object>';
		}
		else
		{
			//WMP7+
			strWMP = '<object id="'+this.id+'" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"';
			strWMP += ' width="'+this.width+'" height="'+this.height+'">';
			strWMP += '<param name="autostart" value="'+this.autoStart.toString()+'" />';
			if(this.uiMode != "audio")
			{
				strWMP += '<param name="uiMode" value="'+this.uiMode+'" />';
			}
			if(this.url != null)
			{
				strWMP += '<param name="url" value="'+this.url+'" />';
			}
			if(this.currentPosition > 0)
			{
				strWMP += '<param name="currentposition" value="' + this.currentPosition + '">';
			}
			strWMP += '<param name="volume" value="' + this.volume + '" />';
			strWMP += '</object>';
		}
	}
	else
	{
		//WMP7+
		if(this.wmpInfo.scriptable)
		{
			strWMP = '<object id="'+this.id+'" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"';
			strWMP += ' width="'+this.width+'" height="'+this.height+'">';
			strWMP += '<param name="autoStart" value="' + this.autoStart.toString() + '" />';
			if(this.uiMode != "audio")
			{
				strWMP += '<param name="uiMode" value="'+this.uiMode+'" />';
			}
			if(this.url != null)
			{
				strWMP += '<param name="url" value="'+this.url+'" />';
			}
			strWMP += '<param name="volume" value="' + this.volume + '" />';
			strWMP += '</object>';
		}
		else
		{
			strWMP = '<object id="'+this.id+'" type="video/x-ms-wmv"';
			if(this.url != null)
			{
				strWMP += ' data="'+this.url+'"';
			}
			strWMP += ' width="'+this.width+'" height="'+this.height+'">';
			strWMP += '<param name="autoStart" value="'+this.autoStart.toString()+'" />';
			if(this.uiMode != "audio")
			{
				strWMP += '<param name="uiMode" value="'+this.uiMode+'" />';
			}
			strWMP += '<param name="volume" value="70" />';
			strWMP += '</object>';
		}
	}
	
	//events
	strWMP += '\n<script language="javascript" for="' + this.id + '" event="ScriptCommand(Type, Param)">';
	strWMP += '\nplayer_event("scriptCommand", Type, Param);';
	strWMP += '\n</script>';
	
	return strWMP;
}
WindowsMediaPlayer.prototype.checkState = function()
{
	//document scope
	var x = this.getPlayState();
	if(x != null && (x == 6 || x != this.playState))
	{
		this.playState = x;
		if(this.playStateChange != null)
		{
			this.playStateChange(this.playState);
		}
	}
	var y = this.getPosition();
	if(y != null && y > 0 && y != this.currentPosition)
	{
		this.currentPosition = y;
		if(this.positionChange != null)
		{
			this.positionChange(this.currentPosition);
		}
	}
	/*
	if(this.playState != 1)
		this.eventTimer = setTimeout(this.mp.checkState, this.mp.eventUpdateInterval);
	else{
		clearInterval(this.mp.eventTimer);
		this.mp.eventTimer = null;
	}
	*/
	this.eventTimer = setTimeout(this.id+".checkState()", this.eventUpdateInterval);
}
WindowsMediaPlayer.prototype.getDuration = function()
{
	var x = 0;
	try
	{
		if(this.isNSPlugin)
		{
 			x = this.mp.GetDuration()+1;
		}
		else if (this.isWMP64)
		{
			x = this.mp.Duration+1;
		}
		else
		{
			if(this.isNSApplet)
			{
				x = this.mp.getCurrentMedia().getDuration();
			}
			else
			{
				x = this.mp.currentMedia.duration;
			}
		}
	}
	catch(e)
	{
	}
	return x;
}
WindowsMediaPlayer.prototype.getPlayState = function()
{
	var ps = 0;
	try
	{
		if(this.isWMP64 || this.isNSPlugin)
		{
 			if(this.isNSPlugin)
 			{
	 			ps = this.mp.GetPlayState()+1;
			}
			else if (this.isWMP64)
			{
				ps = this.mp.PlayState+1;
			}
 			if(ps == 4)
 			{
 				//check buffering
	 			ps = 6;
	 			var x = this.getBufferingProgress();
	 			if(x != 0 && x <= 100 && x != this.bufferProgress)
	 			{
	 				this.bufferProgress = x;
	 			}
 			}
		}
		else
		{
			ps = this.mp.playState;
			if(ps == 6)
			{
				this.bufferProgress = this.getBufferingProgress();
			}
		}
	}
	catch(e)
	{
	}
	return ps;
}
// CurrentPosition
WindowsMediaPlayer.prototype.getPosition = function()
{
	var pos = -1;
	try
	{
		if(this.isNSPlugin)
		{
 			pos =  this.mp.GetCurrentPosition();
		}
		else if (this.isWMP64)
		{
			pos = this.mp.CurrentPosition;
		}
		else
		{
			// If using v7+ interface set through applet's setXXX routine or
			// by setting property directly
			if(this.isNSApplet)
			{
				pos = this.mp.getControls().getCurrentPosition();
			}
			else
			{
				pos = this.mp.controls.currentPosition;
			}
		}
	}
	catch(e)
	{
	}
	return pos;
}
WindowsMediaPlayer.prototype.setPosition = function(time)
{
	try
	{
		this.currentPosition = time;
		if(this.isNSPlugin)
		{
 			if(this.mp.GetPlayState() == 0 || this.mp.GetPlayState() == 1 || this.mp.GetPlayState() == 2)
 			{
				if(time-this._wmp64PreRollTime>=0)
				{
 					time -= this._wmp64PreRollTime;
 				}
 				if(this.mp.GetCurrentPosition() != time)
 				{
 					this.mp.SetCurrentPosition(time);
 				}
 			}
 			else
 			{
				this.currentPosition = time;
				return;
			}
		}
		else if (this.isWMP64)
		{
			if(this.mp.PlayState == 0 || this.mp.PlayState == 1 || this.mp.PlayState == 2)
			{
				if(time-this._wmp64PreRollTime>=0)
				{
 					time -= this._wmp64PreRollTime;
 				}
				if(this.mp.CurrentPosition != time)
				{
					this.mp.CurrentPosition = time;
				}
			}
			else
			{
				this.currentPosition = time;
			}
		}
		else
		{
			// If using v7+ interface set through applet's setXXX routine or
			// by setting property directly
			if(this.isNSApplet)
			{
				if(this.mp.getControls().isAvailable('currentPosition'))
				{
					this.mp.getControls().setCurrentPosition(time);
				}
				else
				{
					this.currentPosition = time;
				}
			}
			else
			{
				if(this.mp.controls.isAvailable('currentPosition'))
				{
					this.mp.controls.currentPosition = time;
				}
				else
				{
					this.currentPosition = time;
				}
			}
		}
	}
	catch(e)
	{
	}
}
WindowsMediaPlayer.prototype.setZoom = function(p)
{
	var w = this.width*(p/100.0);
	w = w>0?w:1;
	var h = this.height*(p/100.0);
	h = h>0?h:1;
	this.setSize(w, h);
	
	//hide audioonly image
	var img = findObj(this.audioOnlyImageId);
	if(p == 0 && img != null)
	{
		img.style.display = 'none';
		//alert("hide image" + img);
	}
	else if(img != null && (this.uiMode == "audio" || this.uiMode == "invisible"))
	{
		img.style.display = 'block';
		//alert("show image" + img);
	}
}
WindowsMediaPlayer.prototype.setSize = function(w, h)
{
	if(this.mp != null)
	{
		if(document.all && !document.getElementById)
		{
			document.all[this.id].style.pixelWidth = w;
			document.all[this.id].style.pixelHeight = h;
		}
		else
		{
			document.getElementById(this.id).style.width = w+"px";
			document.getElementById(this.id).style.height = h+"px";
		}
	}
	else
	{
		this.height = 1;
		this.width = 1;
	}
}
WindowsMediaPlayer.prototype.setUIMode = function(mode)
{
	if(this.uiMode != mode)
	{
		this.uiMode = mode;
		switch(mode)
		{
			case "audio":
			{
				if(this.isWMP64)
				{
					this.height = 70;
				}
				else
				{
					this.height = 63;
				}
				break;
			}
			case "none":
			{
				this.height = this.origHeight;
				break;
			}
			case "full":
			{
				if(this.isWMP64)
				{
					this.height = this.origHeight+70;
				}
				else
				{
					this.height = this.origHeight+65;
				}
				break;
			}
			case "invisible":
			{
				this.setZoom(0);
				break;
			}
		}
		if(this.mp != null)
		{
			this.setSize(this.width, this.height);
		}
		
		var img = findObj(this.audioOnlyImageId);
		if(img != null)
		{
			if((mode == "audio" || mode == "invisible") && img.src != "")
			{
				img.style.display = 'block';
				img.style.visibility = 'visible';
			}
			else
			{
				img.style.display = 'none';
				img.style.visibility = 'hidden';
			}
		}
		//audio - show image and controls
		//none - video window only
		//full - with controls
		//invisible
	}
}
WindowsMediaPlayer.prototype.setImage = function(url)
{
	var img = findObj("audioonly-image");
	if(img != null && url != null && url != "")
	{
		//alert(url + this.uiMode);
		img.src = url;
		if(this.uiMode == "audio" || this.uiMode  == "invisible")
		{
			img.style.display = 'block';
		}
	}	
}
// URL
WindowsMediaPlayer.prototype.setUrl = function(url)
{
	this.url = url;
	if(this.mp != null)
	{
		try
		{
			if(this.isNSPlugin)
			{
 				this.mp.SetFileName(url);
			}
			else if (this.isWMP64)
			{
				this.mp.FileName = url;
			}
			else
			{
				// If using v7+ interface set through applet's setXXX routine or
				// by setting property directly
				if(this.isNSApplet)
				{
					this.mp.setURL(url);
				}
				else
				{
					this.mp.URL = url;
				}
			}
			
			if(this.useCallbacks && this.eventTimer == null)
			{
				this.eventTimer = setTimeout(this.id+".checkState()", this.eventUpdateInterval);
			}
		}
		catch(e)
		{
		}
	}
}
// Volume
WindowsMediaPlayer.prototype.getVolume = function()
{
	var vol = -1;
	try
	{
		if(this.isNSPlugin)
		{
 			vol = this.mp.GetVolume();
		}
		else if (this.isWMP64)
		{
			vol = this.mp.Volume;
		}
		else
		{
			// If using v7+ interface set through applet's setXXX routine or
			// by setting property directly
			if(this.isNSApplet)
			{
				vol = this.mp.getSettings().getVolume();
			}
			else
			{
				vol = this.mp.settings.volume;
			}
		}
	}
	catch(e)
	{
	}
	return vol;
}
WindowsMediaPlayer.prototype.setVolume = function(level)
{
	try
	{
		if(level > 0 && level <= 100 && level != "")
		{
			var mp64Level = (parseInt(level) > 40) ? 25*(parseInt(level))-2500 : (100*(parseInt(level))-6000);
			if(parseInt(level) == 0)
			{
				mp64Level = -10000;
			}
			if(mp64Level > 0)
			{
				mp64Level = 0;
			}
			if(this.isNSPlugin)
			{
 				this.mp.SetVolume(mp64Level);
			}
			else if (this.isWMP64)
			{
				this.mp.Volume = mp64Level;
			}
			else
			{
				// If using v7+ interface set through applet's setXXX routine or
				// by setting property directly
				if(this.isNSApplet)
				{
					this.mp.getSettings().setVolume(level);
				}
				else
				{
					this.mp.settings.volume = level;
				}
			}
		}
	}
	catch(e)
	{
	}
}
WindowsMediaPlayer.prototype.getErrorDescription = function(){
	var desc = "";
	if(this.isNSPlugin)
	{
 		desc = this.mp.GetErrorDescription();
	}
	else if (this.isWMP64)
	{
		desc = this.mp.ErrorDescription;
	}
	else
	{
		// If using v7+ interface set through applet's setXXX routine or
		// by setting property directly
		if(this.isNSApplet)
		{
			if(this.mp.getControls().isAvailable('stop'))
			{
				this.mp.getControls().stop();
			}
			if(this.mp.getControls().isAvailable('currentPosition'))
			{
				this.mp.getControls().setCurrentPosition(0);
			}
		}
		else
		{
			if(this.mp.controls.isAvailable('stop'))
			{
				this.mp.controls.stop();
			}
			if(this.mp.controls.isAvailable('currentPosition'))
			{
				this.mp.controls.currentPosition = 0;
			}
		}
	}
}
// PLAYBACK
WindowsMediaPlayer.prototype.stop = function()
{
	if(this.isNSPlugin)
	{
 		if(this.mp.GetPlayState() != 0)
		{
 				this.mp.Stop();
 				this.mp.SetCurrentPosition(0);
		}
	}
	else if (this.isWMP64)
	{
		if(this.mp.PlayState != 0)
		{
 				//alert("WMP64 Stop");
 				this.mp.Stop();
 				this.mp.CurrentPosition = 0;
		}
	}
	else
	{
		// If using v7+ interface set through applet's setXXX routine or
		// by setting property directly
		if(this.isNSApplet)
		{
			if(this.mp.getControls().isAvailable('stop'))
			{
				this.mp.getControls().stop();
			}
			if(this.mp.getControls().isAvailable('currentPosition'))
			{
				this.mp.getControls().setCurrentPosition(0);
			}
		}
		else
		{
			if(this.mp.controls.isAvailable('stop'))
			{
				this.mp.controls.stop();
			}
			if(this.mp.controls.isAvailable('currentPosition'))
			{
				this.mp.controls.currentPosition = 0;
			}
		}
	}
}
WindowsMediaPlayer.prototype.pause = function()
{
	if(this.playState == 2)
	{
		this.play();
		return;
	}
	if(this.isNSPlugin)
	{
 		if(this.mp.GetPlayState() == 2)
 		{
 			this.mp.Pause();
 		}
	}
	else if (this.isWMP64)
	{
		if(this.mp.PlayState == 2)
		{
 			this.mp.Pause();
 		}
	}
	else
	{
		// If using v7+ interface set through applet's setXXX routine or
		// by setting property directly
		if(this.isNSApplet)
		{
			if(this.mp.getControls().isAvailable('pause'))
			{
				this.mp.getControls().pause();
			}
			else
			{
				this.mp.controls.stop();
			}
		}
		else
		{
			if(this.mp.controls.isAvailable('pause'))
			{
				this.mp.controls.pause();
			}
			else
			{
				this.mp.controls.stop();
			}
		}
	}
}
WindowsMediaPlayer.prototype.play = function()
{
	try
	{
		if(this.playState == 3)
		{
			this.pause();
			return;
		}
		if(this.isNSPlugin)
		{
 			if(this.mp.GetPlayState() != 2)
 			{
 				this.mp.Play();
 			}
		}
		else if (this.isWMP64)
		{
			if(this.mp.PlayState != 2)
			{
 				this.mp.Play();
 			}
		}
		else
		{
			// If using v7+ interface set through applet's setXXX routine or
			// by setting property directly
			if(this.isNSApplet)
			{
				if(this.mp.getControls().isAvailable('play'))
				{
					this.mp.getControls().play();
				}
			}
			else if(this.mp.controls != null)
			{
				if(this.mp.controls.isAvailable('play'))
				{
					this.mp.controls.play();
				}
			}
		}
		
		if(this.useCallbacks && this.eventTimer == null)
		{
			this.eventTimer = setTimeout(this.id+".checkState()", this.eventUpdateInterval);
		}
	}
	catch(e)
	{
	}
}
///CALLBACKS
WindowsMediaPlayer.prototype.getBufferingProgress = function() {
	var x = 0;
	if(this.isNSPlugin)
	{
 		x = this.mp.GetBufferingProgress();
	}
	else if (this.isWMP64)
	{
		x = this.mp.BufferingProgress;
	}
	else
	{
		if(this.isNSApplet)
		{
			if(this.mp.network.getBufferingProgress() >= 0)
			{
				x = this.mp.network.getBufferingProgress();
			}
			else if(this.mp.network.getDownloadProgress() >= 0)
			{
				x = this.mp.network.getDownloadProgress();
			}
		}
		else
		{
			if(this.mp.network.bufferingProgress >= 0)
			{
				x = this.mp.network.bufferingProgress;
			}
			else if(this.mp.network.downloadProgress >= 0)
			{
				x = this.mp.network.downloadProgress;
			}
		}
	}
	return x;
}
WindowsMediaPlayer.prototype.error = function()
{
	var errorMsg = this.getErrorDescription();
	//alert("Windows Media Player: " + errorMsg);
}
WindowsMediaPlayer.prototype.scriptCommand = function(type, data)
{
	//alert("WMP: ScriptCommand " + type + " " + data);
	if(this.scriptChange != null)
	{
		this.scriptChange(type, data);
	}
}
