/*
 *
 * ◆ worldclock.js
 *
 *    CREATED:  2001-10-23
 *    MODIFIED: 2002-07-20
 *    BY Yuki SHIMAZU
 *  ----------------------------改変： 2007/09/11 by dsuke (thanks to Yuki)
 * 
 * ◇参考資料
 * http://www.ueda.info.waseda.ac.jp/~gaku/js/how034.html
 * http://isweb13.infoseek.co.jp/computer/nilab/dl.html
 * http://www.apple.co.jp/hotnews/swatch/index.html
 * http://www.swatch.com/internettime/
 * http://www.ne.jp/asahi/taka/hello/summer.html
 * http://www.inside.ne.jp/js/
 *
 */

// まず今が何年か調べる
var today = new Date();
var year = today.getYear();
if (year < 1900) { year += 1900; }

// ローカル時間とグリニッジ標準時の差を分で表示
var tz_offset = today.getTimezoneOffset();

// 米英のサマータイムの期間を格納
var pos_startUS  = GetDstStartUS(year);
var pos_endUS    = GetDstEndUS(year);
var pos_startUK  = GetDstStartUK(year);
var pos_endUK    = GetDstEndUK(year);
var pos_startRU  = GetDstStartRU(year);
var pos_endRU    = GetDstEndRU(year);

// 各都市の時差を格納
// GPS
var tz_sa = -5 * 60;    // GMT -5 スケアメ(レディング)
var tz_sc = -5 * 60;    // GMT  -5 スケカナ(ケベック)
var tz_coc = 8 * 60;   // GMT  +8 中国杯(ハルビン)
var tz_teb = 1 * 60;   // GMT  +1 エリック杯(パリ)
var tz_cor = 3 * 60;   // GMT  +3 ロシア杯(モスクワ)
var tz_nhk = 9 * 60;   // GMT  +9 NHK杯(仙台)
var tz_gpf = 1 * 60;   // gmt  +1 イタリア(トリノ)
//JGP
var tz_aut = 1 * 60;    // GMT +1 ウィーン(オーストリア)
var tz_est = 2 * 60;    // GMT  +2 タリン(エストニア)
var tz_cro = 1 * 60;   // GMT  +1 ザグレブ(クロアチア)
var tz_bul = 2 * 60;   // GMT  +2 ソフィア(ブルガリア)
var tz_ger = 1 * 60;   // GMT  +1 ケムニッツ(ドイツ)
var tz_gbr = 0 * 60;   // GMT  +-0 シェフィールド(イギリス)
var tz_jgpf = 1 * 60;   // gmt  +1 グダニスク(ポーランド)

var tz_jpn = 9 * 60;   // GMT  +9 日本時間

// ◆メインルーチン
function update_watch(arg) {
    // いまの時刻を取得（たぶん1000ミリ秒単位）
    var now  = new Date();
    var n_t  = now.getTime();
	var cg = arg;
	
	if (cg != 'jr') {
		document.getElementById('cl_sa').innerText = printtime(n_t,tz_sa);
		document.getElementById('cl_sc').innerText = printtime(n_t,tz_sc);
		document.getElementById('cl_coc').innerText = printtime(n_t,tz_coc);
		document.getElementById('cl_teb').innerText = printtime(n_t,tz_teb);
		document.getElementById('cl_cor').innerText = printtime(n_t,tz_cor);
		document.getElementById('cl_nhk').innerText = printtime(n_t,tz_nhk);
		document.getElementById('cl_gpf').innerText = printtime(n_t,tz_gpf);
		document.getElementById('cl_sa').textContent = printtime(n_t,tz_sa);
		document.getElementById('cl_sc').textContent = printtime(n_t,tz_sc);
		document.getElementById('cl_coc').textContent = printtime(n_t,tz_coc);
		document.getElementById('cl_teb').textContent = printtime(n_t,tz_teb);
		document.getElementById('cl_cor').textContent = printtime(n_t,tz_cor);
		document.getElementById('cl_nhk').textContent = printtime(n_t,tz_nhk);
		document.getElementById('cl_gpf').textContent = printtime(n_t,tz_gpf);
	} else {
		document.getElementById('cl_jpn').innerText = printtime(n_t,tz_jpn);
		document.getElementById('cl_aut').innerText = printtime(n_t,tz_aut);
		document.getElementById('cl_est').innerText = printtime(n_t,tz_est);
		document.getElementById('cl_cro').innerText = printtime(n_t,tz_cro);
		document.getElementById('cl_bul').innerText = printtime(n_t,tz_bul);
		document.getElementById('cl_ger').innerText = printtime(n_t,tz_ger);
		document.getElementById('cl_gbr').innerText = printtime(n_t,tz_gbr);
		document.getElementById('cl_jgpf').innerText = printtime(n_t,tz_jgpf);
		document.getElementById('cl_jpn').textContent = printtime(n_t,tz_jpn);
		document.getElementById('cl_aut').textContent = printtime(n_t,tz_aut);
		document.getElementById('cl_est').textContent = printtime(n_t,tz_est);
		document.getElementById('cl_cro').textContent = printtime(n_t,tz_cro);
		document.getElementById('cl_bul').textContent = printtime(n_t,tz_bul);
		document.getElementById('cl_ger').textContent = printtime(n_t,tz_ger);
		document.getElementById('cl_gbr').textContent = printtime(n_t,tz_gbr);
		document.getElementById('cl_jgpf').textContent = printtime(n_t,tz_jgpf);
	}
//    document.worldclock.internettime.value = internettime();

  setTimeout('update_watch("' + cg + '")', 999);        // 1000msec = 1sec（秒）
}


