/*
Copyright (2003) Tobias Ouwejan, Wolters-Noordhoff B.V.

Browser LIB

- Flash detection implemented
- Svg detection implemented

18-04-07 Jaap Dijkma
- update volgens http://www.webreference.com/tools/browser/javascript.html
- added ie7, gecko
- version and agent in lowercase

*/

// configuration
var svgRequiredVersion = 3;
var flashRequiredVersion = 9;
var mathPlayerRequiredVersion = 1;
var mediaPlayerRequiredVersion = 1;

// System globals
var svg1Installed = false;          // boolean. true if svg 1 is installed
var svg2Installed = false;          // boolean. true if svg 2 is installed
var svg3Installed = false;          // boolean. true if svg 3 is installed

var flash2Installed = false;        // boolean. true if flash 2 is installed
var flash3Installed = false;        // boolean. true if flash 3 is installed
var flash4Installed = false;        // boolean. true if flash 4 is installed
var flash5Installed = false;        // boolean. true if flash 5 is installed
var flash6Installed = false;        // boolean. true if flash 6 is installed
var flash7Installed = false;        // boolean. true if flash 7 is installed
var flash8Installed = false;        // boolean. true if flash 8 is installed
var flash9Installed = false;        // boolean. true if flash 9 is installed

var mathPlayer1Installed = false;   // boolean. true if MathPlayer 1 is installed

var mediaPlayer1Installed = false;  // boolean. true if MediaPlayer is installed

// var jsVersion = 1.0;                // the version of javascript supported


// constants
var SVG_player = 'svg';
var FLASH_PLAYER = 'flash';
var MATH_PLAYER = 'mathPlayer';
var MEDIA_PLAYER = 'mediaPlayer';

// oBrowser object definition
function oBrowser(avoidPluginCheck){
  this.ver = navigator.appVersion.toLowerCase();
  this.agent = navigator.userAgent.toLowerCase();
  var is_minor = parseFloat(this.ver);
  var is_major = parseInt(is_minor);
  
  this.dom=document.getElementById?1:0
  this.opera = (this.agent.indexOf("opera") != -1);
  this.mac = (this.agent.indexOf("mac") != -1);
  
  var iePos  = this.ver.indexOf('msie');
  if (iePos !=-1) {
    if(this.mac) {
      var iePos = this.agent.indexOf('msie');
      is_minor = parseFloat(this.agent.substring(iePos+5,this.agent.indexOf(';',iePos)));
    } else 
	  is_minor = parseFloat(this.ver.substring(iePos+5,this.ver.indexOf(';',iePos)));
    is_major = parseInt(is_minor);
  }
	
  this.saf = ((this.agent.indexOf('safari')!=-1)&&(this.agent.indexOf('mac')!=-1))?true:false;
  this.gecko = ((!this.saf)&&(navigator.product)&&(navigator.product.toLowerCase()=="gecko"))?true:false;
  this.fir = (this.gecko) && ((navigator.vendor=="Firefox")||(this.agent.indexOf('firefox')!=-1));
  
  this.ie = (iePos!=-1) && (!this.opera);
  this.ie4 = (this.ie && is_major == 4);
  this.ie5 = (this.ie && is_major == 5);
  this.ie6 = (this.ie && is_major == 6);
  this.ie7 = (this.ie && is_major == 7);

  this.aol = (this.agent.indexOf("aol") != -1);
  this.webtv = (this.agent.indexOf("webtv") != -1); // msntv (formerly webtv)

  this.win = ( (this.agent.indexOf("win")!=-1) || (this.agent.indexOf("16bit")!=-1) );
  this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
  this.ns4=(document.layers && !this.dom)?1:0;
  this.bw=(this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera5);
  // plugins
  if (!avoidPluginCheck){
    this.plugins = new oPlugins();
  }
  return this;
}
// oBrowser prototype methods
// no methods defined yet


// oPlugins
function oPlugins(){
  this.svg = new oPlugin(SVG_player, svgRequiredVersion);
  this.flash = new oPlugin(FLASH_PLAYER, flashRequiredVersion);
  this.mathPlayer = new oPlugin(MATH_PLAYER, mathPlayerRequiredVersion);
  this.mediaPlayer = new oPlugin(MEDIA_PLAYER, mediaPlayerRequiredVersion);
  return this;
}
// oPlugin prototype methods
  // no methods defined yet


// oPlugin object definition
function oPlugin(pl, rv){
  this.name = pl;
  this.requiredVersion = rv;
  this.version;
  // methods
  this.checkPlugin();
  return this;
}


// oPlugin prototype methods
oPlugin.prototype.checkPlugin = function(){
  // constructor
  switch (this.name) {
   case SVG_player :
      this.checkSvg();
      return;
   case FLASH_PLAYER :
      this.checkFlash();
      return;
   case MATH_PLAYER :
      this.checkMathPlayer();
      return;
   case MEDIA_PLAYER :
      this.checkMediaPlayer();
      return;
   default :
      return;
  }
};
oPlugin.prototype.available = function(){
  return (this.requiredVersion >= this.version);
};
oPlugin.prototype.checkSvg = checkSvg;
oPlugin.prototype.checkFlash = detectFlash;
oPlugin.prototype.checkMathPlayer = detectMathPlayer;
oPlugin.prototype.checkMediaPlayer = detectMediaPlayer;


