
function getOperatingSystem() {
  switch(navigator.platform[0]) {
    case 'M': return 'mac';
    case 'L': return 'linux';
    default: return 'windows';
  }
}

function hasWebotsPlayerPlugin() {
  result=false;
  if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.platform[0]!='M') { // Internet Explorer on Windows
    document.write('<SCRIPT LANGUAGE=VBScript\>\n');
    document.write('on error resume next\n');
    document.write('result = IsObject(CreateObject("WPLAYER.wplayerCtrl.1"))\n');
    document.write('</SCRIPT\>\n');
    // Other option to do it
    //var obj=0;
    //try {
    //  obj=new ActiveXObject("WPLAYER.wplayerCtrl.1");
    //  } catch (e) {}
    // if (obj!=0) result=true;
  } else if (navigator.plugins && navigator.plugins.length > 0) {
    var pluginsArrayLength = navigator.plugins.length;
    for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
      //document.writeln('found: '+navigator.plugins[pluginsArrayCounter].name+"<br>")
      if (navigator.plugins[pluginsArrayCounter].name.indexOf("Webots Player") >=0) {
        result=true;
      }
    }
  }
  return result;
}

function installWebotsPlayerXPICallback(url,status) {
  if (status == 0) {
    alert("Webots Player successfully installed");
    window.location.reload(false);
  }
}

function installWebotsPlayerXPI() {
  xpi={'Webots Player plugin':'http://www.cyberbotics.com/wplayer/'+getOperatingSystem()+'/wplayer.xpi'};
  InstallTrigger.install(xpi,installWebotsPlayerXPICallback);
}

function displayWebotsPlayer(url,width,height) {
  if (hasWebotsPlayerPlugin()==false) {
    document.writeln('<table width="'+width+'" height="'+height+'"><tr><td align="center">');
    document.writeln('<h3>Webots Player plugin missing!</h3>');
    if (navigator.userAgent.indexOf("Safari")!=-1) {
      document.write('<p>Download it from <a href=');
      document.write('"http://www.cyberbotics.com/wplayer/mac/wplayer.dmg">');
      document.write('here</a><br><small>(and copy it in your /Library/');
      document.writeln('Internet Plug-Ins folder and restart Safari)</small></p>');
    } else if (navigator.userAgent.indexOf("MSIE")!=-1) {
      document.write('<p>Download it from <a href=');
      document.write('"http://www.cyberbotics.com/wplayer/windows/wplayer_ie.exe">');
      document.write('here</a><br><small>(execute it and click on the Refresh button of Internet Explorer');
      document.writeln(')</small></p>');
    } else if (navigator.userAgent.indexOf("Firefox")!=-1 ||
               navigator.userAgent.indexOf("Mozilla")!=-1) {
      document.writeln('<p>click on the install button to get it<br>');
      document.writeln('<small>(you may need to allow this web site to ');
      document.writeln('install software)</small><br>');
      document.write('<a href="http://www.cyberbotics.com/wplayer/troubleshooting.html">');
      document.writeln('troubleshooting</a></p>');
      document.writeln('<p><input type=button value="install" onclick="installWebotsPlayerXPI();"></p>');
    } else {
      document.write('<p>Webots Player does not work on your browser configuration. ');
      document.writeln('It works only on the following configurations:</p>');
      document.writeln('<ul>');
      document.writeln('<li>Firefox or Mozilla under Windows, Linux or Mac OS X</li>');
      document.writeln('<li>Safari under Mac OS X</li>');
      document.writeln('<li>Internet Explorer under Windows</li>');
      document.writeln('</ul>');
    }
    document.writeln('</td></tr></table>');
  } else {
    if (navigator.userAgent.indexOf("MSIE")!=-1) {
      document.write('<object classid="CLSID:7929103C-B5B0-4EFD-95D9-5C0E262D6B0A" ');
      document.writeln('width="'+width+'" height="'+height+'">');
      document.write('<param name="url" ')
      document.writeln('value="'+url+'">');
      document.writeln('<param name="refresh" value="64">'); // useful?
      document.writeln('</object>');
    } else {
      document.write('<embed type="x-world/x-wplayer" width="'+width+'" height="'+height+'" ');
      document.write('url="'+url+'" ');
      document.write('pluginurl="http://www.cyberbotics.com/wplayer/'+getOperatingSystem());
      document.write('/wplayer.xpi">');
      document.writeln('</embed>');
    }
  }
}
