/*****************************************************************************************
 *
 *		Tuxia Primitives
 *              Version 1.9 MCS
 *
 ****************************************************************************************/

var shellCommand = null;
var vod = null;
var video = null;
var sound = null;
var configd = null;

var fixedSdpFileName =  "/wfs/mpg/currentmp4.sdp";
var tv_standard      = null;
var currentVersion = "";
var globalFirstTime  = 0;

/*-----------------------------------------------------------*/
function shellCommandObj()
{ 
    this.shellCommandXPCOM	= null;
    this.getVersionInterface= null;
    var aComp				= null;
    var aVerComp             = null;

    try
    {		
        aComp = Components.classes["@tuxia.com/goannaComponents/shellCommand;1"].getService();
        if (aComp != null)
            this.shellCommandXPCOM = aComp.QueryInterface(Components.interfaces.IshellCommand);
    }
    catch(e)
    {
        alert("Could not get IshellCommand interface. "+e.toString());
        return;
    }

    try
    {
        aVerComp = Components.classes["@tuxia.com/goannaComponents/getVersion;1"].getService();
        if (aVerComp != null)
            this.getVersionInterface = aVerComp.QueryInterface(Components.interfaces.IGetVersion);
    }
    catch(e)
    {
        currentVersion = "3.69";
        this.getVersionInterface = null;
    }
}

shellCommandObj.prototype.RunShellCommand = function(commandString)
{
    if (this.shellCommandXPCOM != null)
    {
        this.shellCommandXPCOM.RunShellCommand(commandString);
    }
}

shellCommandObj.prototype.GetVersion = function()
{
    var retVal = 0;
    if (this.getVersionInterface != null)
    {
        try
        {
            retVal = this.getVersionInterface.GetTasteVersion();
        }
        catch(e)
        {
            alert(e.toString());
        }
    } 
    return(retVal);
}

shellCommandObj.prototype.GetSDPFile = function(url)
{
    if (this.shellCommandXPCOM != null)
    {
      //construct the command to get the sdp file
      var command = "curl "+url+" > "+fixedSdpFileName;

      //Run the shell command to get the SDP file.
      this.RunShellCommand(command);
    } 
}

/*-----------------------------------------------------------*/
function configdObj()
{
    this.configdXPCOM = null;
    var aComp = null;

    // Get configuration manager service and query interface
    try
    {
        aComp = Components.classes["@tuxia.com/goannaComponents/configdComp;1"].createInstance();
        if(aComp != null)
		    this.configdXPCOM = aComp.QueryInterface(Components.interfaces.IconfigdComp);					
    }
    catch(e)
    {
        alert("Could not create component:\n"+e.toString());
        return;
    }

    if (this.configdXPCOM != null)
    {
        // Login to configuration manager
        this.configdXPCOM.Init("localhost");
    }
}

configdObj.prototype.GetValue = function(compName)
{
    return this.configdXPCOM.GetValue(compName);
}

/*-----------------------------------------------------------*/
function tvia5005Obj()
{
    this.port = 0x00;
    this.xloc = 0;
    this.yloc = 0;
    this.alphar = 0;
    this.alphag = 0;
    this.alphab = 0;
    this.tviaXPCOM = null;
    this.CAPTURE_PORT_VOD = 0x00;          // EM8400 (VOD) Capture port A
    this.CAPTURE_PORT_TV = 0x80;           // (tuner?) Capture port B
    var aComp = null;

    try
    {
        // Create instance of TVIA5000 object
        aComp = Components.classes["@tuxia.com/goannaComponents/tvia5000;1"].createInstance();
        if(aComp != null)
            this.tviaXPCOM = aComp.QueryInterface(Components.interfaces.Itvia5000);
    }
    catch(e)
    {
        alert("Could not create component:\n"+e.toString());
        return;
    }

    //set defaults
    if(this.tviaXPCOM != null)
    {
        if(tv_standard == "ntsc")
        {
            this.tviaXPCOM.SetSourceGeometry( 2, 0, 716, 480);            
        }
        else // PAL
        {
            this.tviaXPCOM.SetSourceGeometry( 2, 0, 716, 576);
        }

        this.tviaXPCOM.SetEnableBobWeave(false);
        this.tviaXPCOM.SetEnableKeyColor( true);
        this.tviaXPCOM.SetPlaybackKeyColor( 255, 255, 255);
        this.tviaXPCOM.SetEnableInterlace( true);

        if(currentVersion > "3.69")
        {					
            if(tv_standard == "ntsc")
                this.tviaXPCOM.SetStandard( true );
            else
                this.tviaXPCOM.SetStandard( false );
        }
    }
    else
    {
        alert ("this.tviaXPCOM is NULL");
    }
}

