/*

This file is the core of the js mediaplayer controls


Coded for Noturec, 2005
*/



timeshowtype='normal';
state=0;
oldvolume=50;
mute=false;
ispause=false;
isplay=false;
AX=true;
currentjump=0; // current notule id (indexed item, used for jumping between indexed items)
i=0;
niePos=0,nieStart=false,nieFull=false; // some vars for NonActiveX supported browsers
curspreker=0;

// check for WMP7 and activeX, if not available, display non activeX version
var IE    = (navigator.userAgent.indexOf("MSIE") != -1);
var WMP7;
if (IE)
{
     WMP7 = new ActiveXObject('WMPlayer.OCX');
}
if (! WMP7)
    AX=false;
if (! IE)
    AX=false;


function Init()
{
    if(AX)
        CheckMe();
    else
	CheckMeNonMSIE();
}


function CheckMeNonMSIE()
{
    //this function repeats itselves every second
    setTimeout("CheckMeNonMSIE()",10000);
    if(nieStart)
    {
	niePos++;
	playedhour= parseInt(niePos/60/60);
	playedmin = parseInt((niePos-(playedhour*60*60))/60);
	playedsec = parseInt(niePos-(playedmin*60));
	
	if(playedhour<10) playedhour="0"+playedhour;
	if(playedmin<10) playedmin="0"+playedmin;
	if(playedsec<10) playedsec="0"+playedsec;
	document.getElementById("timeshow").innerHTML=playedhour+':'+playedmin+':'+playedsec;
	
    }
}

function CheckMe()
{
    // this function repeats itselves every .5 second.
    // it shows the time (time played or time to play) and 
    // checks if the sound has been muted /
    setTimeout("CheckMe()", 500);
    
    
    // when stream is being buffered, dont display buttons, but show progress :
    if((document.getElementById("Player").Network.bufferingProgress < 100 ) && (document.getElementById("Player").Network.bufferingProgress > 0 ) )
    {
	showbuffering();
	// show buffer %
	document.getElementById('controlbuffer').innerHTML='Opslaan in buffer : '+document.getElementById("Player").Network.bufferingProgress+'% voltooid';
    }
    else
    {
	showbuttons();
    }
    
    if(timeshowtype=='normal')
    {
	totaltime=parseInt(document.getElementById("Player").controls.currentPosition);
	totalhour=parseInt(totaltime/3600);
	totalmin=parseInt( (totaltime-(totalhour*3600))/60);
	totalsec=parseInt( ((totaltime-(totalhour*3600))-(totalmin*60))  );

      if(totalsec<10)
	totalsec='0'+totalsec;
      if(totalmin<10)
	totalmin='0'+totalmin;
      if(totalhour<10)
        totalhour='0'+totalhour;
    
    
	document.getElementById("timeshow").innerHTML=totalhour+':'+totalmin+':'+totalsec;
    }
      else if(timeshowtype=='reversed')
    {
      // calculate time left on video
      totaltime=document.getElementById("Player").currentmedia.duration;
      totalhour=parseInt(totaltime/3600);
      totalmin=parseInt( (totaltime-(totalhour*3600))/60);
      totalsec=parseInt( ((totaltime-(totalhour*3600))-(totalmin*60))  );

      curtime=document.getElementById("Player").controls.currentPosition;
      curhour=parseInt(curtime/3600);
      curmin=parseInt( (curtime-(curhour*3600))/60);
      cursec=parseInt( ((curtime-(curhour*3600))-(curmin*60))  );

      tohour=parseInt(totalhour-curhour);
      tomin=parseInt(totalmin-curmin);
      tosec=parseInt(totalsec-cursec);
    
      if(tosec<0)
      {
	tomin--;
	tosec+=60;
      }
      if(tomin<0)
      {
        tohour--;
	tomin+=60;
      }
    
      if(tosec<10)
	tosec='0'+tosec;
      if(tomin<10)
	tomin='0'+tomin;
      if(tohour<10)
        tohour='0'+tohour;

      document.getElementById("timeshow").innerHTML=tohour+':'+tomin+':'+tosec;
    }
  
  // if file is playing, show other playbutton
  if(document.getElementById("Player").playState ==3)
  {
    ChangeImage('play','img/buttons/btn_play_act.gif');
  }
  else
  {
    ChangeImage('play','img/buttons/btn_play_off.gif');
  }
  
    // when video is on pause, display a blinking play button :
    if(ispause && isplay)
    {
	if(i==0)
	    ChangeImage('play','img/buttons/btn_play_off.gif');
	if(i==1)
	    ChangeImage('play','img/buttons/btn_play_act.gif');
    }
    
    if(i==1)
	i=0;
    else
	i=1;
}