// function checkSvg()
function checkSvg(){
  //start SVG detection
  if (navigator.plugins){
    if (navigator.plugins["SVG Plugin"] || navigator.plugins["Adobe SVG Viewer Plugin"]){

      for(n=0;n<navigator.plugins.length;n++){
        if (navigator.plugins[n].description.indexOf("SVG") > -1 ) {
          svgDescription = navigator.plugins[n].description;
        }
      }

      if (svgDescription == "Adobe SVG Viewer for Macintosh"){
        version = 1;
      }else{
        version = parseInt(svgDescription.charAt(svgDescription.indexOf(".")-1));
      }
      svg1Installed = version == 1;
      svg2Installed = version == 2;
      svg3Installed = version == 3;
    }
  }
  for (var i = 1; i <= this.requiredVersion; i++) {
    if (eval("svg" + i + "Installed") == true) this.version = i;
  }
}


function detectFlash() {
  //start Flash detection
  if (navigator.plugins) {
    // check for flash 2 or flash 3+.
    if (navigator.plugins["Shockwave Flash 2.0"]
        || navigator.plugins["Shockwave Flash"]) {

      // Some version of Flash was found. Time to figure out which.

      // Set convenient references to flash 2 and the plugin description.
      var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;

      // A flash plugin-description looks like this: Shockwave Flash 4.0 r5
      // We can get the major version by grabbing the character before the period
      // note that we don't bother with minor version detection.
      // Do that in your movie with $version or getVersion().
      var flashVersion = parseInt(flashDescription.substring(16));

      // We found the version, now set appropriate version flags. Make sure
      // to use >= on the highest version so we don't prevent future version
      // users from entering the site.
      flash2Installed = flashVersion == 2;
      flash3Installed = flashVersion == 3;
      flash4Installed = flashVersion == 4;
      flash5Installed = flashVersion == 5;
      flash6Installed = flashVersion == 6;
      flash7Installed = flashVersion == 7;
      flash8Installed = flashVersion == 8;
      flash9Installed = flashVersion >= 9;
    }
  }

  // Loop through all versions we're checking, and
  // set actualVersion to highest detected version.
  for (var i = 2; i <= this.requiredVersion; i++) {
    if (eval("flash" + i + "Installed") == true) this.version = i;
  }

  // If we're on msntv (formerly webtv), the version supported is 4
  // (as of January 1, 2004)
  if(this.webtv) this.version = 4;

  return this.version;
}


// function checkMathPlayer()
function detectMathPlayer(){
  //start MathPlayer detection
  if (isMPInstalled()){
    version = 1;
  }else{
    version = 0;
  }
  mathPlayer1Installed = version == 1;
  for (var i = 1; i <= this.requiredVersion; i++) {
    if (eval("mathPlayer" + i + "Installed") == true) this.version = i;
  }
}

function isMPInstalled()
{
  try{
    var oMP = new ActiveXObject("MathPlayer.Factory.1");
    return true;
  }
  catch(e) {
  return false;
  }
}


// function checkMediaPlayer()
function detectMediaPlayer(){
  //start MediaPlayer detection
  if (navigator.plugins){
//    if (navigator.plugins["Windows Media"]){

      for(n=0;n<navigator.plugins.length;n++){
        //alert(navigator.plugins[n].description);
        if (navigator.plugins[n].description.indexOf("Windows Media") > -1 ) {
          mediaPlayerDescription = navigator.plugins[n].description;
        }
      }
      mediaPlayer1Installed = version == 1;
      mediaPlayer2Installed = version == 2;
      mediaPlayer3Installed = version == 3;
//    }
  }
  for (var i = 1; i <= this.requiredVersion; i++) {
    if (eval("mediaPlayer" + i + "Installed") == true) this.version = i;
  }
}




// create the browser object
var browser = new oBrowser(true);

// Write vbscript detection on ie win.
// IE on Windows doesn't support regular
// JavaScript plugins array detection.
if(browser.ie && browser.win && !browser.aol){
  document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
  document.write('on error resume next \n');
  //flash
  document.write('flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
  document.write('flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
  document.write('flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
  document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');
  document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');
  document.write('flash7Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7"))) \n');
  document.write('flash8Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.8"))) \n');
  document.write('flash9Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.9"))) \n');
  //svg
  document.write('svg1Installed = (IsObject(CreateObject("Adobe.SVGCtl.1"))) \n');
  document.write('svg2Installed = (IsObject(CreateObject("Adobe.SVGCtl.2"))) \n');
  document.write('svg3Installed = (IsObject(CreateObject("Adobe.SVGCtl.3"))) \n');
  //mediaplayer
  document.write('mediaplayer1Installed = (IsObject(CreateObject("MediaPlayer.MediaPlayer.1"))) \n');

    document.write('<\/SCR' + 'IPT\> \n'); // break up end tag so it doesn't end our script
}



