//*************************************************************************************************
//**  WMPlayerClass
//**  author: jack@inex.nl
//**  (c) 2004 InfoPinnacle BV.
//*************************************************************************************************

function WMPlayerClass(){
  this.Init=Init;
  this.LoadMovie=LoadMovie;
  this.MovieIsLoaded=MovieIsLoaded;
  this.Play=PlayMediaPlayer;
  this.Pause=PauseMediaPlayer;
  this.Stop=StopMediaPlayer;
  this.InitializePlayer=InitializePlayer;
  this.DoPlayerBuffering=DoPlayerBuffering;
  this.DoPlayerStateChange=DoPlayerStateChange;
  this.DoError=DoError;
  this.TrackBuffer=TrackBuffer;
  this.TrackTime=TrackTime;
  this.GetDuration=GetDuration;
  this.State=GetState;
  this.SetSize=SetSize;
  this.SetPosition=SetPosition;
  this.GetPosition=GetPosition;
  this.SetVolume=SetVolume;
  this.GetVolume=GetVolume;
  this.JumpToMarker=JumpToMarker;
  this.GetMediaInfoString=GetMediaInfoString;
  this.ToggleMute=ToggleMute;
  this.IsMuted=IsMuted;

  var VP_STOPPED   = 0;
  var VP_PAUSED    = 1;
  var VP_PLAYING   = 2;
  var VP_BUFFERING = 3;

  var theEventHandler=null;
  var theVideoDivId=null;
  var theObjectName=null;
  var theTrackBufferTimer = 0;
  var videoPlayerInitialized=false;
  var playerIsBuffering=false;
  var currentStatus=VP_STOPPED;
  var StatesArr=null;
  var theClientInfo=null;
  var theWidth=0;
  var theHeight=0;
  var doShowControls=0;
  var isTransparant=0;
  var theUrl="";
  var theVolume;
  var isPlaying=false;
  var muted=false;
  var ErrorState=false;
  
  function Init(aEventHandler, aVideoDivId, aObjectName,aClientInfo){
    theEventHandler=aEventHandler
    theVideoDivId=aVideoDivId;
    theObjectName=aObjectName;
    StatesArr=new Array("Stop","Pause","Play","Buffering");
    currentStatus=VP_STOPPED;
    theClientInfo=aClientInfo;
    PlayerIsBuffering=false;  
  }

  function InitializePlayer(aWidth,aHeight,aIsTransparant,aShowControls){
    document.getElementById(theVideoDivId).innerHTML=buildPlayerHTML(aWidth,aHeight,aShowControls,aIsTransparant,"",theObjectName);
    theWidth=aWidth;
    theHeight=aHeight;
    doShowControls=aShowControls;
    videoPlayerInitialized=true;
    isTransparant=aIsTransparant;
    theUrl="";
    muted=IsMuted();
  }

  function MovieIsLoaded (){if(videoPlayerInitialized)try{return (document.getElementById("mediaPlayer").fileName.length>5);}catch(e){return false;} }
  function PlayMediaPlayer(){try{if(document.getElementById("mediaPlayer").fileName.length>5){document.getElementById("mediaPlayer").play();}}catch(e){}}
  function StopMediaPlayer(){try{document.getElementById("mediaPlayer").stop();theEventHandler.Raise(theObjectName,"TIMECHANGE",0);}catch(e){}}
  function PauseMediaPlayer(){try{if(GetState()=='Play') document.getElementById("mediaPlayer").pause();}catch(e){}}
  function GetDuration(){try{return document.getElementById("mediaPlayer").duration;}catch(e){return 0;}}
  function GetState(){try{return StatesArr[currentStatus];}catch(e){return null;}}
  function GetMediaInfoString(aId){return document.getElementById("mediaPlayer").GetMediaInfoString(aId);}

  function IsMuted(){ try{return (document.getElementById("mediaPlayer").Mute==1);}catch(e){return false;}}
  function ToggleMute(){
    if(muted){
      document.getElementById("mediaPlayer").Mute=0;
      muted=false;
    }else{
      document.getElementById("mediaPlayer").Mute=1;
      muted=true;
    }
  }

  function GetPosition(){return document.getElementById("mediaPlayer").currentPosition; }
  function SetPosition(aValue){
    var val1=Math.round(document.getElementById("mediaPlayer").currentPosition);
    var val2=Math.round(aValue);
    if( val1-val2>1 || val1-val2<1)try{document.getElementById("mediaPlayer").currentPosition=aValue;}catch(e){}
    theEventHandler.Raise(theObjectName,"TIMECHANGE",aValue);
  }

  function GetVolume(){return document.getElementById("mediaPlayer").Volume; }
  function SetVolume(aValue){
    var val1=Math.round(document.getElementById("mediaPlayer").Volume);
    var val2=Math.round(aValue);
    if( val1-val2>1 || val1-val2<1)try{document.getElementById("mediaPlayer").Volume=aValue;}catch(e){}
    theEventHandler.Raise(theObjectName,"VOLUMECHANGE",aValue);
  }

  function SetSize(aWidth,aHeight){
    theWidth=aWidth;
    theHeight=aHeight;
    if(theClientInfo.family=="ie4"){
      document.getElementById("mediaPlayer").width=theWidth;
      document.getElementById("mediaPlayer").height=theHeight;
    }else{
      var myUrl=(currentStatus==VP_STOPPED?"":theUrl)
      document.getElementById(theVideoDivId).innerHTML=buildPlayerHTML(theWidth,theHeight,"",isTransparant,myUrl,theObjectName);
    }
  }
 
  function LoadMovie(aClip,useAsxGen){
    if(videoPlayerInitialized){
      if(useAsxGen)
        theUrl=theAsxgenUrl+"?clip="+aClip+"&p=.asx"
      else
        theUrl=theClipServerUrl+aClip;
      //prompt('',theUrl);
      if(theClientInfo.family=="ie4"){
        document.getElementById("mediaPlayer").fileName=theUrl;
      }else{
       document.getElementById(theVideoDivId).innerHTML=buildPlayerHTML(theWidth,theHeight,"",isTransparant,theUrl,theObjectName);
      }
    }
  }

  function JumpToMarker(aMarker){
    try{if(aMarker <= document.getElementById("mediaPlayer").MarkerCount) document.getElementById("mediaPlayer").CurrentMarker = aMarker;}catch(e){}
  }

  function DoPlayerBuffering(isBuffering){
    theEventHandler.Raise(theObjectName,"BUFFERING",isBuffering);
    PlayerIsBuffering=isBuffering;
    var bufProgress=document.getElementById("mediaPlayer").BufferingProgress;
    if(bufProgress<100)PlayerIsBuffering=true;
    if(PlayerIsBuffering){
      DoPlayerStateChange(currentStatus,VP_BUFFERING);
      TrackBuffer();
    }else{
      DoPlayerStateChange(currentStatus,VP_PLAYING);
      //if(bufProgress<100)alert(bufProgress);
    }
  }

  function DoPlayerStateChange(lOldState, lNewState){
    currentStatus=lNewState;
    if (currentStatus==VP_PLAYING){isPlaying=true;TrackTime();}
    else if(currentStatus==VP_STOPPED){isPlaying=false;StopMediaPlayer();}
    else{isPlaying=false;}    
    if(lOldState==VP_BUFFERING)PlayerIsBuffering=false;
    if(currentStatus==VP_BUFFERING)PlayerIsBuffering=true;
    if(PlayerIsBuffering){      //player is buffering, but because of autoplay=false, we dont get the event in DoPlayerBuffering function
      currentStatus=VP_BUFFERING;
      TrackBuffer();
    }
    theEventHandler.Raise(theObjectName,"OLDSTATE",StatesArr[lOldState]);
    theEventHandler.Raise(theObjectName,"NEWSTATE",StatesArr[currentStatus]);
  }

  function DoError(){
    ErrorState=true;
    var errDesc = "";
    try { errDesc = document.getElementById("mediaPlayer").error.item(0).errorDescription; } catch(e){}
    theEventHandler.Raise(theObjectName,"ERROR",errDesc);
    StopMediaPlayer();
  }
  
  function TrackBuffer(){
    
    if(PlayerIsBuffering){
      theEventHandler.Raise(theObjectName,"BUFFERCHANGE",document.getElementById("mediaPlayer").BufferingProgress);
      clearTimeout("theTrackBufferTimer");  //dont want more then one timer on this function simultaniously
      theTrackBufferTimer=setTimeout(theObjectName+".TrackBuffer()",250);
    }else{
      if(!ErrorState){
        theEventHandler.Raise(theObjectName,"MOVIELOADED","");
      }else{
        ErrorState=false;
      }
    }
  }

  function TrackTime(){
    theMP=document.getElementById("mediaPlayer");var myTime=theMP.currentPosition;
    if(theMP.currentPosition>0 && theMP.currentPosition>(theMP.duration-1))theEventHandler.Raise(theObjectName,"CLIPEND","");
    if(isPlaying){theEventHandler.Raise(theObjectName,"TIMECHANGE",myTime);
    if(theVolume!=GetVolume()){
      theVolume=GetVolume();
      theEventHandler.Raise(theObjectName,"VOLUMECHANGE",theVolume);
    }
    setTimeout(theObjectName+".TrackTime()",100);}   
  }

  function buildPlayerHTML(aWidth,aHeight,aShowControls,aIsTransparant,aUrl, aObjectName){
    //prompt('',aUrl);
    var output="";
    if(theClientInfo.family=="ie4"){
      output='<object id="mediaPlayer"  width="'+aWidth+'" height="'+aHeight+'" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby="Loading Microsoft® Windows® Media Player components..." type="application/x-oleobject">;'
      output+='<param name="FileName" value="'+aUrl+'"/>';
      output+='<param name="DisplaySize" value="4"/>';
      output+='<param name="ShowStatusBar" value="0"/>';
      output+='<param name="ShowAudioControls" value="0"/>';
      output+='<param name="ShowControls" value="'+aShowControls+'"/>';
      output+='<param name="AutoSize" value="1"/>';
      output+='<param name="AnimationAtStart" value="false"/>';
      output+='<param name="AutoPlay" value="true"/>';
      output+='<param name="TransparentAtStart" value="'+aIsTransparant+'"/></object>';
      output+='<scr'+'ipt for="mediaPlayer" event="Buffering(bStart)" type="text/javascript">'+aObjectName+'.DoPlayerBuffering(bStart);</scr'+'ipt>';
      output+='<scr'+'ipt for="mediaPlayer" event="PlayStateChange(lOldState, lNewState)" type="text/javascript">'+aObjectName+'.DoPlayerStateChange(lOldState, lNewState);</scr'+'ipt>';
      output+='<scr'+'ipt for="mediaPlayer" event="ScriptCommand(aType, aValue)" type="text/javascript">DoScriptCommand(aType, aValue);</scr'+'ipt>';
      output+='<scr'+'ipt for="mediaPlayer" event="MarkerHit(markerNum)" type="text/javascript">DoMarkerHit(markerNum);</scr'+'ipt>';
      output+='<scr'+'ipt for="mediaPlayer" event="error()" type="text/javascript">'+aObjectName+'.DoError();</scr'+'ipt>';
      
    }else{
      output='<embed id="mediaplayer" ';
      output+='type="application/x-mplayer2" pluginspage = "http://www.microsoft.com/Windows/MediaPlayer/" ';
      output+='src="'+aUrl+'" ';
      output+='name="mediaplayer" ';
      output+='width="'+aWidth+'" ';
      output+='height="'+(aHeight+44)+'" ';
      output+='showstatusbar="0" ';
      output+='showaudiocontrols="0" ';
      output+='showcontrols="true" ';
      output+='DisplaySize="4" ';
      output+='autosize="0"></embed>';
    }
    return output;
  }

}
