// Filename :   mt.js
// Author :     MIZUBAYASHI Kosuke <miz884@yahoo.co.jp>
// Created :    2003-10-14
// Updated :    Sun Oct 10 01:39:55 JST 2004
// Description: MovableType 用 js
// //////////////////////////////////////////////////////////////
//
// (C) Copyright 2003-2004  MIZUBAYASHI Kosuke <miz@mizba.net>
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2 as
//  published by the Free Software Foundation.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You can get GNU General Public License from this the URI below.
//  http://www.gnu.org/licenses/gpl.txt
//
//
//  本プログラムはフリーソフトウェアです．GNU General Public License
//  version 2 の定める条件を満たす限りにおいて，再頒布，改変することが
//  できます．
//
//  本プログラムは有用であることを願って頒布しますが，一切の保証はありません．
//  商用として成り立ちうる可能性，特定目的への適合性は，非明示的なものを含めて
//  一切存在しません．
//  詳しくは GNU General Public License をご覧下さい．
//
//  GNU General Public License 全文は以下の URI から入手できます．
//  http://www.gnu.org/licenses/gpl.txt
//
// ///////////////////////////////////////////////////////////////////
// ChangeLog:
// 2004-05-09 04:09 MIZUBAYASHI Kosuke <miz@mizba.net>
//   ライセンスを適用 (GPL v2)
// ///////////////////////////////////////////////////////////////////


// ///////////////////////////////////////////////////////////////////
// BEGIN:
// Updated :     2003-10-14 2005年  6月  4日 土曜日 13:00:13 JST
// Description : ページロード直後の処理を記述

function onLoadFunction () {
  // showNewIcons ();
}

// END:
// ///////////////////////////////////////////////////////////////////


// ///////////////////////////////////////////////////////////////////
// BEGIN:
// Updated :     2003-10-14
// Description : 和暦を表示するための関数群です

/** 月の和名を配列に格納 */
var jmons = new Array ( "睦月","如月","弥生","卯月","皐月","水無月","文月","葉月","長月","神無月","霜月","師走" );

/**
 * 月を和暦で表示
 */
function writeJMon ( mon ) {
  document.open ();
  document.write ( jmons [ mon - 1 ] );
  document.close ();
}

/**
 * 年，月を和暦で表示
 */
function writeJYMon ( year , mon ) {
  document.open ();
  document.write ( year + "年" + jmons [ mon - 1 ] );
  document.close ();
}

// END:
// ///////////////////////////////////////////////////////////////////



// ///////////////////////////////////////////////////////////////////
// BEGIN:
// Updated :     2003-10-14
// Description : テーブルの行の偶奇に合わせてスタイルを切り替えるための関数群

/** 複数の表から呼ばれても動作するように，id ごとに偶奇を格納 */
var trRow = new Array ();

/**
 * 偶奇にあわせてスタイルを切り替え
 *
 * writeTrOE_init でカウンタを初期化し，
 * writeTrOE で <tr> タグを出力します．
 * それぞれの関数に渡す id によって，
 * 複数のテーブルを識別します．
 */
function writeTrOE_init ( id ) {
  trRow [ id ] = 0;
}
function writeTrOE ( id ) {
  ++ trRow [ id ];
  document.open ();
  document.write ( "<tr" );
  if ( trRow [ id ] % 2 == 0 ) {
    document.write ( " class=\"even\" " );
  } else {
    document.write ( " class=\"odd\" " );
  }
  document.write ( ">" );
  document.close ();
}

// END:
// ///////////////////////////////////////////////////////////////////



// ///////////////////////////////////////////////////////////////////
// BEGIN:
// Updated :     2003-10-25
// Description : Cookie を用いてアクセスユーザの名前を強調表示するための関数群

/**
 * cookie 内のユーザ名をチェックし，
 * 引数と同じであれば強調表示する．
 *
 * @param p_auth 投稿者の名前
 * @param p_prefix 投稿者の前に表示したい文字列
 * @param p_suffix 投稿者の後に表示したい文字列
 */