tvia5005Obj.prototype.noVideo = function()
{
    if(this.tviaXPCOM != null)
    {
        this.tviaXPCOM.SetCapturePort(this.port);
        this.tviaXPCOM.SetPlaybackGeometry(0, 0, 0, 0);
        this.tviaXPCOM.SetEnableBobWeave(false);
        this.tviaXPCOM.SetEnablePlayback(false);
        this.tviaXPCOM.CallSetupVideo();
        this.tviaXPCOM.SetEnableAlphaBlending( false);
        this.tviaXPCOM.CallSetupAlphaBlending();
    }
}

tvia5005Obj.prototype.setVideoLocationSize = function(x, y, w, h)
{
    if(this.tviaXPCOM != null)
    {
        // for SCR 1270
        if ((tv_standard == "ntsc") && (globalFirstTime == 0))
        {
            video.setFullScreen();
            globalFirstTime = 1;
        }
        // for SCR 1270

        this.xloc = x;
        this.yloc = y;
        this.tviaXPCOM.SetCapturePort(this.port);
        this.tviaXPCOM.SetPlaybackGeometry(x, y, w, h);
        this.tviaXPCOM.SetEnableKeyColor( true);
        this.tviaXPCOM.SetPlaybackKeyColor( this.alphar, this.alphag, this.alphab);
        this.tviaXPCOM.SetEnablePlayback(true);

        if (tv_standard == "ntsc")
        {
            this.tviaXPCOM.SetEnableCloseCaptionPassThru(false, 0);
            this.tviaXPCOM.SetEnableBobWeave(true);
        }
        else // PAL
            this.tviaXPCOM.SetEnableBobWeave(false);

        this.tviaXPCOM.CallSetupVideo();
    }
}

tvia5005Obj.prototype.closeScreen = function()
{
    if(this.tviaXPCOM != null)
    {
        this.tviaXPCOM.SetCapturePort(this.port);
        this.tviaXPCOM.SetPlaybackGeometry(0, 0, 0, 0);
        this.tviaXPCOM.SetEnablePlayback(false);
        this.tviaXPCOM.CallSetupVideo();
    }
}

tvia5005Obj.prototype.setTransparentColor = function (r, g, b)
{
    this.alphar = r;
    this.alphag = g;
    this.alphab = b;
}

tvia5005Obj.prototype.setFullScreen = function ()
{
	if(this.tviaXPCOM != null)
	{
		this.tviaXPCOM.SetEnableAlphaBlending( false);
		this.tviaXPCOM.CallSetupAlphaBlending();
		this.tviaXPCOM.SetCapturePort(this.port);
      
		if(tv_standard == "ntsc")
    {
      this.tviaXPCOM.SetPlaybackGeometry(0, 0, 720, 480);
      if (currentVersion >= "3.71") 
        this.tviaXPCOM.SetEnableCloseCaptionPassThru(true, 5);

			this.tviaXPCOM.SetEnableBobWeave(true);
    }
		else // PAL
    {
			this.tviaXPCOM.SetPlaybackGeometry(0, 0, 720, 576);
			this.tviaXPCOM.SetEnableBobWeave(false);
    }

		this.tviaXPCOM.SetEnablePlayback(true);
		this.tviaXPCOM.SetEnableKeyColor( true);
		this.tviaXPCOM.SetPlaybackKeyColor( this.alphar, this.alphag, this.alphab);
		this.tviaXPCOM.CallSetupVideo();
	}
}

