/***************************************************************
    SYSTEM   : 新田塚スポーツクラブ アーク
    TITLE    : プログラムダイナミックHTMLの定義
    SCRIPT   : script.js
    VERSION  : Ver1.0.7
    LANGUAGE : JavaScript1.3
    CODESET  : UTF-8
    EXPLAIN  : プログラムページの動的操作を定義する。
    AUTHOR   : yap
    CREATED  : 2008/08/06  //as Ver1.0.1
    UPDATED  : 2010/04/22  //as Ver1.0.7
***************************************************************/

//週ID/週名の定義
var arrWeekId   = new Array('sun','mon','tue','wed','thu','fri','sat');
var arrWeekName = new Array('日','月','火','水','木','金','土');

//和歴名/和歴との差分の定義
var eraName = '平成';
var eraYear = 1988;

//スケジュールフォルダの定義
var dirSchedule = '../data/sche/';

/***************************************************************
    初期値の設定
***************************************************************/

//クエリ変数の取得
var query  = new Array();
buffer = document.location.search.replace(/^\?/, '').split('&');
for (i in buffer) {
    pairs = buffer[i].split('=');
    query[pairs[0]] = pairs[1];
}

//日付の取得
if (query['date']) {
    var today = new Date(query['date'].substr(0, 4), query['date'].substr(4, 2) - 1, query['date'].substr(6, 2));
} else {
    var today = new Date();
}

/***************************************************************
    休日の判定
***************************************************************/
function isHoliday()
{
    //休日フラグの設定
    var strMD = (today.getMonth() + 1) + '/' + today.getDate();
    var flgHol = 0;

    //定休日の設定(3:定休日)  Inserted on 2009.03.20 by yap
    var iniDate = new Date();  //今日の開始日
    iniDate.setDate(1);        //開始日を１日に変更
    var numWeek = parseInt((iniDate.getDay() + today.getDate() - 1) / 7);  //今日が第何週目かを計算
    if (arrReg[today.getDay()][numWeek]) {flgHol = 3;}

    //休日の設定(0:通常日,1:休館日,2:開館日)
    if (holidays) {
        var arrTemp = new Array();
        arrTemp = holidays.split(',');
        for (i = 0; i < arrTemp.length; i++) {
            ret = arrTemp[i].match(/^\s*(-?)\s*(\d{1,2})\s*\/\s*(\d{1,2})\s*$/g);
            if (ret) {
                if (eval(RegExp.$2) == today.getMonth() + 1 && eval(RegExp.$3) == today.getDate()) {
                    flgHol = (RegExp.$1 == '-')? 2: 1;
                }
            }
        }
    }

    //戻り値のセット
    return flgHol;
}

/***************************************************************
    本日のスケジュール作成関数
***************************************************************/
function creSchedule()
{
    dispCurrDate();
    var sw = false;
    var dir = dirSchedule + today.getFullYear() + (Math.floor(today.getMonth() / 3) + 1);

    /**** Inserted on 2009.04.21 & 06.09 by yap from here ****/
    if (specialDays) {
        var strDate = today.getFullYear() + ('0' + (today.getMonth() + 1)).slice(-2) + ('0' + today.getDate()).slice(-2);
        for (var i in specialDays) {
            if (strDate == specialDays[i]) {
                document.write('<img src="' + dir + '/prog_' + strDate.substr(4, 4) + '.gif" alt="レッスンプログラム" id="sche" />');
                sw = true;
            }
        }
    }
    /**** Inserted on 2009.04.21 & 06.09 by yap to here ****/

    if (!sw) {
        var flg = isHoliday();
        if (flg == 1) {  //休館日
            document.write('<img src="' + dir + '/prog_extra.gif" alt="レッスンプログラム" id="sche" />');
        } else if (flg == 2) {  //開館日  Inserted on 2008.10.24 by yap
            document.write('<img src="' + dir + '/prog_alter.gif" alt="レッスンプログラム" id="sche" />');  //Inserted on 2008.10.24 by yap
        } else if (flg == 3) {  //定休日  Inserted on 2009.03.20 by yap
            document.write('<img src="' + dir + '/prog_fri.gif" alt="レッスンプログラム" id="sche" />');  //Inserted on 2009.03.20 by yap
        } else {
            var strDate = today.getFullYear() + '/' + (today.getMonth() + 1) + '/' + today.getDate();
            var strHolName = ktHolidayName(strDate);
            if (strHolName) {
                document.write('<img src="' + dir + '/prog_sun.gif" alt="レッスンプログラム" id="sche" />');
            } else {
                document.write('<img src="' + dir + '/prog_' + arrWeekId[today.getDay()] + '.gif" alt="レッスンプログラム" id="sche" />');
            }
        }
    }
}

/***************************************************************
    スケジュールの表示関数
***************************************************************/
function dispSchedule()
{
    dispCurrDate();
    var sw = false;
    var dir = dirSchedule + today.getFullYear() + (Math.floor(today.getMonth() / 3) + 1);

    /**** Inserted on 2009.04.21 & 06.09 by yap from here ****/
    if (specialDays) {
        var strDate = today.getFullYear() + ('0' + (today.getMonth() + 1)).slice(-2) + ('0' + today.getDate()).slice(-2);
        for (var i in specialDays) {
            if (strDate == specialDays[i]) {
                window.document.images['sche'].src = dir + '/prog_' + strDate.substr(4, 4) + '.gif';
                sw = true;
            }
        }
    }
    /**** Inserted on 2009.04.21 & 06.09 by yap to here ****/

    if (!sw) {
        var flg = isHoliday();
        if (flg == 1) {  //休館日
            window.document.images['sche'].src = dir + '/prog_extra.gif';
        } else if (flg == 2) {  //開館日  Inserted on 2008.10.24 by yap
            window.document.images['sche'].src = dir + '/prog_alter.gif';  //Inserted on 2008.10.24 by yap
        } else if (flg == 3) {  //定休日  Inserted on 2009.03.20 by yap
            window.document.images['sche'].src = dir + '/prog_fri.gif';  //Inserted on 2009.03.20 by yap
        } else {
            var strDate = today.getFullYear() + '/' + (today.getMonth() + 1) + '/' + today.getDate();
            var strHolName = ktHolidayName(strDate);
            if (strHolName) {
                window.document.images['sche'].src = dir + '/prog_sun.gif';
            } else {
                window.document.images['sche'].src = dir + '/prog_' + arrWeekId[today.getDay()] + '.gif';
            }
        }
    }
}

/***************************************************************
    翌日のスケジュール関数
***************************************************************/
function nextSchedule()
{
    var numDay = today.getDate();
    today.setDate(++numDay);
    dispSchedule();
}

/***************************************************************
    前日のスケジュール関数
***************************************************************/
function prevSchedule()
{
    var numDay = today.getDate();
    today.setDate(--numDay);
    dispSchedule();
}

/***************************************************************
    当日日付の表示関数
***************************************************************/
function dispCurrDate()
{
    var strDate = eraName;
    strDate += (today.getFullYear() - eraYear) + '年';
    strDate += (today.getMonth() + 1) + '月';
    strDate += today.getDate() + '日(';
    strDate += arrWeekName[today.getDay()] + ')';
    document.getElementById('date').innerHTML = strDate;
}
