/**********************************************************************************/
/* Virtools Web Player                                                            */
/* (for more information see http://player.virtools.com/downloads/update3.5.asp)  */
/*                                                                                */
/* This javascript file contains a set of functions to install (and check the     */
/* version) of the Virtools Web Player plugin for gecko based browser (meaning    */
/* Netscape 6.1+, Mozilla 1.x and Firefox 1.x) under Microsoft Windows operating  */
/* system.                                                                        */
/*                                                                                */
/* IMPORTANT REMARKS: The Netscape Communicator/Navigator 4.x family is no longer */
/* supported by Virtools.                                                         */
/**********************************************************************************/

/**********************************************************************************/
/* Here are some instructions to control the installation of the                  */
/* Virtools Web Player without being redirected to the Virtools Web Site.         */
/*                                                                                */
/* 1. Download DetectBrowser.js and TestNPVirtools3.5.js (this file) from         */
/*    http://player.virtools.com/downloads/update3.5.asp                          */
/* 2. Change the value of "redirectionURL" (at the begining of                    */
/*    TestNPVirtools3.5.js) to a web page you want to be redirected to.           */
/* 3. Include this in the header of your web page.                                */
/*    <script src="DetectBrowser.js"></script>                                    */
/*    <script src="TestNPVirtools3.5.js"></script>                                */
/* 4. Call AutoInstallLast() (or on the AutoInstallLast functions) to start       */
/*    the check/installation process.                                             */
/*                                                                                */
/* Note: Since this file will be hosted on your website, you will have to update  */
/* it each time a new release of the Virtools Web Player is available  		      */
/* (you have to update the AutoInstallLast functions to replace the value         */
/*	of the player version).                                                       */
/*                                                                                */
/* Example:                                                                       */
/*                                                                                */
/* <a href="#" OnClick="javascript:AutoInstallLast();">install Virtools Player</a>*/
/*                                                                                */
/* Remarks: for security reasons the browser is not allowed to install a          */
/* plugin from the OnLoad handler. It means the final user of your website will   */
/* have to "click" on a link/button so the check/install function is called.      */
/**********************************************************************************/


// Used by gecko based browser to redirect the browser after the installation of the plugin.
// When you are using this script on our own website you can change this value.
// Take a look at the explaination at the beninning of this file.
var redirectionURL = "play.html";

/**************************************************************************************************/
/* Auto install the plug-in last version with XPInstall (gecko).                                  */
/*                                                                                                */
/* The function return an integer :                                                               */
/*   true  : if something was installed.                                                          */
/*   false : if nothing was installed.                                                            */
/**************************************************************************************************/
function AutoInstallLast()
{
  return AutoInstall(3,5,0,32);
}

/**********************************************************************************/
/* Test if the system and the browser are compatible to install/launch the plugin.*/
/* Possible return value are:                                                     */
/*  0 : the system or the browser is not compatible.                              */
/*  2 : gecko browser detected (netscape 6+, mozilla, firefox)                    */
/*                                                                                */
/* If 0 is returned an alert box is displayed.                                    */
/**********************************************************************************/
function TestSystem()
{
  // this script only supports windows plateform
  if (!is_win32)
  {
    return 0;
  }
  
  // we only support gecko based browser (netscape navigator 4.x (x >= 5) is no longer supported)
  // it means:
  //  - netscape 6.1+ & 7.x
  //  - mozilla 1.x
  //  - firefox 1.X
  //if (!(is_gecko || is_nav)) {
  if (!is_gecko)
  {
    return 0;
  }
  
  // here we check the navigator is able to install software
  // the method used depend on the browser type: navigator or gecko
  
  // gecko version
  if (is_gecko) {
    // check it is a known gecko browser
    if (!(is_nav6up || is_moz || is_fb || is_fx))
    {
        return 0;
    }
  
    
    // is XPInstall enable ?
    if (!InstallTrigger.enabled())
    {
      return 0;
    }
    return 2;
  }
  return 0;
}