function swapmute()
{
    if(AX)
    {
	if(mute)
	    mute=false;
	else
	    mute=true;
	
	if(mute)
        {
	    document.getElementById("Player").settings.volume=0;
	    oldvolume=s.getValue();
	    if(oldvolume<10) oldvolume=10;
	    s.setValue(0);
	}
        else
	{
	    document.getElementById("Player").settings.volume=oldvolume;
	    s.setValue(oldvolume);
        }
    }
    else
    {
	notsupportedbrowser();
    }
}

// change display type of time
function swaptimeshow()
{
    if (timeshowtype=='normal')
    {
	timeshowtype='reversed';
    }
    else if (timeshowtype=='reversed')
    {
	timeshowtype='normal';
    }
}

function swappause()
{
    if(ispause)
    {
	if(AX)
	{
	    setplay();
	}
	else
	{
	    registerNonMSIEVar('nieStart',true);
	    registerNonMSIEVar('niePos',niePos);
	    changeNonMSIEPlayer();
	}
	ispause=false;
    }
    else
    {
	if(AX)
	{
	    document.getElementById('Player').controls.Pause();
        }
	else
	{
	    registerNonMSIEVar('nieStart',false);
	    registerNonMSIEVar('niePos',(niePos-1));
	    changeNonMSIEPlayer();
	}
	ispause=true;
    }
}

function setplay()
{
    if(AX)
    {
	document.getElementById('Player').controls.Play()
        ispause=false;
        isplay=true
    }
    else
    {
    	registerNonMSIEVar('nieStart',true);
	registerNonMSIEVar('niePos',niePos);
	changeNonMSIEPlayer();
    }
}

function setstop()
{
    if(AX)
    {
	document.getElementById('Player').controls.Stop()
        ispause=false;
	isplay=false;
    }
    else
    {
	registerNonMSIEVar('nieStart',false);
	registerNonMSIEVar('niePos',0);
	changeNonMSIEPlayer()
    }
}

function fullscrn()
{
    if(AX)
    {
	document.getElementById('Player').fullScreen=1    
    }
    else
    {
	notsupportedbrowser();
    }
}

function savesetting(type,val)
{
}


function jumptoitem(notule)
{
}



function jumpnext()
{
}

function jumpprevious()
{
}

function parsevideoplayer(file)
{
    if(AX)
    {
	// Give IE browser the fancy object, which makes it real easy to control
	retval='<OBJECT ID="Player" ';
	retval+='CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="279" height="200">';
	retval+='<PARAM name="autoStart" value="false">';
	retval+='<PARAM name="URL" value="'+file+'">';
	retval+='<PARAM name="uiMode" value="none">';
	retval+='<PARAM name="fullScreen" value="0">';
	retval+='<PARAM name="enableContextMenu" value="false">';
	retval+='</OBJECT>';
    }
    else
    {
	// Other browsers get an embed inside a div, not great to control, but best there is
	retval='<div id="fplayer" name="fplayer">';
	registerNonMSIEVar('nieFile',file);
	retval+=parseNonMSIEPlayer(file);
	retval+='</div>';
    }

    return retval;
}


function showbuffering()
{
    // hide buttons
    document.getElementById('controlbuttons').style.visibility='hidden';
    document.getElementById('controlbuttons').style.display='none'
    
    // show buffering process
    document.getElementById('controlbuffer').style.visibility='visible';
    document.getElementById('controlbuffer').style.display='block';
}

function showbuttons()
{
    // hide buffering process
    document.getElementById('controlbuffer').style.visibility='hidden';
    document.getElementById('controlbuffer').style.display='none';    

    // show buttons
    document.getElementById('controlbuttons').style.visibility='visible';
    document.getElementById('controlbuttons').style.display='block'
}


