//------------------------------------------------------
//  Author: Tony Fung
//  Date : July 2002
//  Desc : All Function for Wmp 7 In Object Mode
//  Ver : 1.2
//------------------------------------------------------


//-------------------------
// WMP 7 PlayStatus
//-------------------------
var WMP7_STATE_UNDEFINED = 0;
var WMP7_STATE_STOP = 1;
var WMP7_STATE_PAUSE = 2;
var WMP7_STATE_PLAY = 3;
var WMP7_STATE_FF = 4;
var WMP7_STATE_FR = 5;
var WMP7_STATE_BUFFER = 6;
var WMP7_STATE_WAIT = 7;
var WMP7_STATE_MEDIAEND = 8;
var WMP7_STATE_TRANSITION= 9;
var WMP7_STATE_READY = 10;
var WMP7_STATE_LICENSE = 16;
//-------------------------


//-------------------------------------------
//  Wmp Object , Create and Control WMP 6, 7
//-------------------------------------------

function WmpObject(id,objName)
{
	this.WmpName=objName;
	this.realObj=null;
	this.moviePath="";
	this.width=400;
	this.height=300;
	this.isIE=(navigator.userAgent.indexOf('IE') > -1) && (navigator.platform == "Win32");
	this.NS4 = (document.layers) ? 1 : 0; // just put here first , not use at this time
	this.IE4 = (document.all) ? 1 : 0; // just put here first , not use at this time
	this.id = (this.NS)? "MediaPlayer1":id;
	this.runningLoop = false;
	this.FontColor = "000000";
	
	this.statusWindows=null;
	
	//-------------------------
	//   Wmp status
	//-------------------------
	this.EnableContextMenu=1;
	this.AutoStart=1;
	//-------------------------
	
	//-------------------------
	//   Wmp 7 status
	//-------------------------
	this.UIMode="Full"; // invisible, none, mini, full
	this.StretchToFit=0;
	//-------------------------


	//-------------------------
	//   Set object Function
	//-------------------------
	this.SetMovie = _SetMovie;
	this.ShowWmp7 = _ShowWmp7;
	this.FullScreen = _FullScreen;
	this.Play = _Play;
	this.Stop = _Stop;
	this.Pause = _Pause;
	this.GetMute = _GetMute;
	this.SetMute = _SetMute;
	this.ShowBuffer = _ShowBuffer;
	this.GetBuffer = _GetBuffer;
	this.Display = _Display;
	this.Retry = _Retry;
	this.IsAlive = _IsAlive;
	this.IsStatusWindowUp = _IsStatusWindowUp;
	this.ShowStatusWindow = _ShowStatusWindow;
	this.GetVersionInfo = _GetVersionInfo;
	this.GetPlayState = _GetPlayState;
	this.muteClick = _muteClick;
	this.MM_swapImage = _MM_swapImage;
	this.switchBw = _switchBw;
	this.playStopClick = _playStopClick;
	this.playPauseClick = _playPauseClick;
	this.runLoop = _runLoop;

	//-------------------------

	//-------------------------
	//   Function Code
	//-------------------------
	function _runLoop()
	{
		if(!this.runningLoop)
		{
			var showText = "<SCRIPT language='JavaScript'>window.setInterval(\""+this.id+".Display('"+this.WmpName+"BuffBox',"+this.id+".GetBuffer());\",450);</SCRIPT>";
			document.write(showText);
			//window.setInterval(this.id+".Display( 'this.WmpName+"BuffBox',"+this.id+".GetBuffer());",450);
			this.runningLoop = true;
		}
	}
	function _muteClick(imageName, on_image, off_image)
	{
		this.SetMute();
		if(this.GetMute())
		{
			_MM_swapImage(imageName,'',on_image);
		}
		else
		{
			_MM_swapImage(imageName,'',off_image);
		}
	}
	function _MM_swapImage() { //v3.0
	  var i,j=0,x,a=_MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	   if ((x=_MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
	}
	function _MM_findObj(n, d) { //v4.01
	  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=_MM_findObj(n,d.layers[i].document);
	  if(!x && d.getElementById) x=d.getElementById(n); return x;
	}
	function _switchBw(sBandwidth, v56k_imageName, v56k_onimage, v56k_offimage, vb_imageName, vb_onimage, vb_offimage, stop_imageName, stop_image)
	{
		if(sBandwidth == "bb") 
		{
			this.SetMovie(bbAsx);
			_MM_swapImage(vb_imageName,'',vb_onimage);
			_MM_swapImage(v56k_imageName,'',v56k_offimage);
		}
		else 
		{
			this.SetMovie(nbAsx);
			_MM_swapImage(vb_imageName,'',vb_offimage);
			_MM_swapImage(v56k_imageName,'',v56k_onimage);
		}
	}
	function _playPauseClick(imageName, play_image, pause_image)
	{
		var playState = this.GetPlayState();
			if(playState ==WMP7_STATE_PLAY )
			{
				this.Pause();
				_MM_swapImage(imageName,'',pause_image);
			}
			else
			if(playState ==WMP7_STATE_PAUSE ||playState ==WMP7_STATE_STOP)
			{
				this.Play();
				_MM_swapImage(imageName,'',play_image);
			}
	}
	function _playStopClick(imageName, play_image, stop_image)
	{
		var playState = this.GetPlayState();
			if(playState ==WMP7_STATE_PLAY ||playState ==WMP7_STATE_PAUSE ||playState ==WMP7_STATE_FF ||playState ==WMP7_STATE_FR||playState == WMP7_STATE_BUFFER||playState ==WMP7_STATE_WAIT)
			{
				this.Stop();
				_MM_swapImage(imageName,'',stop_image);
			}
			else
			{
				this.Play();
				_MM_swapImage(imageName,'',play_image);
			}
	}
	function _GetVersionInfo()
	{
		return eval("document."+this.WmpName+".versionInfo;");
	}
	function _ShowStatusWindow()
	{
		this.statusWindows = window.open("wmpStatus.html", "wmpStatuWindow", "width=220,height=220");
		this.runLoop();
	}
	function _IsStatusWindowUp()
	{
		if (this.statusWindows && this.statusWindows.open && !this.statusWindows.closed)
			return true;
		else
			return false;
	}
	function _IsAlive()
	{
		if(eval("document."+this.WmpName))
		{
			return true;
		}
		else
			return false;
	}
	function _Retry()
	{
		this.SetMovie(this.moviePath);	
	}
	function _SetMovie(InMovie)
	{
		this.moviePath=InMovie;
		return eval("document."+this.WmpName+".URL='"+this.moviePath+"';");
	}
	
	function _ShowWmp7() 
	{
		var showText = "<OBJECT ID='"+this.WmpName+"' ";
		showText += "CLASSID = 'CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6' ";
		showText += "width='"+this.width+"' height='"+this.height+"'>";
		showText += "<param name = 'URL' value = '"+this.moviePath+"'>";
		showText += "<param name = 'AutoStart' value = '"+this.AutoStart+"'>";
		showText += "<param name = 'UIMode' value = '"+this.UIMode+"'>";
		showText += "<param name = 'stretchToFit' value = '"+this.StretchToFit+"'>";
		showText += "<param name = 'enableContextMenu' value = '"+this.EnableContextMenu+"'>";
		showText += "</Object>";
		document.write(showText);
	}


	function _FullScreen()
	{
		if(eval("document."+this.WmpName))
		{
			var playState = this.GetPlayState();
			if(playState ==WMP7_STATE_PLAY ||playState ==WMP7_STATE_PAUSE ||playState ==WMP7_STATE_FF ||playState ==WMP7_STATE_FR||playState == WMP7_STATE_BUFFER||playState ==WMP7_STATE_WAIT)
			{
				eval("document."+this.WmpName+".fullScreen=true;");
			}
		}
	}

	function _Play()
	{
		if(eval("document."+this.WmpName))
		{
			eval("document."+this.WmpName+".controls.Play();");
		}
	}
	function _Pause()
	{
		if(eval("document."+this.WmpName))
		{
			eval("document."+this.WmpName+".controls.Pause();");
		}
	}

	function _Stop()
	{
		if(eval("document."+this.WmpName))
		{
			eval("document."+this.WmpName+".controls.Stop();");
		}
	}
	function _GetMute()
	{
		if(eval("document."+this.WmpName))
		{
			return eval("document."+this.WmpName+".settings.mute;");
		}
	}

	function _SetMute()
	{
		if(eval("document."+this.WmpName))
		{
			eval("document."+this.WmpName+".settings.mute = !document."+this.WmpName+".settings.mute;");
		}
	}
	function _GetPlayState()
	{
		var playState = -1;
		if(eval("document."+this.WmpName))
		{
			playState = eval("document."+this.WmpName+".PlayState");
		}
		return playState;
	}
	function _GetBuffer()
	{
		if(eval("document."+this.WmpName))
		{

			var playState = eval("document."+this.WmpName+".PlayState");
			if(playState ==WMP7_STATE_PLAY ||playState ==WMP7_STATE_PAUSE ||playState ==WMP7_STATE_FF ||playState ==WMP7_STATE_FR||playState == WMP7_STATE_BUFFER||playState ==WMP7_STATE_WAIT)
			{
				var tempText ="";
				tempText +=  eval("document."+this.WmpName+".network.bufferingProgress;")+"%";
				/*
				tempText +=  "buffer : "+eval("document."+this.WmpName+".network.bufferingProgress;")+"%";
				tempText +=  "bandWidth : "+eval("document."+this.WmpName+".network.bandWidth;");
				tempText +=  "bitRate : "+eval("document."+this.WmpName+".network.bitRate;");
				tempText +=  "encodedFrameRate : "+eval("document."+this.WmpName+".network.encodedFrameRate;");
				tempText +=  "frameRate : "+eval("document."+this.WmpName+".network.frameRate;");
				tempText +=  "framesSkipped : "+eval("document."+this.WmpName+".network.framesSkipped;");
				tempText +=  "getProxyBypassForLocal(mms) : "+eval("document."+this.WmpName+".network.getProxyBypassForLocal('mms');");
				tempText +=  "lostPackets : "+eval("document."+this.WmpName+".network.lostPackets;");
				tempText +=  "maxBandwidth : "+eval("document."+this.WmpName+".network.maxBandwidth;");
				tempText +=  "receivedPackets : "+eval("document."+this.WmpName+".network.receivedPackets;");
				tempText +=  "receptionQuality : "+eval("document."+this.WmpName+".network.receptionQuality;");
				tempText +=  "recoveredPackets : "+eval("document."+this.WmpName+".network.recoveredPackets;");
				tempText +=  "sourceProtocol : "+eval("document."+this.WmpName+".network.sourceProtocol;");*/
				if(this.IsStatusWindowUp())
				{
					this.statusWindows.framesSkippedBox.innerText=eval("document."+this.WmpName+".network.framesSkipped;");
					this.statusWindows.frameRateBox.innerText=eval("document."+this.WmpName+".network.frameRate;")/100;
					this.statusWindows.sourceProtocolBox.innerText=eval("document."+this.WmpName+".network.sourceProtocol;");
					this.statusWindows.bandWidthBox.innerText=eval("document."+this.WmpName+".network.bandWidth;");
					this.statusWindows.receivedPacketsBox.innerText=eval("document."+this.WmpName+".network.receivedPackets;");
					this.statusWindows.recoveredPacketsBox.innerText=eval("document."+this.WmpName+".network.recoveredPackets;");
					this.statusWindows.lostPacketsBox.innerText=eval("document."+this.WmpName+".network.lostPackets;");
					this.statusWindows.receptionQualityBox.innerText=eval("document."+this.WmpName+".network.receptionQuality;");
				}


				return 	tempText ;
			}
			else
				return "0%";
		}
	}

	function _ShowBuffer()
	{
		var showText = "<span id='"+this.WmpName+"BuffBox' Style='position: absolute;color:#"+this.FontColor+";background-color:black;font-size:10pt;font-family:Arial,Helvetica;valign=bottom;  width=35; height=12;'> 0%</span>";
		//showText += "<SCRIPT language='JavaScript'>window.setInterval(\""+this.id+".Display('"+this.WmpName+"BuffBox',"+this.id+".GetBuffer());\",450);</SCRIPT>";
		document.write(showText);
		this.runLoop();
		//window.setInterval(this.id+".Display( 'this.WmpName+"BuffBox',"+this.id+".GetBuffer());",450);
	}

	function _Display(id, str) 
	{
  		if (this.NS4) 
  		{
  			/*
    			with (document[id].document) 
    			{
      				open();
      				write(str);
      				close();
    			}*/
    			//eval(""+id+".open();"+id+".write("+str+");"+id+".close();");
  		}
  		else 
  		{
    			document.all[id].innerHTML = str;
  		}
	}

	//-------------------------
}
