/*
 * Mozilla Japan グローバルスクリプト [mj-global.js] 改
 * Copyright (C) Mozilla Japan
 * 最終更新日: 2010/07/28
 * 依存ライブラリ: jQuery
 */

var mj =
{
  /*
   * 初期化関数の呼び出しを設定
   */
  onbeforeload: function()
  {
    try {
      $(document).ready(function()
      {
        mj.init();
      });
    } catch (e) {
      // jQuery が使えない環境への対応
      window.onload = function()
      {
        mj.init();
      }
    }
    
    delete this.onbeforeload;
  },
  
  /*
   * 初期化
   */
  init: function()
  {
    this.analytics.init();

    delete this.init;
  },
  
  /*
   * アクセス解析関連
   * 複数ツールへの対応を考慮する
   */
  analytics:
  {
    // サイト固有の設定
    path: "",
    ga_installed: true,
    ga_property_id: "UA-6459738-9",
    urchin_installed: false,
    urchin_img_path: "",
    cookie_domain: "",
    track_outbound_link: true,
    track_file_download: true,
    
    init: function()
    {
      // Google Analytics + Urchin の設定
      this.ga_installed = (typeof _gat == "object");
      if (this.ga_installed) {
        this.ga = _gat._getTracker(this.ga_property_id);
        if (this.cookie_domain != "") {
          this.ga._setDomainName(this.cookie_domain);
        }
        if (this.urchin_installed) {
          this.ga._setLocalGifPath(this.urchin_img_path);
          this.ga._setLocalRemoteServerMode();
        }
      }
      
      // 現在のページを記録
      this.log();
      
      // リンクのトラッキングを設定
      try {
        $("a[href]").click(function(event){
          mj.analytics.track_link(this);
        });
      } catch (e) {}
      
      delete this.init;
    },
    
    track_link: function(target)
    {
      if (target.hostname == "" || target.hostname == undefined) {
        return;
      }
      
      // リンクにトラッキングコードが設定済みの場合はそれを優先する
      if (target.attributes.onclick && 
          target.attributes.onclick.value.indexOf("mj.analytics.log") != -1) {
        return;
      }
      
      var internal_link = (target.hostname == document.domain);
      var path = target.pathname;
      
      // ブラウザによっては pathname の先頭にスラッシュが付かない
      if (path.substr(0, 1) != "/") {
        path = "/" + path;
      }
      
      // 外部リンクのトラッキング
      if (this.track_outbound_link && !internal_link) {
        this.log("/oblink/" + target.href);
      }
      
      var re = new RegExp("\.(png|gif|jpg|jpeg|tif|tiff|ai|eps|psd|svg|pdf|doc|docx|xls|xlsx|ppt|pptx|zip|xpi|jar|exe)$", "i");
      var file_link = (path != undefined && re.test(path));
      
      // ファイルダウンロードのトラッキング
      if (this.track_file_download && internal_link && file_link) {
        this.log(path);
      }
    },
    
    log: function()
    {
      // パスの設定
      var url;
      if (arguments.length == 1) {
        // クリックイベントなどによるカスタム URL
        url = arguments[0];
      } else if (this.path != "") {
        // ページ内で上書きされた URL (例: ダウンロードページ)
        url = this.path;
      } else {
        // 通常のページビュー
        url = location.pathname + location.search;
      }
      
      // Google Analytics の文字化け問題を回避 (unescape の代わりに decodeURI を使う)
      if (typeof decodeURI == "function") {
        url = decodeURI(url);
      }
      
      // Google Analytics & Urchin
      if (this.ga_installed) {
        this.ga._trackPageview(url);
      }
    }
  }
};

mj.onbeforeload();