tvia5005Obj.prototype.setTransparentColorOn = function ()
{
    if(this.tviaXPCOM != null)
    {
        this.tviaXPCOM.SetEnableKeyColor( false);
        this.tviaXPCOM.CallSetupVideo();
        this.tviaXPCOM.SetEnableAlphaBlending( true);
        this.tviaXPCOM.SetEnableAlphaWindow(0, true);
        this.tviaXPCOM.SetAlphaWindowSources(0, 0 ,0);
        this.tviaXPCOM.SetAlphaWindowColor(0, this.alphar, this.alphag, this.alphab);

        if(tv_standard == "ntsc")
            this.tviaXPCOM.SetAlphaWindowCoor(0, 0, 0, 720, 480);
        else
            this.tviaXPCOM.SetAlphaWindowCoor(0, 0, 0, 720, 576);

        this.tviaXPCOM.CallSetupAlphaBlending();
    }
}

tvia5005Obj.prototype.setTransparentColorOff = function ()
{
	if(this.tviaXPCOM != null)
	{
		this.tviaXPCOM.SetEnableAlphaBlending( false);
		this.tviaXPCOM.CallSetupAlphaBlending();
		this.tviaXPCOM.SetEnableKeyColor( false);
		this.tviaXPCOM.SetPlaybackKeyColor( this.alphar, this.alphag, this.alphab);
		this.tviaXPCOM.CallSetupVideo();
	}
}

tvia5005Obj.prototype.setCapturePort = function(newPort)
{
	this.port = newPort;
}

/*-----------------------------------------------------------*/
function vodSigmaObj(configdObj)
{
    this.vod_url = null;
    this.confd = configdObj;
    this.vodXPCOM = null;
    this.vod_rtsp_streaming_options = 0;
    var aComp                       = null;

    try
    {
        aComp = Components.classes["@tuxia.com/goannaComponents/VOD;1"].createInstance();
        if(aComp != null)
            this.vodXPCOM = aComp.QueryInterface(Components.interfaces.IVOD);
    }
    catch(e)
    {
        alert("Could not create component:\n"+e.toString());
        return;
    }
}

vodSigmaObj.prototype.EnableDestWindow = function(enable)
{
    if(this.vodXPCOM != null)
    {
        this.vodXPCOM.EnableDestWindow(enable);
    }
}

vodSigmaObj.prototype.SetDestWindow = function(width, height)
{
    if(this.vodXPCOM != null)
    {
        this.vodXPCOM.SetDestWindow(width, height);
    }
}

vodSigmaObj.prototype.SetStandard = function()
{
	if(this.vodXPCOM != null)
	{
        if (tv_standard == "pal")
            this.vodXPCOM.SetStandard(1);
        else
            this.vodXPCOM.SetStandard(0);
	}
}