/******************************************************************/
/* Return true if the npvirtools.dll has been loaded by netscape. */
/******************************************************************/
function VirtoolsDllHere()
{
  // here we go through the plugin loaded by the bowser
  // to look for the virtools one.
  
  for(i=0;i<navigator.plugins.length;i++) {
    // the current plugin
    myplug = navigator.plugins[i];
    
    // first we check the name of the plugin
    index = myplug.filename.lastIndexOf('\\');
    if (index!=-1) {
      sub = myplug.filename.substring(index+1,myplug.filename.length);
      if (sub.toLowerCase()!="npvirtools.dll") {
        continue;
      }
    } else if (myplug.filename.toLowerCase()!="npvirtools.dll") {
      continue;
    }
    
    // now we check the mime type supported by the plugin  
    for(j=0;j<myplug.length;j++) {
      if(myplug[j].type=='application/x-virtools' || myplug[j].type=='application/x-nemo') {
        return true;
      }
    }
    
  }
  	
  // the plugin has not been found
  return false;
}

/******************************************************************/
/* Return the version of the plugin.                              */
/* Return null if the plugin is not registered.                   */
/* The object returned is an InstallVersion                       */
/* (see XPInstall API Reference)                                  */
/******************************************************************/
function VirtoolsPluginVersion()
{
  return InstallTrigger.getVersion("plugins/virtools/VirtoolsPI/");
}

/******************************************************************/
/* Test if the plug-in is present on the system.                  */
/*                                                                */
/* The function return an integer :                               */
/*   0 : if the system is not ready to use the plug-in            */
/*       (in this case an alert box is displayed).                */
/*   1 : if the plug-in dll is not installed on the system.       */
/*   2 : if the plug-in dll is installed on the system but not    */
/*       register with XPInstall.                                 */
/*   3 : if the plug-in dll is registered with XPInstall but the  */
/*       version is lesser than the current.                      */
/*   4 : all is ok.                                               */
/******************************************************************/
function TestNPVirtools(maj,min,rev,bld)
{
  // the system or browser is not ok
  if (!TestSystem()) {
    return 0;
  }

  // cannot find npvirtools.dll in the plugins loaded by the browser
  if (!VirtoolsDllHere()) {
    return 1;
  }

  if (is_gecko) {
    // gecko browser
    
    // compare the the version needed with the current version
    ver = InstallTrigger.compareVersion("plugins/virtools/VirtoolsPI/",maj,min,rev,bld);
    
    switch(ver) {
      // component not present or not registered
      case -5:
        return 2;
        break;
 	
      // current version as a smaller (earlier) build number than the needed version
      case -1:
      // current version as a smaller (earlier) release number  than the needed version
      case -2:
      // current version as a smaller (earlier) minor number than the needed version
      case -3:
      // current version as a smaller (earlier) major number than the needed version
      case -4:
        return 3;
        break;
 		
      // the versions are the same	
      case 0:
      case 1:
      // current version as a larger (newer) build number than the needed version
      case 2:
      // current version as a larger (newer) release number than the needed version
      case 3:
      // current version as a larger (newer) minor number than the needed version
      case 4:
      // current version as a larger (newer) major number than the needed version
        return 4;
        break;
    }

    // unknow return value
    // component not present or not registered
    return 2;
  }
  
  // navigator browser no longer supported.
  
  return 0;
}

/******************************************************************/
/* Call TestNPVirtools and display a string in an alert box       */
/* associated with the return value of TestNPVirtools.            */
/* This function is here for debug purpose.                       */
/******************************************************************/
function DisplayTestNPVirtools(maj,min,rev,bld)
{
  switch (TestNPVirtools(maj,min,rev,bld)) {
    case 1 :
      alert("Virtools Web Player is not installed.");
      break;
	  		
    case 2 :
      alert("Virtools Web Player is installed but not registered in your browser.");
      break;
	  		
    case 3 :
      alert("Virtools Web Player is installed but newer version available.");
      break;
	  		
    case 4 :
      alert("Virtools Web Player installed.");
      break;
	  		
    default:
      alert("Virtools Web Player seems to not be installed.");
      break;
  }
}