function authorEmph ( p_auth , p_prefix , p_suffix ) {
  var v_prefix = p_prefix;
  if ( ! v_prefix ) {
    v_prefix = "";
  }
  var v_suffix = p_suffix;
  if ( ! v_suffix ) {
    v_suffix = "";
  }
  var v_html = v_prefix;
  var v_cookie_miz_auth = getCookie ( "miz_auth" );
  if ( p_auth && p_auth == v_cookie_miz_auth ) {
    v_html += "<span class=\"author_self\">";
    v_html += p_auth;
    v_html += "</span>";
  } else {
    v_html += p_auth;
  }
  v_html += v_suffix;
  //
  document.open ();
  document.write ( v_html );
  document.close ();
}

// END:
// ///////////////////////////////////////////////////////////////////



// ///////////////////////////////////////////////////////////////////
// BEGIN:
// Updated :     2003-10-25
// Description : エントリの最終時刻に基づいて表示を切り替えるための関数群

/** Cookie 名 */
var c_LAST_ACCESS_TIME = "miz_last_access";
/** 前回のアクセス時刻を格納する変数．rememberAccessTime が値を設定します． */
var g_prevAccessTime = null;
/** entryId とアクセス時刻の対応を保持します．*/
// var g_accessTimeMap = new Map ( getCookie ( c_LAST_ACCESS_TIME ) );


/**
 * 最終アクセス時刻を記憶させます．
 *
 * @param p_entryId エントリ ID
 * @param p_blogURL MT の URL．<$MTBlogURL$> で取得可能です．
 * @param p_blogHost MT のホスト名．<$MTBlogHost$> で取得可能です．
 */
function rememberAccessTime ( p_entryId , p_blogURL , p_blogHost ) {
  if ( ! p_entryId ) {
    return;
  }
  // 前回のアクセス時刻を保存しておきます
  g_prevAccessTime = getLastAccessTime ( p_entryId );
  // 改めて今回のアクセス時刻を保存します
  var v_path = null;
  if ( p_blogURL && p_blogHost ) {
    v_path = p_blogURL.substring ( p_blogURL.indexOf ( p_blogHost ) + p_blogHost.length );
  } else {
    v_path = "/";
  }
  var now = new Date ();
  fixDate ( now );
  var value = now.getTime ();
  var expire = new Date ();
  expire.setTime ( now.getTime () + 10* 365 * 24 * 60 * 60 * 1000 ); // 10年
  // g_accessTimeMap.put ( "" + p_entryId , "" + value );
  setCookie ( c_LAST_ACCESS_TIME , g_accessTimeMap.toString () , expire , v_path );
}


/**
 * 最終アクセス時刻を取得する関数です.
 *
 * @param p_entryId エントリ ID
 * @return エントリの最終アクセス時刻 (Date オブジェクト)
 */
function getLastAccessTime ( p_entryId ) {
  if ( ! p_entryId ) {
    return null;
  }
  var v_sec = g_accessTimeMap.get ( p_entryId );
  var v_result = new Date ();
  v_result.setTime ( v_sec );
  return v_result;
}


/**
 * 最終アクセス時刻に基づいて表示を制御します．
 * もし p_lastUpdated が v_lastAccessTime - p_delay よりも新しければ，
 * p_html を表示します．
 * もし，cookie から最終アクセス時刻が取得できなかったり，
 * p_lastUpdated が正しくなかった場合も p_html を表示します．
 *
 * @param p_entryId エントリ ID
 * @param p_lastUpdated 最終更新日 (Dateオブジェクト)
 * @param p_html 表示したい HTML
 * @param p_delay 最終アクセス日以前どれだけ猶予を設けるか (ms)
 */