// ◆米国版 DST 開始設定（４月第１日曜日2:00AM から 10月最終日曜日1:00AM まで）
// 2007年より3月第2日曜日午前2時?11月第1日曜日午前2時に変更
// 引数arg_yearで指定された年のサマータイム開始時刻を return で返す
function GetDstStartUS(arg_year) {
    // 第2引数の3→4月　第3引数の1→1日　第4引数の2→2:00AM
    //var dst_start = new Date(arg_year, 3, 1, 2, 0, 0);
// for (var i = 1; i <= 7; i++) {
    var dst_start = new Date(arg_year, 2, 1, 2, 0, 0);
    for (var i = 1; i <= 14; i++) {
        dst_start.setDate(i);
        if (0 == dst_start.getDay()) {    // 0は日曜日を意味する
            break;
        }
    }
    return dst_start.getTime();
}

// ◆米国版 DST 終了設定（４月第１日曜日2:00AM から 10月最終日曜日1:00AM まで）
// 2007年より3月第2日曜日午前2時?11月第1日曜日午前2時に変更
// 引数arg_yearで指定された年のサマータイム終了時刻を return で返す
function GetDstEndUS(arg_year) {
    // 第2引数の9→10月　第3引数の31→31日　第4引数の1→1:00AM
// var dst_end = new Date(arg_year, 9, 31, 1, 0, 0);
// for (var i = 31; i > 24; i--) {
    var dst_end = new Date(arg_year, 10, 1, 1, 0, 0);
    for (var i = 1; i <= 7; i++) {
        dst_end.setDate(i);
        if (0 == dst_end.getDay()) {    // 0は日曜日を意味する
            break;
        }
    }
    return dst_end.getTime();
}


// ◆英国版 DST 開始設定（３月最終土曜日2:00AM から 10月最終土曜日2:00AM まで）
//2007年現在は3月最終日曜日午前1時?10月最終日曜日午前1時？
// 引数arg_yearで指定された年のサマータイム開始時刻を return で返す
function GetDstStartUK(arg_yearUK) {
    // 第2引数の2→3月　第3引数の31→31日　第4引数の1→1:00AM
    var dst_startUK = new Date(arg_yearUK, 2, 31, 1, 0, 0);
    for (var i = 31; i > 24; i--) {
        dst_startUK.setDate(i);
        if (0 == dst_startUK.getDay()) {    // 0は日曜日を意味する
            break;
        }
    }
    return dst_startUK.getTime();
}

// ◆英国版 DST 終了設定（３月最終土曜日2:00AM から 10月最終土曜日2:00AM まで）
//2007年現在は3月最終日曜日午前1時?10月最終日曜日午前1時？
// 引数arg_yearで指定された年のサマータイム終了時刻を return で返す
function GetDstEndUK(arg_yearUK) {
    // 第2引数の9→10月　第3引数の31→31日　第4引数の1→1:00AM
    var dst_endUK = new Date(arg_yearUK, 9, 31, 1, 0, 0);
    for (var i = 31; i > 24; i--) {
        dst_endUK.setDate(i);
        if (0 == dst_endUK.getDay()) {    // 0は日曜日を意味する
            break;
        }
    }
    return dst_endUK.getTime();
}

