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

var mj =
{
  /*
   * 初期化
   */
  init: function()
  {
    $(document).ready(function()
    {
      mj.analytics.init();
      
      if (navigator.userAgent.indexOf("MSIE") != -1) {
        mj.gnav.init();
      } else {
        delete mj.gnav.init;
        delete mj.download.open_sub_window;
      }
    });
    
    delete this.init;
  },
  
  /*
   * メインメニュー関連
   */
  gnav:
  {
    /*
     * IE のみドロップダウンを設定。他のブラウザは CSS で機能するため、この関数は不要
     */
    init: function()
    {
      var items = $("div#gnav li[id^=gnav]");
      items.mouseover(function(event){
        $(this).addClass("hover");
      });
      items.mouseout(function(event){
        $(this).removeClass("hover");
      });
      
      delete this.init;
    }
  },
  
  /*
   * ダウンロード関連
   */
  download:
  {
    /*
     * IE のみ、ダウンロード開始のためにサブウィンドウを開く
     */
    open_sub_window: function(product, version)
    {
      var link = "http://download.mozilla.org/?product=" + product + "-" + version + "&os=win&lang=ja";
      var options = "toolbar=0,location=no,directories=0,status=0,scrollbars=0,resizeable=0,width=1,height=1,top=0,left=0";
      window.open(link, "download_window", options);
      window.focus();
    }
  },
  
  /*
   * アクセス解析関連
   * 複数ツールへの対応を考慮する
   */
  analytics:
  {
    // サイト固有の設定
    ga_installed: true,
    ga_property_id: "UA-6459738-7",
    urchin_installed: false,
    urchin_img_path: "",
    cookie_domain: ".discovershiretoko.org",
    track_outbound_link_enabled: true,
    track_outbound_link_prefix: "/oblink/",
    track_file_download_enabled: 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();
        }
      }
      
      // リンクのトラッキングを設定
      $("a[href]").click(function(event){
        mj.analytics.track_link(this);
      });
      
      // 現在のページを記録
      this.log();
      
      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_enabled && !internal_link) {
        this.log(this.track_outbound_link_prefix + 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_enabled && internal_link && file_link) {
        this.log(path);
      }
    },
    
    log: function()
    {
      // パスの設定
      var url;
      if (arguments.length == 1) {
        // クリックイベントなどによるカスタム URL
        url = arguments[0];
      } else if (this.path != undefined) {
        // ページ内で上書きされた URL (例: ダウンロードページ)
        url = this.path;
      } else {
        // 通常のページビュー
        url = location.pathname + location.search;
      }
      
      // Google Analytics の文字化け問題を回避 (unescape が使われているのが原因)
      if (typeof decodeURI == "function") {
        url = decodeURI(url);
      }
      
      // 記録
      if (this.ga_installed) {
        this.ga._trackPageview(url);
      }
    }
  }
};

mj.init();
