/** -*- Mode:js;tab-width:2;indent-tabs-mode:nil;coding:utf-8 -*- */
/**
 * UserAgent.js
 *
 * ユーザエージェントを判別するクラス
 *
 * @id: $Id
 * @author: norio.minato
 */

function UserAgent()
{
  var is_analyzed = false;

  /**
   * ユーザエージェントに関する情報を保   */
  this.info = function ()
  {
    this.ua              = navigator.userAgent;
    this.ua_name         = false;  // UA
    this.ua_version      = false;  // UAのバージ
    this.os_name         = false;  // OS
    this.os_version      = false;  // OSのバージ
    this.appName         = navigator.appName;
    this.appCodeName     = navigator.appCodeName;
    this.appVersion      = navigator.appVersion;
    this.appMinorVersion = navigator.appMinorVersion;
    this.product         = navigator.product;
    this.productSub      = navigator.productSub;
    this.vendor          = navigator.vendor;
    this.vendorSub       = navigator.vendorSub;
    this.userAgent       = (navigator.userAgent) ? navigator.userAgent : false;
    this.userProfile     = navigator.userProfile;
    this.language        = navigator.language;
    this.userLanguage    = navigator.userLanguage;
    this.browserLanguage = navigator.browserLanguage;
    this.systemLanguage  = navigator.systemLanguage;
    this.platform        = navigator.platform;
    this.cpuClass        = navigator.cpuClass;
    this.oscpu           = navigator.oscpu;
    this.onLine          = navigator.onLine;
    this.cookieEnabled   = navigator.cookieEnabled;
    this.javaEnabled     = navigator.javaEnabled();
    this.plugins         = navigator.plugins;
    this.mimeTypes       = navigator.mimeTypes;
    this.opsProfile      = navigator.opsProfile;
    this.taintEnabled    = navigator.taintEnabled();
  }

  /**
   * ユーザエージェントに関する情報を連想配列で返   */
  this.getList = function ()
  {
    if (_is_analyzed == false) this.identifier();

    var list = {'ua':              this.info.ua,
                'ua_name':         this.info.ua_name,
                'ua_version':      this.info.ua_version,
                'os_name':         this.info.os_name,
                'os_version':      this.info.os_version,
                'appName':         this.info.appName,
                'appCodeName':     this.info.appCodeName,
                'appVersion':      this.info.appVersion,
                'appMinorVersion': this.info.appMinorVersion,
                'product':         this.info.product,
                'productSub':      this.info.productSub,
                'vendor':          this.info.vendor,
                'vendorSub':       this.info.vendorSub,
                'userAgent':       this.info.userAgent,
                'userProfile':     this.info.userProfile,
                'language':        this.info.language,
                'userLanguage':    this.info.userLanguage,
                'browserLanguage': this.info.browserLanguage,
                'systemLanguage':  this.info.systemLanguage,
                'platform':        this.info.platform,
                'cpuClass':        this.info.cpuClass,
                'oscpu':           this.info.oscpu,
                'onLine':          this.info.onLine,
                'cookieEnabled':   this.info.cookieEnabled,
                'javaEnabled':     this.info.javaEnabled,
                'plugins':         this.info.plugins,
                'mimeTypes':       this.info.mimeTypes,
                'opsProfile':      this.info.opsProfile,
                'taintEnabled':    this.info.taintEnabled};
    return list;
  }

  /**
   * ユーザエージェントの解析実行
   */
  this.identifier = function (ua_data)
  {
    if (typeof(navigator) != "object" || !navigator.userAgent)
      return false;

    var ua = ua_data
    if (! ua) ua = navigator.userAgent;
    this.info.ua = ua;

    //-----------------------------------------------------------------
    // UAの    //-----------------------------------------------------------------
    // Sleipnir
    if (ua.indexOf('Sleipnir') >= 0) {
      var matched = ua.match(new RegExp('Sleipnir/([^ ]+)'));
      this.info.ua_version = matched[1];
      this.info.ua_name    = 'sleipnir';
    }
    // Opera
    else if (ua.indexOf('Opera') >=0) {
      var matched = ua.match(new RegExp('Opera[ /]([^ ]+)'));
      this.info.ua_version = matched[1];
      this.info.ua_name    = 'opera';
    }
    // IE
    else if (ua.indexOf('MSIE') >= 0) {
      var matched = ua.match(new RegExp('MSIE ([^;]+);'));
      this.info.ua_version = matched[1];
      this.info.ua_name = 'ie';
    }
    // Firefox
    else if (ua.indexOf('Firefox') >=0) {
      var matched = ua.match(new RegExp('Firefox/([^ ]+)'));
      this.info.ua_version = matched[1];
      this.info.ua_name = 'firefox';
    }
    // Shiira
    else if (ua.indexOf('Shiira') >= 0) {
      var matched = ua.match(new RegExp('Shiira/([^ ]+)'));
      this.info.ua_version = matched[1];
      this.info.ua_name    = 'shiira';
    }
    // Safari
    else if (ua.indexOf('Safari') >= 0) {
      var matched = ua.match(new RegExp('Safari/([^ ]+)'));
      this.info.ua_version = matched[1];
      this.info.ua_name    = 'safari';
    }
    // Konqueror
    else if (ua.indexOf('Konqueror') >= 0) {
      var matched = ua.match(new RegExp('Konqueror/([^;]+)'));
      this.info.ua_version = matched[1];
      this.info.ua_name    = 'konqueror';
    }
    // Dreampassport
    else if (ua.indexOf('DreamPassport') >= 0) {
      var matched = ua.match(new RegExp('DreamPassport/([^)]+)'));
      this.info.ua_version = matched[1];
      this.info.ua_name    = 'dreampassport';
    }
    // iCab
    else if (ua.indexOf('iCab') >= 0) {
      var matched = ua.match(new RegExp('iCab( J/| )([^ ;]+)'));
      this.info.ua_version = matched[2];
      this.info.ua_name    = 'icab';
    }
    // Netscape
    else if (ua.indexOf('Netscape') >= 0) {
      var matched = ua.match(new RegExp('Netscape6?/([^ ]+)'));
      this.info.ua_version = matched[1];
      this.info.ua_name    = 'netscape';
    }
    // Mozilla
    else if (ua.indexOf('Mozilla') >= 0) {
      var matched = ua.match(new RegExp('Mozilla/([^ ]+)'));
      this.info.ua_version = matched[1];
      this.info.ua_name    = 'mozilla';
    }

    //-----------------------------------------------------------------
    // OSを取得
    //-----------------------------------------------------------------
    // Windows
    if (ua.indexOf('Windows') >= 0) {
      var matched = ua.match(new RegExp('Windows ([^;)]+)'));
      this.info.os_version = matched[1];
      this.info.os_name    = 'win';
    }
    else if ((new RegExp('Win([0-9]+|NT)')).test(ua)) {
      var matched = ua.match(new RegExp('Win([0-9]+|NT)'));
      this.info.os_version = matched[1];
      this.info.os_name    = 'win';

      // win32はひとつのバージョンと      if (matched[1] == '32')
        this.info.os_version = 'win32';
    }
    // MacOS
    else if (ua.indexOf('Mac_PowerPC') >= 0) {
      this.info.os_version = 'PPC';
      this.info.os_name    = 'mac';
    }
    else if (ua.indexOf('Mac') >= 0) {
      var matched = ua.match(new RegExp('Macintosh; [UIN]; ([^;)]+)'));
      this.info.os_version = matched[1];
      this.info.os_name    = 'mac';
    }
    // Linux
    else if (ua.indexOf('Linux') >= 0) {
      var matched = ua.match(new RegExp('Linux( |;;)([^;)]+)'));
      this.info.os_version = matched[2];
      this.info.os_name    = 'linux';
    }
    // FreeBSD
    else if (ua.indexOf('BSD') >= 0) {
      var matched = ua.match(new RegExp('(Free|Open)BSD ([^;)]+)'));
      this.info.os_version = matched[2];
      this.info.os_name    = 'bsd';
    }
    // SunOS
    else if (ua.indexOf('SunOS') >= 0) {
      var matched = ua.match(new RegExp('SunOS ([^;)]+)'));
      this.info.os_version = matched[1];
      this.info.os_name    = 'sunos';
    }
    _is_analyzed = true;
  }

  this.identifier();
}