// ◆ロシア版 DST 開始設定（3月最終日曜日午前2時?10月最終日曜日午前3時まで）
// 引数arg_yearで指定された年のサマータイム開始時刻を return で返す
function GetDstStartRU(arg_yearRU) {
    // 第2引数の2→3月　第3引数の31→31日　第4引数の2→2:00AM
    var dst_startRU = new Date(arg_yearRU, 2, 31, 2, 0, 0);
    for (var i = 31; i > 24; i--) {
        dst_startRU.setDate(i);
        if (0 == dst_startRU.getDay()) {    // 0は日曜日を意味する
            break;
        }
    }
    return dst_startRU.getTime();
}

// ◆ロシア版 DST 終了設定（3月最終日曜日午前2時?10月最終日曜日午前3時まで）
// 引数arg_yearで指定された年のサマータイム終了時刻を return で返す
function GetDstEndRU(arg_yearRU) {
    // 第2引数の9→10月　第3引数の31→31日　第4引数の3→3:00AM
    var dst_endRU = new Date(arg_yearRU, 9, 31, 3, 0, 0);
    for (var i = 31; i > 24; i--) {
        dst_endRU.setDate(i);
        if (0 == dst_endRU.getDay()) {    // 0は日曜日を意味する
            break;
        }
    }
    return dst_endRU.getTime();
}

// ◆表示・サマータイム処理
function printtime(now_time, timezone) {
    var hour, min, sec;

    var pos_t = now_time + (tz_offset + timezone) * 60 * 1000;

    // サマータイム処理
    if (timezone == tz_teb  ||  timezone == tz_est || timezone == tz_gbr) {
        // 欧州
        if ((pos_startUK <= pos_t) && (pos_endUK > pos_t)) {
            pos_t += 60 * 60 * 1000;
        }
    } else if ( timezone == tz_sa || timezone == tz_sc ) {
        // 北米
        if ((pos_startUS <= pos_t) && (pos_endUS > pos_t)) {
            pos_t += 60 * 60 * 1000;
        }
    } else if ( timezone == tz_cor ) {
        // ロシア
        if ((pos_startRU <= pos_t) && (pos_endRU > pos_t)) {
            pos_t += 60 * 60 * 1000;
        }
   }

    var t = new Date();
	var aryDay = new Array("日","月","火","水","木","金","土");

    t.setTime(pos_t);
	month = t.getMonth() + 1;
	date = t.getDate();
    hour = t.getHours();
    min  = t.getMinutes();
    sec  = t.getSeconds();
	day = t.getDay();
	
    // 「XX時00分00秒」で、かつ、XX が「4,5,6,16,18,20」のうち
    // いずれかであるときにページをリロード
    // この処理により時間ごとの背景色が自動的に変わる
    if ( (hour==4||hour==5||hour==6||hour==16||hour==18||hour==20) && min==0 && sec==0 ) {
        window.location.reload();
    }

    if (hour < 10) {
        hour = "0" + hour;
    }
    if (min < 10) {
        min = "0" + min;
    }
    if (sec < 10) {
        sec = "0" + sec;
    }

//    return hour + ":" + min + ":" + sec;
   return month + "/" + date + " (" + aryDay[day] + ") " + hour + ":" + min;
}



// ◆インターネットタイムの処理
function internettime() {
    var c = new Date();
    var gmt = c.toGMTString();
    var d = gmt.indexOf(":")

    var h = eval( gmt.slice(d-2,d) );
    var m = eval( gmt.slice(d+1,d+3) );
    var s = eval( gmt.slice(d+4,d+6) );

    // LONDON と BIEL（SWATCH本社所在地／PARISとtime zoneが同じ）の 時差は１時間（3600秒）
    var beat = (h * 3600 + m * 60 + s + 3600) / 86.4;

    if (beat >= 1000) beat = beat - 1000;
    beat = Math.floor(beat);

    var zero = "";
    if (beat < 100) zero = "0";
    if (beat < 10) zero = "00";

    return "@" + zero + beat;
}