function parsebuttons()
{
    retval='';
    if(AX)
    {
	retval+='<div id=controlbuffer name=controlbuffer style="visibility:\'hidden\';display:\'none\'" class="buffercount"></div>';
    }
    retval +='<div id=controlbuttons name=controlbuttons style="visibility:\'visible\';display:\'block\'">';
    retval+='<table width="279" border="0" cellspacing="0" cellpadding="0">';
    retval+='<tr>';
    retval+='<td width="17"><a style=\"cursor:hand\" onMouseOut="ChangeImage(\'back\',\'img/buttons/btn_back_off.gif\')" onMouseDown="ChangeImage(\'back\',\'img/buttons/btn_back_on.gif\')"><img src="img/buttons/btn_back_off.gif" name="back" id="back" border="0" class="vidbtn" width="17" height="18"></a></td>';
    retval+='<td width="60" background="img/buttons/back_counter.gif"><div style=\"cursor:hand\" class="counter" id=timeshow name=timeshow onClick="swaptimeshow();">00:00:00</div></td>';
    retval+='<td width="18"><a style=\"cursor:hand\" onMouseOut="ChangeImage(\'forward\',\'img/buttons/btn_forward_off.gif\')" onMouseDown="ChangeImage(\'forward\',\'img/buttons/btn_forward_on.gif\')"><img src="img/buttons/btn_forward_off.gif" name="forward" id="forward" border="0" class="vidbtn" width="18" height="18"></a></td>';
    retval+='<td width="18"><a style=\"cursor:hand\" onMouseOut="ChangeImage(\'stop\',\'img/buttons/btn_stop_off.gif\')" onMouseDown="ChangeImage(\'stop\',\'img/buttons/btn_stop_on.gif\')" onClick="setstop()"><img src="img/buttons/btn_stop_off.gif" name="stop" id="stop" border="0" class="vidbtn" width="18" height="18"></a></td>';
    retval+='<td width="30"><a style=\"cursor:hand\" onMouseOut="ChangeImage(\'play\',\'img/buttons/btn_play_off.gif\')" onMouseDown="ChangeImage(\'play\',\'img/buttons/btn_play_on.gif\')" onClick="setplay()"><img src="img/buttons/btn_play_off.gif" name="play" id="play"  border="0" class="vidbtn" width="30" height="18"></a></td>';
    retval+='<td width="17"><a style=\"cursor:hand\" onMouseOut="ChangeImage(\'pause\',\'img/buttons/btn_pause_off.gif\')" onMouseDown="ChangeImage(\'pause\',\'img/buttons/btn_pause_on.gif\')" onClick="swappause()"><img src="img/buttons/btn_pause_off.gif" name="pause" id="pause" border="0" class="vidbtn" width="17" height="18"></a></td>';
    retval+='<td width="4"><img src="img/buttons/spacer.gif" width="6" height="18"></td>';
    retval+='<td width=78 background="img/buttons/slider_back.gif" align=center>';
    retval+='<div class="slider" id="slider-1" tabIndex="1" >';
    retval+='<input class="slider-input" id="slider-input-1" type="hidden">';
    retval+='</div>';
    retval+='</td>';
    retval+='<td width="18"><a onMouseOut="ChangeImage(\'mute\',\'img/buttons/btn_mute_off.gif\')" onMouseDown="ChangeImage(\'mute\',\'img/buttons/btn_mute_on.gif\')" onClick="swapmute()" ><img src="img/buttons/btn_mute_off.gif" name="mute" id="mute" border="0" class="vidbtn" width="18" height="18"></a></td>';
    retval+='<td width="19"><a onMouseOut="ChangeImage(\'fullscreen\',\'img/buttons/btn_fullscreen_off.gif\')" onMouseDown="ChangeImage(\'fullscreen\',\'img/buttons/btn_fullscreen_on.gif\')" onClick="fullscrn();"><img src="img/buttons/btn_fullscreen_off.gif" name="fullscreen" id="fullscreen" border="0" class="vidbtn" width="19" height="18"></a></td>';
    retval+='</tr>';
    retval+='</table>';
    retval+='</div>';
    
    
    return retval;
}


function notsupportedbrowser()
{
    alert('deze functie wordt niet ondersteund door uw browser.\nHiervoor dient u gebruik te maken van Internet Explorer');
}

function registerNonMSIEVar(varname,value)
{
    // currently used vars : nieFile,nieStart,niePos
    
    if(varname=='nieFile')
    {
	nieFile=value;
    }
    if(varname=='nieStart')
    {
	nieStart=value;
    }
    if(varname=='niePos')
    {
	niePos=value;
    }
}

function changeNonMSIEPlayer()
{
    // extra check for non MSIE
    if(!AX)
    {
	document.getElementById('fplayer').innerHTML=parseNonMSIEPlayer();
    }
}

function parseNonMSIEPlayer()
{
    retval='<EMBED type="application/x-mplayer2"';
    retval+='  ID="FFPlayer"';
    retval+='  pluginspage = "http://www.microsoft.com/Windows/MediaPlayer/"';
    retval+='  SRC="'+nieFile+'"';
    retval+='  name="FFPlayer"';
    retval+='  width="279"';
    retval+='showstatusbar="0"';
    if(nieStart)
	retval+='  AutoStart="1"';
    else    
	retval+='  AutoStart="0"';
    retval+='  currentPosition="'+niePos+'"';
//    retval+='  EnableContextMenu="1"';
    retval+='  ShowControls="0"';
//    retval+='  AnimationAtStart="0"';
//    retval+='  fullscreen="'+nieFull+'"';
//    retval+='  ShowStatusBar="0"';
    retval+='  SendOpenStateChangeEvents="1"'; 

    retval+='</EMBED>';
    
    return retval;
}

/*
It's the end, you can stop staring at our code now ;)
*/
