
// ページ読み込み後の初期実行
jQuery(document).ready(function (){
  jQuery('#month_select>li[month]').click(button_func);
  jQuery('#month_select>li.prev') .click(pager_func);
  jQuery('#month_select>li.next') .click(pager_func);

  jQuery('#month_select>li[month], #month_select>li.prev').css('cursor','pointer');
  jQuery('#month_select>li[month], #month_select>li.next').css('cursor','pointer');

  get_calender(0);
  show_calendar();
});

// 月をボタンにする
var button_func = function (){
  var $$    = jQuery(this);
  var month = $$.attr('month');
  var $e    = jQuery('#month_select>li[month]');

  $e.removeClass('on');
  $$.addClass('on');    // クリックした月をハイライト

  show_calendar();
}

// 年度を切り替えたときの処理
var pager_func = function (){
  var $$     = jQuery(this);
  var cal  = $$.attr('cal');
  var $prev  = jQuery('li[cal="prev"]');
  var $next  = jQuery('li[cal="next"]');
  var text_p = $prev.text().replace(/年度<</ig, ''); // 年を削って計算
  var text_n = $next.text().replace(/>>年度/ig, '');
  var $calendar = jQuery('#calendar');


  ( cal == 'next' ) ? text_p++ : text_p--;

  text_n = text_p + 2;

  $prev.text( text_p + '年度<<' ); // 年付けて表示
  $next.text( '>>' + text_n + '年度' );

  text_p++;
  get_calender(text_p);

}

// カレンダーを取得
var get_calender = function (yy){
  jQuery('#calendar').html('読み込み中です');

  jQuery.ajax({
    'url'  : 'calendar.cgi',
    'type' : 'get',
    'data' :{
    'c '   :'calendar',
    'yy'   : yy
    },
    success: function(txt){
      jQuery('#calendar').html(txt);
      show_calendar();
    },
    error:function(){
      alert('通信に失敗しました');
    }
  });
}

// 選択中の月のカレンダーを表示する
var show_calendar = function (){
  var $g = jQuery('li.on');
  var current_month =$g.attr('month');

  jQuery('#calendar tr').each( function (){
    var $$$ = jQuery(this);
    var m   = $$$.attr('month');
   ( current_month == m ) ? $$$.show() : $$$.hide();
  });
}


