// JavaScript Document
//初期設定
$( document ).ready( function()  {
  // 初期画面の表示
  $("#newblog").load("cgi/rss/index.php");
  $("#newblog2").load("../rss/index.php");
  //schedule_ajax();
  //updateschedule();

} );


// Ajax通信確立
function schedule_ajax()  {

  // 最新メッセージ領域を準備
  $( '<table id="sche_lists">' )
      .append( '<tbody />' )
      .appendTo( 'div#container-messages' );
  //$( 'table#messages' ).hide();

  // Ajaxによるアクセスにキャッシュを利用しない(毎回サーバにアクセス)
  $.ajaxSetup( { cache : false } );

  // 各種イベント処理の登録
  $( 'table#sche_lists > tbody' ).ajaxStart( function( ev )  {
    // 初めてサーバとの通信を開始するとき
    // 最新メッセージ一覧の表示
    $( this ).parent().show();
  } ).ajaxSend( function( ev, xhr, options )  {
    // サーバにデータを送信する直前
    // 通信開始を知らせるメッセージの表示
    $( this ).html( '<tr><td colspan="4">今年度の例会・事業スケジュールが表示されます</td></tr>' );
  } ).ajaxError( function( ev, xhr, options, err )  {
    // サーバと通信できなかったとき
    // エラーメッセージの表示
    $( this ).html( '<tr><td colspan="4">更新できませんでした</td></tr>' )
  } ).ajaxComplete( function( ev, xhr, options )  {
    // この部分は通信ができたかどうかに関わらず最後に必ず行う
    $( 'span#lastModified' ).text( new Date().toLocaleString() + ' 現在' );
  } );
}

function updateschedule()  {
  // 最新メッセージの更新
  $.getJSON( './cgi/schedule/java/schedule',
    function( messages )  {
      //
      if ( messages.length < 1 )  return;
      var tbody = $( 'table#sche_lists > tbody' );  // 更新する要素
      tbody.empty();
      // 表示画面の更新
	  $( '<tr>' ).html(	'<th>&nbsp;</th>' +
                       	'<th>日　　付</th>' +
                       	'<th>内　　容</th>' +
						'<th>担　　当</th>')
                   .appendTo( tbody );
      $.each( messages, function( i, obj )  {
        $( '<tr>' ).html( '<td class="sche_td01" style="background-color:' + obj.color01 + '">' + obj.classification + '</td>' +
                          '<td class="w100" style="background-color:' + obj.color02 + '">' + obj.event_date + '</td>' +
                          '<td style="background-color:' + obj.color02 + '">' + obj.content + '</td>' +
						  '<td class="w60" style="background-color:' + obj.color02 + '">' + obj.committee + '</td>')
                   .appendTo( tbody );
      } );
    }
  );
}

//ページトップへスクロール
function backtop() {
  var x1 = x2 = x3 = 0;
  var y1 = y2 = y3 = 0;
  if (document.documentElement) {
      x1 = document.documentElement.scrollLeft || 0;
      y1 = document.documentElement.scrollTop || 0;
  }
  if (document.body) {
      x2 = document.body.scrollLeft || 0;
      y2 = document.body.scrollTop || 0;
  }
  x3 = window.scrollX || 0;
  y3 = window.scrollY || 0;
  var x = Math.max(x1, Math.max(x2, x3));
  var y = Math.max(y1, Math.max(y2, y3));
  window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));
  if (x > 0 || y > 0) {
      window.setTimeout("backtop()", 25);
  }
}