vodSigmaObj.prototype.Open = function()
{	
    var retval = 0;

//rdp example of use of function
//SendStringToSerialPort("We are in open (local).\n\r");

    if (this.vodXPCOM != null)
    {		
        this.vodXPCOM.Close();    // <rdp 05/02/2005> for SCR 1224

		if (currentVersion > "3.69")
		{
			/*The following values are applicable only for MPEG-4 streams.
			  Even if 'SetOptions' is called, the options are ignored for MPEG-1 and MPEG-2 streams*/
			var dchar1 = 0;
			var dchar2 = 0;
			var dchar3 = 0;
			var dchar4 = 0;
      var dchar5 = 0;
      var dchar6 = 0;
      var dchar7 = 0;
			
			dchar1=this.confd.GetValue("browsers/nanozilla/base/interface/chrome-STB/purple.SECONDS_FOR_BUFFERING");
 			dchar2=this.confd.GetValue("browsers/nanozilla/base/interface/chrome-STB/purple.AUDIO_DATAPORT");
 			dchar3=this.confd.GetValue("browsers/nanozilla/base/interface/chrome-STB/purple.VIDEO_DATAPORT");
 			this.vodXPCOM.SetOptions(dchar1, dchar2, dchar3);
			
			dchar4=this.confd.GetValue("browsers/nanozilla/base/interface/chrome-STB/purple.RTP_DATAPORT");
 			this.vodXPCOM.SetRTPDataPort(dchar4);

        if (currentVersion >= "3.71") 
        { 
            dchar4 = this.confd.GetValue("browsers/nanozilla/base/interface/chrome-STB/purple.CC_DATAPORT");
            this.vodXPCOM.SetCCOptions(dchar4);

            this.vodXPCOM.SetRtspStreamingOptions(this.vod_rtsp_streaming_options); //set via SetVideoTypeName()

            dchar5 = this.confd.GetValue("browsers/nanozilla/base/interface/chrome-STB/purple.BUFFER_CHECK");
            if ("yes" == dchar5)
            {
                dchar6 = 1;
                dchar7 = this.confd.GetValue("browsers/nanozilla/base/interface/chrome-STB/purple.BITRATE_BUFFER_CHECK");
            }
            else
            {
                dchar6 = 0;
                dchar7 = 0;
            }
            this.vodXPCOM.SetBufferCheckOptions(dchar6, dchar7);

            // this is NOT local-fullscreen mode, we DON'T want unconditional retries....
            this.vodXPCOM.SetOpenRetryOptions(0, 0, 0, 0);
            }
        }
        
        retval = this.vodXPCOM.Open(this.vod_url, 0, 0); //this.vod_url is set in setVODURL() called by SetVideoTypeName()
        if(retval != 0)
        {
            alert("Unable to open url - "+this.vod_url);
            return;
        }

        if (currentVersion > "3.69")
        {
            this.SetStandard();
        }
    }
}

vodSigmaObj.prototype.Play = function()
{
	if(this.vodXPCOM != null)
	{
		this.vodXPCOM.Play();
	}
}

vodSigmaObj.prototype.Rewind = function()
{
	if(this.vodXPCOM != null)
	{
		this.vodXPCOM.Rewind();
	}
}

vodSigmaObj.prototype.FastForward = function()
{
	if(this.vodXPCOM != null)
	{
		this.vodXPCOM.FastForward();
	}
}

vodSigmaObj.prototype.Pause = function()
{
	if(this.vodXPCOM != null)
	{
		this.vodXPCOM.Pause();
	}
}

// rdp 05/02/2005 this function is obsolete (now is a noop) -- always call Close instead (or in tandem)
vodSigmaObj.prototype.Stop = function()
{
	if(this.vodXPCOM != null)
	{
		this.vodXPCOM.Stop();
	}
}

vodSigmaObj.prototype.Close = function()
{
    if (this.vodXPCOM != null)
    {
        this.vodXPCOM.Close();
    }
}

vodSigmaObj.prototype.setVODURL = function(new_vod_url)
{
	this.vod_url = new_vod_url;
}

vodSigmaObj.prototype.setRtspStreamingOptions = function(option)
{
  if (currentVersion >= "3.71") 
    this.vod_rtsp_streaming_options = option;
}

/*-----------------------------------------------------------*/
function soundMixerObj(configd)
{
    this.soundMixerXPCOM = null;
	this.confd = configd;
	this.main_vol_val		= 0;
	this.line1_vol_val    = 0;
	var aComp				= null;

	try
	{
		aComp = Components.classes["@tuxia.com/goannaComponents/soundMixer;1"].getService();
		if(aComp != null)
   		this.soundMixerXPCOM = aComp.QueryInterface(Components.interfaces.IsoundMixer);
	}
	catch(e)
	{
  		alert("Could not create component:\n"+e.toString());
        return;
	}
	if(currentVersion > "3.69")
	{
		this.main_vol_val = this.GetConfiguredVolume();

		this.line1_vol_val = this.GetConfiguredLine1Volume();

		if(this.main_vol_val != this.line1_vol_val)
		{
			this.soundMixerXPCOM.SetLine1Volume(this.main_vol_val);
		}
		this.SetMainVolume(this.main_vol_val);
	}
}

soundMixerObj.prototype.GetMainVolume = function()
{
    return (this.soundMixerXPCOM.GetMainVolume());
}

