 
// 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 theSlideShow    = new SlideShowClass();

var theClipName           = "";
var theOnMovieLoadAction  = new Array();
var pageInitialized       = false;
var AlmostLive=false;
  
function InitObjects(){
  if(this.theClientInfo.family!="ie4")document.getElementById("IEVideoControlsDiv").style.visibility="hidden";
  theEventHandler.Init(50);
  theOptions.Init("theOptions","OptionsDiv", 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);
  theSlideShow.Init("theSlideShow",theSlideMenu);
  if(theLiveStatus==WEBCAST_ARCHIVED)theSlideShow.ToggleView();
  theMenuTabs.Init("MenuTab", "TabContent", 5, "theMenuTabs");
  for(i=1;i<=5;i++)theMenuTabs.UnBlockTab(i); //show tabs
  //if(theBitrates.length<2 && theLanguages.length<2){theMenuTabs.BlockTab(4);} 
  if(theLiveStatus==4){theMenuTabs.BlockTab(1);theStartingTab=2;}
  else if(theLiveStatus==3){theMenuTabs.BlockTab(1);theMenuTabs.BlockTab(3);}
  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('thePlayerMenu','MENUCHANGE','DoPlayList');
  theEventHandler.Register('theSlideMenu','MENUCHANGE','GotoSlide');
  theEventHandler.Register('theSlideShow','SLIDEINDEXCHANGED','VideoFollowSlide');
  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 );
  if(theLiveStatus==WEBCAST_LIVE) {AlmostLive=true;ShowWebcastStatus();}
}

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){}}}

function VideoFollowSlide(aSource, aValue){
  theVideoPlayer.Play(); 
  theVideoPlayer.JumpToMarker( theSlideMenu.GetCurrentItem() );
}
function GotoSlide(aSource, aValue){ 
  theSlideShow.GotoSlide(aValue);
}

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());
}

function showTime(aSource,aTime){
  document.getElementById("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;  
      AlmostLive=true;
      if(theLiveStatus==WEBCAST_LIVE) ShowWebcastStatus();
    }
  }
  ShowWebcastStatus();
}

function showOldState(aSource,aValue){document.getElementById(aValue+"ButtonDiv").className="cwc"+aValue+"Button";}
function showNewState(aSource,aValue){
  try{
    if(aValue=="Buffering"){
      document.getElementById("playButtonDiv").style.visibility="hidden";
      document.getElementById("stopButtonDiv").style.visibility="hidden";
      document.getElementById("pauseButtonDiv").style.visibility="hidden";
      hideSlider()
    }else{
      document.getElementById("playButtonDiv").style.visibility="visible";
      document.getElementById("stopButtonDiv").style.visibility="visible";
      if(theLiveStatus==WEBCAST_ARCHIVED){
        document.getElementById("pauseButtonDiv").style.visibility="visible";
        showSlider();
      }
    }
    
    document.getElementById(aValue+"ButtonDiv").className="cwc"+aValue+"Button_a";
    if (aValue=="Play"&&theVideoPlayer.MovieIsLoaded) document.getElementById("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>';  
    document.getElementById("TitlesDiv").innerHTML=output;
  }catch(e){document.getElementById("TitlesDiv").innerHTML="";}
}

function ShowWebcastStatus(){
  //status=theLiveStatus;
  if(theLiveStatus==WEBCAST_IDLE){
    showTitle(WEBCAST_IDLE_MSG);
    document.getElementById("VideoPlayerBackDiv").style.visibility="hidden";
  }
  if(theLiveStatus==WEBCAST_PRE_LIVE){
    showTitle(WEBCAST_PRE_LIVE_MSG);
    document.getElementById("VideoPlayerBackDiv").style.visibility="hidden";
  }
  if(theLiveStatus==WEBCAST_LIVE){
    document.getElementById("VideoPlayerBackDiv").style.visibility="visible";
    if (AlmostLive)
      showTitle(theAlmostLiveText); 
    else{ 
      var mySlideItem=theSlideMenu.GetItemById( theSlideMenu.GetCurrentItem() );   
      var sel = GetMenuItemByThumb(mySlideItem[ITEM_ITEMID],"/s_thumbs/slide");
      if(sel>-1) showTitle( ThePlayList[sel][ITEM_TITLE] );
    }
  }
  if(theLiveStatus==WEBCAST_PRE_ARCHIVED){
    showTitle(WEBCAST_PRE_ARCHIVED_MSG);
    document.getElementById("VideoPlayerBackDiv").style.visibility="hidden";
  }  
}

function DoScriptCommand(aType, aValue){
  //status = aType + " " + aValue + " " + Math.random() + " " + theSlideShow.IsOnHold();
  if(aType=="SLIDE"){
    AlmostLive=false;
    if(theSlideMenu.GetCurrentItem()!=parseInt(aValue)){ 
      if(!theSlideShow.IsOnHold() ) 
        theSlideMenu.SelectItem(theSlideMenu.GetItemIdByValue(parseInt(aValue)),true);
      //theSlideShow.GotoSlide( theSlideMenu.GetItemIdByValue(parseInt(aValue)),true);
      //if(theLiveStatus==WEBCAST_LIVE) showTitle("");
      updatePlayerMenuFromSlides( aValue );
    }
    ShowWebcastStatus();
  }
  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(){
  document.getElementById("theTimeSliderBarDiv").className="cwcTimeSliderBarDiv";
  document.getElementById("theTimeSliderCaptureDiv").className="cwcTimeSliderCaptureDiv";
}

function hideSlider(){
  document.getElementById("theTimeSliderBarDiv").className="cwcTimeSliderBarDiv-off";
  document.getElementById("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_ITEMID] + " - " + 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();
    document.getElementById("PlayerTimeDiv").style.visibility="hidden";
    document.getElementById("PauseButtonDiv").style.visibility="hidden";
  }else{
    theClipServerUrl=theArchivedClipServerUrl;
    //showSlider();
    document.getElementById("PlayerTimeDiv").style.visibility="visible";
    document.getElementById("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 updatePlayerMenuFromSlides(aValue){
  if(theLiveStatus==WEBCAST_ARCHIVED){
    var mySlideItem=theSlideMenu.GetItemById(parseInt(aValue));      
    var sel = thePlayerMenu.GetMenuItemByThumb(mySlideItem[ITEM_ITEMID],"/s_thumbs/slide");
    if(sel>-1 && thePlayerMenu.GetCurrentItem()!=sel) thePlayerMenu.SelectItem(sel);
    var myItem=thePlayerMenu.GetItemById(thePlayerMenu.GetCurrentItem());
    showTitle(myItem[ITEM_TITLE]);
  }
}
    
function GetMenuItemByThumb(aSlidenum, aSubstr){
  var sel=-1;
  for(var i=0;i<ThePlayList.length;i++){
    if(ThePlayList[i][ITEM_THUMBNAIL].indexOf(aSubstr)>-1){
      var tmpStr=ThePlayList[i][ITEM_THUMBNAIL].toLowerCase();
      var p=tmpStr.indexOf(aSubstr)+aSubstr.length;
      var tmp=parseInt(ThePlayList[i][ITEM_THUMBNAIL].substr(p).replace(".jpg",""));
      if(aSlidenum>=tmp) sel=i;
    }
  }
  return sel;
}