 
// initialize objects:
var theClientInfo   = new ClientInfoClass();
var HTMLUtil        = new HTMLUtilClass();
var MathUtil        = new MathUtilClass();
var theCookie       = new CookieClass();
var theEventHandler = new EventHandlerClass();        // gobal eventhandler, will be used by severall classes
var theVideoPlayer  = new WMPlayerClass();
var thePlayerMenu   = new FoldableMenuClass();
var theMenuTabs     = new TabbedMenuClass();
var theOptions      = new OptionsClass();
var theSlideMenu    = new FoldableMenuClass();
var theTimeSlider   = new SliderBarClass();

var theClipName           = "";
var theOnMovieLoadAction  = new Array();
var pageInitialized       = false;
var clipEnding            = true;
  
function InitObjects(){
  if(this.theClientInfo.family!="ie4")HTMLUtil.GetElement("IEVideoControlsDiv").style.visibility="hidden";
  
  theEventHandler.Init(50);
  theOptions.Init("theOptions",theBitrates,theLanguages,theCookieBaseName);
  theVideoPlayer.Init(theEventHandler,"VideoPlayerDiv","theVideoPlayer",theClientInfo);
  theVideoPlayer.InitializePlayer(thePlayerWidth,thePlayerHeight,false,false);
  thePlayerMenu.Init("PlayerMenuDiv","thePlayerMenu",(theLiveStatus==WEBCAST_ARCHIVED?ThePlayList:TheLivePlayList),15);
  theSlideMenu.Init("SlideMenuDiv","theSlideMenu",TheSlides,15);
  theMenuTabs.Init("MenuTab", "TabContent", 5, "theMenuTabs");
  for(i=1;i<=5;i++)theMenuTabs.UnBlockTab(i); //show tabs
  if(theBitrates.length<2 && theLanguages.length<2 && hasOptionsText<10){theMenuTabs.BlockTab(4);} 
  if(theLiveStatus==4){theMenuTabs.BlockTab(1);theStartingTab=2;}
  else if(theLiveStatus==2){theMenuTabs.BlockTab(1);theMenuTabs.BlockTab(3);theStartingTab=2;}
  else if(theLiveStatus==1){theMenuTabs.BlockTab(1);theMenuTabs.BlockTab(3);}
  else {theMenuTabs.BlockTab(2);theMenuTabs.BlockTab(3);} 
  if(TheSlides.length==0){theMenuTabs.BlockTab(2);}
  for(i=1;i<5;i++)if( theStartingTab==i && theMenuTabs.IsBlocked(i) )theStartingTab++;
  
  theMenuTabs.SelectTab(theStartingTab);
  theTimeSlider.Init("theTimeSlider",true,13,225,16,15);
  thePlayerMenu.SetMenuItemHTML(theMenuItemHTML);
  thePlayerMenu.BuildMenu();
  theSlideMenu.BuildMenu();
  
  theEventHandler.Register('theVideoPlayer','OLDSTATE','showOldState');
  theEventHandler.Register('theVideoPlayer','NEWSTATE','showNewState');
  theEventHandler.Register('theVideoPlayer','BUFFERCHANGE','showBufChange');
  theEventHandler.Register('theVideoPlayer','TIMECHANGE','showTime');
  theEventHandler.Register('theVideoPlayer','MOVIELOADED','movieLoaded');
  theEventHandler.Register('theVideoPlayer','ERROR','playerError');
  theEventHandler.Register('theVideoPlayer','CLIPEND','clipEnd');
  theEventHandler.Register('thePlayerMenu','MENUCHANGE','DoPlayList');
  theEventHandler.Register('theSlideMenu','MENUCHANGE','gotoSlide');
  theEventHandler.Register('MAINEVENT','MOUSEMOVE', 'DoMouseMove');
  theEventHandler.Register('MAINEVENT','MOUSEUP', 'DoMouseUp');
  theEventHandler.Register('theMenuTabs','MENUCHANGE','changeTab');
  theEventHandler.Register('theOptions','OPTIONCHANGE','updateOption');
   
  pageInitialized=true;
  theSlideMenu.Next(true);
  if(startPlayListOnLoad)setTimeout( "DoPlayList('MAINEVENT',0);",1000 );
  //setTimeout("clipEnding=false",5000);   // dont use clipending when player just started
  //if(startPlayListOnLoad)setTimeout( "thePlayerMenu.SelectItem(('MAINEVENT',1);",1000 );
  if(theLiveStatus==WEBCAST_LIVE) showTitle(theAlmostLiveText);
}  