soundMixerObj.prototype.MuteToggle = function()
{
	this.soundMixerXPCOM.Mute();
}

soundMixerObj.prototype.SetMainVolume = function(volume)
{
	this.main_vol_val=volume;
	this.soundMixerXPCOM.SetMainVolume(volume);
	this.soundMixerXPCOM.SetLine1Volume(volume);
}

soundMixerObj.prototype.MainVolumePlus = function()
{
	this.main_vol_val = parseInt(this.main_vol_val, 10) + parseInt(5, 10); //5 is an arbitrary number...can be changed	
	if(this.main_vol_val > 100)
	{
		this.main_vol_val = 100;
	}	
	if(this.soundMixerXPCOM != null)
	{
		this.SetMainVolume(this.main_vol_val);
	}
}

soundMixerObj.prototype.MainVolumeMinus = function()
{
	this.main_vol_val = parseInt(this.main_vol_val, 10) - parseInt(5, 10); //5 is an arbitrary number...can be changed	
	
	if(this.main_vol_val < 0)
	{
		this.main_vol_val = 0;
	}	
	if(this.soundMixerXPCOM != null)
	{
		this.SetMainVolume(this.main_vol_val);
	}
}

soundMixerObj.prototype.GetConfiguredVolume = function()
{
	var dchar = null;
	dchar = this.confd.GetValue("browsers/nanozilla/base/interface/chrome-STB/purple.MAIN_VOL");
	return dchar;
}

soundMixerObj.prototype.GetConfiguredLine1Volume = function()
{
    var dchar = null;
    dchar = this.confd.GetValue("browsers/nanozilla/base/interface/chrome-STB/purple.LINE1_VOL");
    return dchar;
}

/*-----------------------------------------------------------*/
function initSTB( version )
{
	var currentVersionNumber = 0;
	
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	configd = new configdObj();

	var dchar = null;
	if (configd != null)
	{
		dchar=configd.GetValue("display/xf86_base.RESOLUTION");
		if (dchar != null)
		{
			switch(dchar)
			{
				case "720x480-NTSC":
					tv_standard = "ntsc";
					break;

				case "720x576-PAL":
					tv_standard = "pal";
					break;

				default:
					tv_standard = "ntsc";
					break;
			}
		}
	}
    else
    {
        alert ("configD is NULL");
        return;
    }    
	
  shellCommand = new shellCommandObj();
  currentVersionNumber = shellCommand.GetVersion();
  if(currentVersionNumber > 0)
    currentVersion = ConvertVersionToString(currentVersionNumber);

    vod          = new vodSigmaObj(configd);
    video        = new tvia5005Obj();
	sound		 = new soundMixerObj(configd);
}

/*-----------------------------------------------------------*/
function NoVideo()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if(video != null)
    {
      video.noVideo();
    }
    if(vod != null)
    {
      vod.EnableDestWindow(2);
    }
}

/*-----------------------------------------------------------*/
function GetResolution()
{
    return tv_standard;
}

/*-----------------------------------------------------------*/
function SetVideoLocationSize (x, y, height, width)
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if ( video != null )
    {
      video.setVideoLocationSize(x, y, width, height);
    }
}

/*-----------------------------------------------------------*/
function SetFullScreen ()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if ( video != null )
    {
        video.setFullScreen ();
        if (tv_standard == "ntsc")
        {
            if (currentVersion >= "3.71") 
            {
                var command;

                command = "poketvia";
                shellCommand.RunShellCommand(command);
            }
        }
    }
    if(vod != null)
    {
      vod.EnableDestWindow(1);

	  if(tv_standard == "ntsc")
	  {
		vod.SetDestWindow(720, 480);
	  }
	  else
	  {
		vod.SetDestWindow(720, 576);
 	  }
    }
}

/*-----------------------------------------------------------*/
function SetTransparentColor (r, g, b)
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if ( video != null )
    {
       video.setTransparentColor (r, g, b);
    }
}

/*-----------------------------------------------------------*/
function SetTransparentColorOn()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if ( video != null )
    {
       video.setTransparentColorOn ();
    }
}

