﻿//*************************************************************************************************
//**  WMPlayerClass
//**  author: jack@inex.nl
//**  (c) 2006 Companywebcast.nl
//*************************************************************************************************
//
//<script type="text/javascript" src="sl/Silverlight.js"></script>
//<script type="text/javascript" src="js/WMediaPlayer_sl.js"></script>
//<script type="text/javascript" src="sl/CwcPlayer/Scene.xaml.js"></script>
//

function WMediaPlayerClass(aControlObj){
  this.Init=Init;
  this.LoadMovie=LoadMovie;
  this.SetController=SetController;
  this.MovieIsLoaded=MovieIsLoaded;
  this.Play=PlayMediaPlayer;
  this.Pause=PauseMediaPlayer;
  this.Stop=StopMediaPlayer;
  this.InitializePlayer=InitializePlayer;
  this.SetSize=SetSize;
  this.SetPosition=SetPosition;
  this.GetPosition=GetPosition;
  this.GetDuration=GetDuration;
  this.GetMediaInfoString=GetMediaInfoString;
  this.FullScreen = FullScreen;
  this.SetBackgroundImage = SetBackgroundImage ;

  var VP_STOPPED   = 0;
  var VP_PAUSED    = 1;
  var VP_PLAYING   = 2;
  var VP_BUFFERING = 3;

  var theVideoDivId=null;
  var theController=null;
  var theObjectName=null;
  var theWidth=0;
  var theHeight=0;
  var doShowControls=0;
  var isTransparant=0;
  var theUrl="";
  var isPlaying=false;
  var ErrorState=false;
  var isAudio=false;
  var videoPlayerInitialized=false;
  var theControl=aControlObj;
  var doDisableContextMenu;
  var startPosition=0;
  
    
  function Init(aVideoDivId, aObjectName,aClientInfo,aDisableContextMenu){
    theVideoDivId=aVideoDivId;
    theObjectName=aObjectName;
    theClientInfo=aClientInfo;
    PlayerIsBuffering=false;
    doDisableContextMenu = aDisableContextMenu;
  }

  function InitializePlayer(aWidth, aHeight, aIsTransparant, aShowControls, aPos) {
    theWidth=aWidth;
    theHeight = aHeight;
    startPosition = aPos;
    document.getElementById(theVideoDivId).innerHTML=buildPlayerHTML(aWidth,aHeight,aShowControls,aIsTransparant,"",theObjectName);
    doShowControls=aShowControls;
    var myBgImage= "";
    if (theBitrateId == 3) myBgImage = ResourceHandlerUrl + "jpg/audioImage.jpg";
    var version = "2.0";
    //todo: add check for moonlight and set version to 1.0 (or maybe 1.1) for all livefases 
    try{if (theLiveStatusId > 5) version = "1.0";}catch(ex){}
    createSilverlightPlayer(theObjectName,theWidth,theHeight, myBgImage,version);
    videoPlayerInitialized=true;
    theUrl = "";
  }
  
  function SetController(aController){
    theController=aController;
  }

  function MovieIsLoaded (){if(scene.mediaPlayer)return scene.mediaPlayer.CurrentState=="Playing"; else return false;}//if(videoPlayerInitialized)try{return (currentStatus>0);}catch(e){return false;} }
  function PlayMediaPlayer(){if(scene.mediaPlayer)scene.mediaPlayer.Play(); }//try{document.getElementById("mediaPlayer").play();}catch(e){}}
  function StopMediaPlayer(){if(scene.mediaPlayer)scene.mediaPlayer.Stop();}//try{document.getElementById("mediaPlayer").stop();}catch(e){}}
  function PauseMediaPlayer(){if(scene.mediaPlayer)scene.mediaPlayer.Pause();}//try{if(GetState()=='Play') document.getElementById("mediaPlayer").pause();}catch(e){}}
  function GetDuration(){return scene.mediaPlayer.NaturalDuration.Seconds;}//try{return document.getElementById("mediaPlayer").duration;}catch(e){return 0;}}
  function GetState(){if(scene.mediaPlayer)scene.mediaPlayer.CurrentState; else return "";}//try{return StatesArr[currentStatus];}catch(e){return null;}}
  function GetMediaInfoString(aId){return 0;}///*return document.getElementById("mediaPlayer").GetMediaInfoString(aId);*/}
  function SetBackgroundImage(aBgImage) { scene.SetBackgroundImage(aBgImage); }
  
    function FullScreen(){}
    
    function SetPosition(aPosition){
        if(scene) scene.SetMediaPosition(aPosition);  
    }
    
    function GetPosition(){
        if(scene.mediaPlayer)return scene.mediaPlayer.Position ; else return 0;
    }
    
    function LoadMovie(aClip,isPP){
        if (scene) scene.LoadMovie(aClip, startPosition);
     }

    function SetSize(aWidth, aHeight){
        if(videoPlayerInitialized){
          if(isAudio){
            theWidth="";
            theHeight="";
          }else{
            theWidth=aWidth;
            theHeight=aHeight;
          }
          if(scene && !scene.IsFullScreen()){
              var silverLightPlugin=document.getElementById("SilverlightPlugIn");
              var silverlightPlugInHost=document.getElementById("SilverlightPlugInHost");
              if(silverLightPlugin!=null){
                if(silverlightPlugInHost.height==theHeight)theHeight-=20; //consider size of controlbar
                silverLightPlugin.width = theWidth;
                silverLightPlugin.height = theHeight+20;
                silverlightPlugInHost.width = theWidth;
                silverlightPlugInHost.height = theHeight+20;
                try{scene.UpdateLayout(theWidth,theHeight);}catch(ex){}
              }
           }
        }
    }

    function SetMediaPosition(aPos){
        scene.SetMediaPosition(aPos);
    }

  
  function buildPlayerHTML(aWidth,aHeight,aShowControls,aIsTransparant,aUrl, aObjectName){
    if(theWidth==0 && theHeight==0){
        theWidth=32;
        theHeight=24;
    }
    var output='<div style="width:'+theWidth+'px;height:'+theHeight+'px;position:relative;" id="SilverlightPlugInHost">xxx</div>';
    return output;
  }
}
 
  var scene="x";
  function createSilverlightPlayer(aObjectName, aWidth, aHeight, aBgImage, aVersion) {
      if (aVersion == null) aVersion = '2.0';
    scene = new CwcPlayer.Scene(aObjectName, aBgImage);
	Silverlight.createObjectEx({
		source: 'sl/CwcPlayer/Scene.xaml?v=2',
		parentElement: document.getElementById('SilverlightPlugInHost'),
		id: 'SilverlightPlugIn',
		properties: {
			width: aWidth.toString(),
			height: (aHeight+20).toString(),
			background:'#FF222222',
            isWindowless: 'false',
            version: aVersion
		},
		events: {
		    onError: null,
			onLoad: Silverlight.createDelegate(scene, scene.Init)
		},		
		context: null 
	});
  }
  
  if (!window.Silverlight) 
	window.Silverlight = {};

    Silverlight.createDelegate = function(instance, method) {
	    return function() {
            return method.apply(instance, arguments);
        }
    }