function clipEnd(){
  if(theLiveStatus==WEBCAST_ARCHIVED && !clipEnding){
    theVideoPlayer.Stop();
    thePlayerMenu.Next(true);
    clipEnding=true
    setTimeout("clipEnding=false",1000);
  }
}

document.onmousemove=function(){if(pageInitialized){try{theEventHandler.Raise("MAINEVENT","MOUSEMOVE",HTMLUtil.GetEventX(event)+","+HTMLUtil.GetEventY(event));}catch(e){}}}
document.onmouseup=function(){if(pageInitialized){try{if(theClientInfo.family=="ie4"){theEventHandler.Raise("MAINEVENT","MOUSEUP",event.x+","+event.y);}else{theEventHandler.Raise("MAINEVENT","MOUSEUP",event.layerX+","+event.layerY);  }}catch(e){}}}
document.onselectstart=function(){return false;}

function DoMouseMove(aSource, aValue){
  if(theTimeSlider.IsDragging()){
    theTimeSlider.Drag(aValue.split(",")[0]);
  }
}

function DoMouseUp(aSource, aValue){
  if(theTimeSlider.IsDragging()){
    theVideoPlayer.SetPosition(theTimeSlider.GetPosition()*theVideoPlayer.GetDuration());
    theTimeSlider.EndDrag();
  }
}

function playerError (aSource, aValue){
  showTitle(aValue);
  theMenuTabs.SelectTab(5);
}

function updateOption(aSource, aValue){
  if(aValue=="language"){theVideoPlayer.Stop(); document.location=document.location; return;}
  if(thePlayerMenu.GetCurrentItem()==0){    //if there is no menu item selected, the  bitrate switch wont work, so this is a fix.
      thePlayerMenu.Next(false);
  }else {
    theOnMovieLoadAction=new Array("position",theVideoPlayer.GetPosition());
  }
  DoPlayList("MAINEVENT",thePlayerMenu.GetCurrentItem());
}

var theSlideTimer=0;
function changeSlide(){theSlideMenu.Next(true);theSlideTimer=setTimeout("changeSlide();",5000);}
function stopSlide(){clearTimeout(theSlideTimer);theSlideTimer=0;}
function changeTab(aSource,aValue){ /*if(aValue=="1")changeSlide(); else stopSlide();*/ }

function showTime(aSource,aTime){
  HTMLUtil.GetElement("PlayerTimeDiv").innerHTML=MathUtil.Position2Time(aTime)+" / " + MathUtil.Position2Time(theVideoPlayer.GetDuration());
  theTimeSlider.SetPosition(aTime/theVideoPlayer.GetDuration());
}

function showBufChange(aSource,aValue){}

function movieLoaded(aSource, aValue){
  if(theOnMovieLoadAction.length>0){
    if(theOnMovieLoadAction[0]=="marker"){
      theVideoPlayer.JumpToMarker(theOnMovieLoadAction[1]);
    }
    if(theOnMovieLoadAction[0]=="position"){
      var newPos=theOnMovieLoadAction[1]*1.0;   //*1.0 for casting to proper type
      theVideoPlayer.SetPosition(newPos);
    }
    theOnMovieLoadAction=new Array();
  }
  startMovie();
  theTimeSlider.UseSlider(true);
  tmp=parseInt(theVideoPlayer.GetMediaInfoString(8));
  if(isFinite(tmp)){
    if(tmp==1 && theLiveStatus!=1){
      document.location = document.location;
    }else{
      theLiveStatus=tmp;  
    }
  }
  ShowWebcastStatus();
}

function showOldState(aSource,aValue){HTMLUtil.GetElement(aValue+"ButtonDiv").className="cwc"+aValue+"Button";}
function showNewState(aSource,aValue){
  try{
    if(aValue=="Buffering"){
      HTMLUtil.GetElement("playButtonDiv").style.visibility="hidden";
      HTMLUtil.GetElement("stopButtonDiv").style.visibility="hidden";
      HTMLUtil.GetElement("pauseButtonDiv").style.visibility="hidden";
      hideSlider()
    }else{
      HTMLUtil.GetElement("playButtonDiv").style.visibility="visible";
      HTMLUtil.GetElement("stopButtonDiv").style.visibility="visible";
      if(theLiveStatus==WEBCAST_ARCHIVED){
        HTMLUtil.GetElement("pauseButtonDiv").style.visibility="visible";
        showSlider();
      }
    }
    
    HTMLUtil.GetElement(aValue+"ButtonDiv").className="cwc"+aValue+"Button_a";
    if (aValue=="Play"&&theVideoPlayer.MovieIsLoaded) HTMLUtil.GetElement("PlayerControlsDiv").style.visibility="visible";
  }catch(e){}
}