/*-----------------------------------------------------------*/
function SetTransparentColorOff ()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if ( video != null )
    {
       video.setTransparentColorOff ();
    }
}

/*-----------------------------------------------------------*/
/* In the special case of mpeg4 multicast the "name" parameter is actually a valid sdp string */
function SetVideoTypeName(vidType, name)
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

    var localURL = null;
    var workName = null;
	
    if (null != vod)
    {
        if ("MPEG4Multicast" == vidType)
        {
            writeSdpToFile(name);
            workName = fixedSdpFileName;

            localURL = "RTSP@ISMA_SDP_FILE";
        }
        else
        {
            workName = name;

            switch (vidType)
            {
                case "MPEG2Multicast":
                    localURL = "RTSP@MULTICAST_MPEG2_TRANSPORT";
                    break;

                case "MPEG2Kasenna":
                    localURL = "RTSP@KASENNA_MPEG2_TRANSPORT";
                    break;

                case "MPEG2Infovalue":
                    localURL = "RTSP@INFOVALUE_MPEG2_TRANSPORT";
                    break;

                case "MPEG1Multicast":
                    localURL = "RTSP@MULTICAST_MPEG1";
                    break;

                case "MPEG1Kasenna":
                    localURL = "RTSP@KASENNA_MPEG1";
                    break;

                case "MPEG1Infovalue":
                    localURL = "RTSP@INFOVALUE_MPEG1";
                    break;

                case "MPEG4Rtsp":
                case "MPEG4RtspForceUdp":
                    localURL = "RTSP@ISMA";
                    if (currentVersion >= "3.71") 
                      vod.setRtspStreamingOptions(0);
                    break;

                case "MPEG4RtspForceTcp":
                    localURL = "RTSP@ISMA";
                    if (currentVersion >= "3.71") 
                      vod.setRtspStreamingOptions(1);
                    break;
        
                case "MPEG4RtspAuto":
                    localURL = "RTSP@ISMA";
                    if (currentVersion >= "3.71") 
                      vod.setRtspStreamingOptions(2);
                    break;
        
		            case "SDPFile":
		                GetSDP(name);
		                workName = fixedSdpFileName;
		                localURL = "RTSP@ISMA_SDP_FILE";
                    break;

                case "UnicastMPEG1":
                    workName = "#dataport=" + workName;
                    localURL = "RTSP@UNICAST_MPEG1";
                    break;

                case "UnicastMPEG2Transport":
                    workName = "#dataport=" + workName;
                    localURL = "RTSP@UNICAST_MPEG2_TRANSPORT";
		                break; 
					
                default:
                    localURL = "RTSP@MULTICAST_MPEG2_TRANSPORT";
                    break;
            }
        }

        localURL = localURL + "://";
        localURL = localURL + workName;

        vod.setVODURL(localURL);
    }
}

/*-----------------------------------------------------------*/
// after this function is called the mouse pointer will disappear after 3 seconds of inactivity until
// ShowInactiveMousePointer is called to kill the unclutter background task....
function HideInactiveMousePointer()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    shellCommand.RunShellCommand("uncluttercontrol stop >/dev/null 2>&1");
    shellCommand.RunShellCommand("uncluttercontrol start 3 >/dev/null 2>&1");
}
function ShowInactiveMousePointer()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    shellCommand.RunShellCommand("uncluttercontrol stop >/dev/null 2>&1");
}

/*-----------------------------------------------------------*/
function Vod_Fastforward ()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if ( vod != null )
    {
      vod.FastForward();
    }
}

/*-----------------------------------------------------------*/
function Vod_Rewind ()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if ( vod != null )
    {
      vod.Rewind();
    }
}

/*-----------------------------------------------------------*/
function Vod_Pause ()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if ( vod != null )
    {
      vod.Pause();
    }
}

/*-----------------------------------------------------------*/
function Vod_Open ()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if ( vod != null )
    {
       vod.Open();
    }
}

/*-----------------------------------------------------------*/
function Vod_Play ()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if ( vod != null )
    {
       vod.Play();
    }
}