function writeIfNewComment ( p_entryId , p_lastUpdated , p_html , p_delay ) {
  // Cookie チェック
  if ( ! document.cookie ) {
    return;
  }
  // エントリーID チェック
  if ( ! p_entryId ) {
    return;
  }
  if ( ! p_html ) {
    return;
  }
  var v_delay = p_delay;
  if ( ! v_delay ) {
    v_delay = 0;
  }
  var v_lastAccessTime = getLastAccessTime ( p_entryId );
  if ( ! v_lastAccessTime || ! p_lastUpdated || ( v_lastAccessTime && v_lastAccessTime.getTime () - v_delay < p_lastUpdated.getTime () ) ) {
    document.open ();
    document.write ( p_html );
    document.close ();
  }
}

/**
 * 最終アクセス時刻に基づいて表示/非表示を制御します．
 * もし p_lastUpdated が v_lastAccessTime - p_delay よりも新しければ，
 * もし，cookie から最終アクセス時刻が取得できなかったり，
 * p_lastUpdated が正しくなかった場合も p_html を表示します．
 *
 * @param p_entryId エントリ ID
 * @param p_lastUpdated 最終更新日 (Dateオブジェクト)
 * @param p_delay 最終アクセス日以前どれだけ猶予を設けるか (ms)
 */
function displayIfNewComment ( p_entryId , p_lastUpdated , p_delay ) {
  // Cookie チェック
  if ( ! document.cookie ) {
    return;
  }
  // エントリーID チェック
  if ( ! p_entryId ) {
    return;
  }
  var v_delay = p_delay;
  if ( ! v_delay ) {
    v_delay = 0;
  }
  var v_lastAccessTime = getLastAccessTime ( p_entryId );
  if ( ! v_lastAccessTime || ! p_lastUpdated || ( v_lastAccessTime && v_lastAccessTime.getTime () - v_delay < p_lastUpdated.getTime () ) ) {
    var v_obj = document.getElementById ( "new" + p_entryId );
    v_obj.style.display = "inline";
  }
}

// END:
// ///////////////////////////////////////////////////////////////////



// ///////////////////////////////////////////////////////////////////
// BEGIN:
// Updated :     2003-10-29
// Description : 未読コメントに遷移するための関数群

/** コメントの id と登録日時を格納 */
var v_commentTime = new Array ();


/**
 * コメントの id と登録日時の組を登録する関数です.
 *
 * @param p_id   コメントの id
 * @param p_time コメントの登録日時
 */
function setCommentTime ( p_id , p_time ) {
  v_commentTime.push ( new Array ( p_id , p_time ) );
}


/**
 * 未読コメントに遷移します．
 */
function goNotViewedComment () {
  // ソート
  v_commentTime.sort ( compareCommentTime );
  var i = 0;
  var v_hash = null;
  for ( i = 0 ; i < v_commentTime.length ; ++ i ) {
    if ( v_commentTime [i][1] >  g_prevAccessTime.getTime () ) {
      v_hash = v_commentTime [i][0];
      break;
    }
  }
  // すべて参照済みだった場合は最後のコメントへ
  if ( v_commentTime [ v_commentTime.length - 1 ][1] < g_prevAccessTime.getTime () ) {
    v_hash = v_commentTime [ v_commentTime.length - 1 ][0];
  }
  if ( ! v_hash ) {
    return false;
  }
  //
  location.hash = v_hash;
  return true;
}

/**
 * ソート用の比較関数です．
 */
function compareCommentTime ( v_a , v_b ) {
  if ( v_a [1] < v_b [1] ) {
    return -1;
  }
  if ( v_a [1] > v_b [1] ) {
    return 1;
  }
  return 0;
}

/**
 * メールアドレスのリンクを生成します．
 */
function showMailAddress ( p_account , p_domain ) {
  document.write ( "&lt;<a href=\"mailto:" );
  document.write ( p_account + "&#" + "64;" + p_domain );
  document.write ( "\">" );
  document.write ( p_account + "&#" + "64;" + p_domain );
  document.write ( "</a>&gt;" );
}

/**
 * 指定 URI へ遷移する
 */
function goUri ( p_uri ) {
  if ( p_uri != null && p_uri != "" ) {
    location.href = p_uri;
  }
}

// END:
// ///////////////////////////////////////////////////////////////////