function toggleFullscreen(){if(!isFullscreen){isFullscreen=true;}else{isFullscreen=false;}}

function startMovie(){
  myState=theVideoPlayer.State();
  if(myState=='Stop' || myState=='Pause'){
    if(thePlayerMenu.GetCurrentItem()==0)thePlayerMenu.Next(true);
    if(theVideoPlayer.MovieIsLoaded()){theVideoPlayer.Play();}
  }
}

function showTitle(aValue){
  try{
    var myTitles=aValue.split("@@__BR__@@");
    var output="";
    if(myTitles.length>0)output+='<h2 class="cwcHc2">'+myTitles[0]+'</h2><img src="http://www.companywebcast.nl/webcast/images/spacer.gif" height="1px" width="100px"><br>'; 
    if(myTitles.length>1)output+='<h2 class="cwcHc2">'+myTitles[1]+'</h2><img src="http://www.companywebcast.nl/webcast/images/spacer.gif" height="10px" width="100px"><br>'; 
    if(myTitles.length>2)output+='<h2 class="cwcHc3">'+myTitles[2]+'</h2><img src="http://www.companywebcast.nl/webcast/images/spacer.gif" height="2px" width="100px"><br>'; 
    if(myTitles.length>3)output+='<h2 class="cwcHc4">'+myTitles[3]+'</h2><img src="http://www.companywebcast.nl/webcast/images/spacer.gif" height="1px" width="100px"><br>';  
    HTMLUtil.GetElement("TitlesDiv").innerHTML=output;
  }catch(e){HTMLUtil.GetElement("TitlesDiv").innerHTML="";}
}

function ShowWebcastStatus(){
  if(theLiveStatus==WEBCAST_IDLE){
    showTitle(WEBCAST_IDLE_MSG);
    HTMLUtil.GetElement("VideoPlayerBackDiv").style.visibility="hidden";
  }
  if(theLiveStatus==WEBCAST_PRE_LIVE){
    showTitle(WEBCAST_PRE_LIVE_MSG);
    HTMLUtil.GetElement("VideoPlayerBackDiv").style.visibility="hidden";
  }
  if(theLiveStatus==WEBCAST_LIVE){
    HTMLUtil.GetElement("VideoPlayerBackDiv").style.visibility="visible";
    showTitle( thePlayerMenu.GetItemById( thePlayerMenu.GetCurrentItem() )[ITEM_TITLE] );
  }
  if(theLiveStatus==WEBCAST_PRE_ARCHIVED){
    showTitle(WEBCAST_PRE_ARCHIVED_MSG);
    HTMLUtil.GetElement("VideoPlayerBackDiv").style.visibility="hidden";
  }  
}

function DoScriptCommand(aType, aValue){
  if(aType=="SLIDE"){
    if(theSlideMenu.GetCurrentItem()!=parseInt(aValue)){ 
      theSlideMenu.SelectItem(theSlideMenu.GetItemIdByValue(parseInt(aValue)),true);
      //if(theLiveStatus==WEBCAST_LIVE) showTitle("");
      if(theLiveStatus==WEBCAST_ARCHIVED){
        var mySlideItem=theSlideMenu.GetItemById(parseInt(aValue));      
        thePlayerMenu.SelectMenuItemByThumb(mySlideItem[ITEM_ITEMID],"/s_thumbs/slide");
        var myItem=thePlayerMenu.GetItemById(thePlayerMenu.GetCurrentItem());
        showTitle(myItem[ITEM_TITLE]);
      }
    }
  }
  if(aType=="BROWSER" && aValue=="REFRESH"){
    document.location=document.location;
  }
}

function DoMarkerHit(aMarker){
  var myId=thePlayerMenu.GetItemIdByValue(aMarker+"@"+theClipName)
  if(myId==undefined)return false;
  thePlayerMenu.SelectItem(myId,false);
  var myItem=thePlayerMenu.GetItemById(myId);
  showTitle(myItem[ITEM_TITLE]);
}

function showSlider(){
  HTMLUtil.GetElement("theTimeSliderBarDiv").className="cwcTimeSliderBarDiv";
  HTMLUtil.GetElement("theTimeSliderCaptureDiv").className="cwcTimeSliderCaptureDiv";
}

