//date updated: 2008/08/01

//<xmp>
/* msgr_detect.js
 * a javascript library to figure out things about a locally installed messenger client
 */

function Messenger() {
  this.startDetect();
  return null;
}
Messenger.prototype.installed = 0;
Messenger.prototype.cobrand = '';
Messenger.prototype.intl = 'us';
Messenger.prototype.version = "4";
Messenger.prototype.platform = "w32";
Messenger.prototype.avn = false;
Messenger.prototype.post = [];
Messenger.prototype.detect = function() {
  // loop through post process
  for (var i=0;i<this.post.length;i++) {
    if (this[this.post[i]]())
      break;
  }
  this.post=[];
  return this;
}
Messenger.prototype.startDetect = function () {
  var ok = this.detectCookie();
  if (!ok) ok = this.detectBing();
  if (!ok) ok = this.detectRafi();
  if (!ok) ok = this.detectMime();
  /* if you don't know by now, you'll never ever know. */
}
Messenger.prototype.getBrowserVersion = function(str, name)
{
  // Parse browser name string "<name>/<version>" for version
  var i = str.toLowerCase().indexOf(name);
  if (i == -1) return;
  i += name.length + 1;
  return (str.substring(i));
}
Messenger.prototype.getPluginVersion = function(description)
{
  // Parse plug-in description "<descripton-text> version <version>" for version
  var versionText = "version ";
  var n = description.indexOf(versionText);
  if (n == -1) return '';
  return description.substring(n + versionText.length);
}
// Return true if version is valid: "#.#.#.#"
Messenger.prototype.isValidVersion = function(version)
{
  if (version.length == 0) return false;
  var verSplit = version.split('.', 4);
  if (verSplit.length == 0) return false;
  for (i=0; i<4; i++)
  {
    if (isNaN(verSplit[i]))
      return false;
  }
  return true;
}
// Compares versions: ("1.0.0.5", "1.0.0.8") returns -3
Messenger.prototype.compareVersion = function(ver1, ver2)
{
  var retVal = 0;
  var verSplit1 = ver1.split('.');
  var verSplit2 = ver2.split('.');

  for (i=0; i<4; i++)
  {
    var delta = parseInt(verSplit1[i]) - parseInt(verSplit2[i]);
    if (delta != 0)
    {
      retVal = delta;
      break;
    }
  }
  return retVal;
}
// detectBrowser(getWhat)
// getWhat: 0=get name; 1=get version;
Messenger.prototype.detectBrowser = function(getWhat)
{
  var ua = navigator.userAgent.toLowerCase();

  if (ua.indexOf('opera')!=-1)
  {
    return (getWhat == 0) ? 'opera' : '';
  }
  else if (ua.indexOf('msie')!=-1)
  {
    return (getWhat == 0) ? 'ie' : '';
  }
  else if (ua.indexOf('safari')!=-1)
  {
    return (getWhat == 0) ? 'safari' : '';
  }
  else if (ua.indexOf('flock')!= -1)
  {
    return (getWhat == 0) ? 'flock' : '';
  }
  else if (ua.indexOf('firefox')!= -1)
  {
    return (getWhat == 0) ? 'firefox' : this.getBrowserVersion(ua, 'firefox');
  }
  else if (ua.indexOf('gecko')!= -1)
  {
    return (getWhat == 0) ? 'gecko' : '';
  }
  else
  {
    return false;
  }
}
// direct document.writeln will wipe out your existing content
// use addX instead to add the activeX object tag.
Messenger.prototype.addX = function(str) {
  var divName = '_ymsgr_detector_obj_' + new Date().getTime();

  while (document.getElementById(divName))
  {
    divName = '_ymsgr_detector_obj_' + new Date().getTime();
  }

  var newTarget = document.createElement("div");
  newTarget.id = divName;
  document.body.appendChild(newTarget);

  var target = document.getElementById(divName);
  target.innerHTML = str;
}
Messenger.prototype.detectMime = function() {
  if (navigator.mimeTypes && navigator.mimeTypes.length) {
    for (var i=0; i<navigator.mimeTypes.length; i++) {
      if (navigator.mimeTypes[i].suffixes.indexOf("yps") > -1) {
         this.installed = 1;
         this.version = "5.04";
         this.platform = "w32";
         return true;
      }
    }
  }
  return false;
}
Messenger.prototype.detectBing = function() {
  if (this.detectBrowser(0) == 'ie')
  {
    this.addX('<object id="Ymsgr02" classid="CLSID:DA4F543C-C8A9-4E88-9A79-548CBB46F18F" hidden="true" width="0" height="0" codebase="tkzwtyzwzi:qwerty"></object>');
    this.post[this.post.length] = "postDetectBing";
  }
}
Messenger.prototype.postDetectBing = function() {
  var v;
  if (document.all) {
    v = document.all.Ymsgr02;
  } else { 
    // gecko w/ activeX patch
    v = document.getElementById("Ymsgr02");
  }
  if (!v) return false;
  if (typeof v.installed == "undefined") return false;
  if (typeof v.version == "undefined") return false;
  this.installed = v.installed();
  var versionstring = v.version(1);
  if (!versionstring || typeof versionstring == "undefined") return false;
  this.version = versionstring.split(".").join(",");
  this.cobrand = v.cobrand();
  this.intl = v.intl();
  return true;
}
Messenger.prototype.detectRafi = function() {
  if (!document.getElementById) return false;
  var p = navigator.plugins["Yahoo Application State Plugin"];
  if (!p) return false;
  
  var pluginVersion = this.getPluginVersion(p.description);
  if (this.detectBrowser(0) == 'firefox')
  {
    var browserVersion = this.detectBrowser(1);
    var va = browserVersion.split('.', 4);
    if (va.length == 0 || va[0].length == 0) return false;

    var major = parseInt(va[0]);
    if (!isNaN(major))
    {
      if (!this.isValidVersion(pluginVersion) ||
          this.compareVersion(pluginVersion, '1.0.0.7') < 0)
      {
        // Pretend Y! Msgr is installed
        this.installed = 1;
        this.version = '8.1.0.421';
        this.platform = 'w32';
        this.avn = true;
        return false;
      }
    }
  }
  this.addX("<object id='Ymsgr03' type='application/x-vnd.yahoo.applicationState' hidden='true'></object>");
  this.post[this.post.length] = "postDetectRafi";
  return false;
}
Messenger.prototype.postDetectRafi = function() {
  var v = document.getElementById("Ymsgr03");
  if (!v) return false;
  if (typeof v.applicationInfo != "function") return false;
  var m = v.applicationInfo("msgr");
  if (!m) return false;
  this.installed = m.installed();
  var ver = m.version();
  this.version = ver.major+","+ver.minor+","+ver.hiBuild+","+ver.loBuild;
  this.cobrand = m.cobrand();
  this.intl = m.internationalCode();
  return true;
}
Messenger.prototype.detectCookie = function() {
  // check C cookie. XXX
  return false;
}