/*-----------------------------------------------------------*/
function Vod_Stop ()
{
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    if ( vod != null )
    {
       vod.Stop();
       vod.Close();
    }
}

/*-----------------------------------------------------------*/
function sound_GetMainVolume()
{
	var volume = 0;
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");	
	if(currentVersion <= "3.69")
	{
		volume = sound.GetConfiguredVolume();
		return;
	}
	else
	{		
		volume = sound.GetMainVolume();
	}
	return (volume);
}

/*-----------------------------------------------------------*/
function sound_SetMainVolume(volume)
{
	if(currentVersion <= "3.69")
		return;
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");	
	sound.SetMainVolume(volume);
}

/*-----------------------------------------------------------*/
function sound_MainVolumePlus()
{
	if(currentVersion <= "3.69")
		return;
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");	
	if(sound != null)
	{
		sound.MainVolumePlus();
	}
}

/*-----------------------------------------------------------*/
function sound_MainVolumeMinus()
{
	if(currentVersion <= "3.69")
		return;
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");	
	if(sound != null)
	{
		sound.MainVolumeMinus();
	}
}

/*-----------------------------------------------------------*/
function sound_Mute()
{
	if(currentVersion <= "3.69")
		return;
	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");	
	if(sound != null)
	{
		sound.MuteToggle();
	}	
}
/*-----------------------------------------------------------*/

function writeSdpToFile( sdpOriginalString )
{
    var stringCmd;
    var sdpSubstitutedString;

    // the input string has to be legal SDP, therefore the first and last char cannot be ', ", etc., etc. 
    sdpSubstitutedString = sdpOriginalString.charAt(0);
    for (var i = 1; i < sdpOriginalString.length; i++)
    {
        if (sdpOriginalString.charAt(i) == "\'")
        {
            sdpSubstitutedString = sdpSubstitutedString + "\'\"\'\"\'";
        }
        else 
        {
            sdpSubstitutedString = sdpSubstitutedString + sdpOriginalString.charAt(i);
        }
    }

    // first remove the file if it is there
    stringCmd = "rm -f " + fixedSdpFileName; 
    shellCommand.RunShellCommand(stringCmd);

    // bash does support printf!
    stringCmd = "printf '" + sdpSubstitutedString + "' >" +  fixedSdpFileName; 
    shellCommand.RunShellCommand(stringCmd);
}

function ConvertVersionToString(curVersionNumber)
{
	var verstring = "";
	var majorVersion = 0;
	var majorVersionStr = "";
	var alphabetNum = 0;
	var alphabetChar = "";
	
	if(curVersionNumber < 8192 || curVersionNumber > 11386)
		return verstring;
		
	majorVersion = ((curVersionNumber / 32) + 44) / 100;
	if(majorVersion < 1 || majorVersion > 100)
		return verstring;
		
	majorVersionStr = majorVersion.toString();
	if(majorVersionStr.length <= 3)
	{
		majorVersion = majorVersion.toFixed(2);
		majorVersionStr = majorVersion.toString();
	}
	
	var decimalPos = majorVersionStr.indexOf('.');
	majorVersionStr = majorVersionStr.slice(0, decimalPos + 3);
	alphabetNum = curVersionNumber % 32;
	alphabetNum = 96 + alphabetNum;
	if(alphabetNum >= 97 && alphabetNum <= 122)
		alphabetChar = String.fromCharCode(alphabetNum);
	verstring = majorVersionStr + alphabetChar;
	return verstring;
}

function GetVersion()
{
	//VBrick/<hwtype>v<version>
	var retString = "VBrick/00v0"+currentVersion;
	return retString;
}

function GetSDP(url)
{
	if(currentVersion <= "3.69")
		return;
	shellCommand.GetSDPFile(url);
}

function SendStringToSerialPort(stringText)
{
  var dchar = 0;
  var command;

  dchar = configd.GetValue("system/shell/serial_shell.PASSTHROUGH_ACTIVE");
  if ("yes" == dchar)
  {
    command = "printf '" + stringText + "' > /dev/ttyS0"; 
    shellCommand.RunShellCommand(command);
  }
}