function hideSlider(){
  HTMLUtil.GetElement("theTimeSliderBarDiv").className="cwcTimeSliderBarDiv-off";
  HTMLUtil.GetElement("theTimeSliderCaptureDiv").className="cwcTimeSliderCaptureDiv-off";
}

function DoPlayList(aSource,aValue){
  // if there is no clip selected, play first clip in list 
  if(aValue==0){thePlayerMenu.Next();aValue=thePlayerMenu.GetCurrentItem();}
  var myItem=thePlayerMenu.GetItemById(aValue);
  
  //status=aValue + " - " + myItem[ITEM_TYPE];
  //update titles section
  if(theLiveStatus!=WEBCAST_LIVE)
    if(theLiveStatus!=WEBCAST_ARCHIVED){ShowWebcastStatus();}else{showTitle(myItem[ITEM_TITLE]);}
  
  if(myItem[ITEM_TYPE]=="stream"){
    theClipServerUrl=theLiveStreamServerUrl;
    theClipName=""
    theVideoPlayer.LoadMovie( myItem[ITEM_ITEMID]+"_"+theOptions.GetLanguage()+"_"+theOptions.GetBitrate() ,false );
    //hideSlider();
    HTMLUtil.GetElement("PlayerTimeDiv").style.visibility="hidden";
    HTMLUtil.GetElement("PauseButtonDiv").style.visibility="hidden";
  }else{
    theClipServerUrl=theArchivedClipServerUrl;
    clipEnding=false;
    //showSlider();
    HTMLUtil.GetElement("PlayerTimeDiv").style.visibility="visible";
    HTMLUtil.GetElement("PauseButtonDiv").style.visibility="visible";
  }
  
  if(myItem[ITEM_TYPE]=="video"){
    if(myItem[ITEM_ITEMID].length>0){
      theClipName=myItem[ITEM_ITEMID];
      //prompt('',theOptions.GetBitrate()+"/cwc/"+theClientName+"/"+theVideoDirName+"/"+theClipName+"_"+theOptions.GetLanguage()+"."+theMediaExtension);
      theVideoPlayer.LoadMovie( theOptions.GetBitrate()+"/cwc/"+theClientName+"/"+theVideoDirName+"/"+theClipName+"_"+theOptions.GetLanguage()+"."+theMediaExtension,true );
    }
  }
  
  if(myItem[ITEM_TYPE]=="marker"){
    myMarkerClip=myItem[ITEM_ITEMID].split("@")[1];
    if(theClientInfo.family!="ie4"){
      theClipName=myMarkerClip;
      myMark=myItem[ITEM_ITEMID].split("@")[0];
      theVideoPlayer.LoadMovie( theOptions.GetBitrate()+"/cwc/"+theClientName+"/"+theVideoDirName+"/"+theClipName+"_"+theOptions.GetLanguage()+"."+theMediaExtension+"&marker="+myMark,true );
    }else{
      if(myMarkerClip!=theClipName&&myMarkerClip.length>0||aSource=="MAINEVENT"){
        //load correct movie first, and remember marker for onloadmovie
        //prompt('',theOptions.GetBitrate()+"/cwc/"+theClientName+"/"+theVideoDirName+"/"+theClipName+"_"+theOptions.GetLanguage()+"."+theMediaExtension);
        theClipName=myMarkerClip;
        theVideoPlayer.LoadMovie( theOptions.GetBitrate()+"/cwc/"+theClientName+"/"+theVideoDirName+"/"+theClipName+"_"+theOptions.GetLanguage()+"."+theMediaExtension,true );
        //if(theOnMovieLoadAction.length==0)
        theOnMovieLoadAction=new Array("marker",myItem[ITEM_ITEMID].split("@")[0]);
      }else{
        //jump to marker in current movie
        theVideoPlayer.JumpToMarker(myItem[ITEM_ITEMID].split("@")[0]);
      }
      }
  }
}
  
function gotoSlide(aSource,aValue){
  var myItem=theSlideMenu.GetItemById(aValue);
  HTMLUtil.GetElement("SlideImg").src=theClientPath+"/powerpoint/slides/slide"+myItem[ITEM_ITEMID]+".jpg";
  HTMLUtil.GetElement("SlideCountDiv").innerHTML=theSlideMenu.GetCurrentItem()+"/"+theSlideMenu.GetItemCount();
}