/**************************************************************************************************/
/* Install the plug-in with XPInstall (gecko).                                                    */
/* If the current version of the plugin is 3.5.0.32 call SmartPluginInstall(3,5,0,32).            */
/*                                                                                                */
/* Parameters:                                                                                    */
/*   - maj (integer)      : The major version number.                                             */
/*   - min (integer)      : Minor version number.                                                 */
/*   - rev (integer)      : Revision number.                                                      */
/*   - bld (integer)      : Build number.                                                         */
/*                                                                                                */
/* The function return an integer :                                                               */
/*   0 : if the system is not ready to use the plug-in (in this case an alert box is displayed).  */
/*   1 : the installation failed (an alert box is displayed)                                      */
/*   2 : the plug-in will be installed.                                                           */
/**************************************************************************************************/
function SmartPluginInstall(maj,min,rev,bld)
{
  // test the system
  if(!TestSystem()) {
    return 0;
  }

  if (is_gecko) {
    // gecko browser

    // build the xpi url
    var fxpi = "http://a532.g.akamai.net/f/532/6712/5m/virtools.download.akamai.com/6712/player/install3.5/virtoolswebplayer.xpi";
    var xpi = {'Virtools Web Player installation':fxpi};
    
    // start the installation
    if (!InstallTrigger.install(xpi,VWPICallback)) {
      return 1;
    }
    return 2;
  }
  
  // navigator browser no longer supported.
  
  return 0;
}

/**************************************************************************************************/
/* Auto install the plug-in with XPInstall (gecko).                                               */
/* If the current version of the plugin is 3.5.0.32 call AutoInstall(3,5,0,32).                   */
/* Auto installing means SmartPluginInstall is called only if needed.                             */
/*                                                                                                */
/* Parameters:                                                                                    */
/*   - maj (integer)      : The major version number.                                             */
/*   - min (integer)      : Minor version number.                                                 */
/*   - rev (integer)      : Revision number.                                                      */
/*   - bld (integer)      : Build number.                                                         */
/*                                                                                                */
/* The function return an integer :                                                               */
/*   true  : if something was installed.                                                          */
/*   false : if nothing was installed.                                                            */
/**************************************************************************************************/
function AutoInstall(maj,min,rev,bld)
{
  // install the plugin depending on the value returned by TestNPVirtools.
  switch(TestNPVirtools(maj,min,rev,bld))
  {
    // the system is not ok
    case 0 :
      return false;
      break;

    // the plugin is already installed with the good version
    case 4 :
      if (redirectionURL.length>0) {
        self.location = redirectionURL;
      }
      return false;
      break;

    // the plugin is installed but not registered	
    case 2 :
      if (confirm("Virtools Web Player is installed but not registered in your browser.\nDo you want to install through Automatic Install (recommended)?")) {
        if (SmartPluginInstall(maj,min,rev,bld)!=2) {
          return false;
        }
        return true;
      }

      if (redirectionURL.length>0) {
        self.location = redirectionURL;
      }
      return false;
      break;

    // the plugin is installed and registered with an older version
    case 3 :
      if (confirm("Virtools Web Player is installed but newer version available.\nDo you want to install it (recommended)?")) {
        if (SmartPluginInstall(maj,min,rev,bld)!=2) {
          return false;
        }
        return true;
      }

      if (redirectionURL.length>0) {
        self.location = redirectionURL;
      }
      return false;
      break;

    // the plugin is not installed nor registered
    case 1 :
    default:
      if (SmartPluginInstall(maj,min,rev,bld)!=2) {
        return false;
      }
      return true;
      break;
  }

  return false;
}

/**************************************************************************************************/
/* Control the behavior of the installation process under gecko based browser.                    */
/* The main goal is to display error message in dialog box or to redirect the browser to a new    */
/* web page when the installation is done. The redirection is done using the value of             */
/* redirectionURL.                                                                                */
/**************************************************************************************************/
function VWPICallback(url,status)
{
  if (status==0) {
    if (redirectionURL.length>0) {
      setTimeout("window.location='" + redirectionURL + "'",1);
    }
  } else if(status==999) {
    setTimeout("alert('You must restart your browser to complete the Virtools Web Player installtion.')",1);
  } else if(status==-210) {
    setTimeout("alert('Installation stoped by user. Aborting setup.')",1);
  } else {
    setTimeout("alert('Installation error. Aborting.')",1);
  }
}

