var updateTimer = null;

function initWindow()  {
    document.body.oncontextmenu = function()  {  return false;  };
}

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

  //non cache
  $.ajaxSetup( { cache : false } );

  $( 'table#messages > tbody' ).ajaxStart( function( ev )  {
    $( this ).parent().show();
  } ).ajaxSend( function( ev, xhr, options )  {
	  //sent message
  } ).ajaxSuccess( function( ev, xhr, options )  {
	  //get success css Setting
	/*
    $( 'tr:odd', this ).addClass( 'odd' );
    $( 'tr:even', this ).addClass( 'even' );
    $( 'tr > td:nth-child( 2n + 1 )', this ).addClass( 'message' );
    */
  } ).ajaxError( function( ev, xhr, options, err )  {
	  //get error
  } ).ajaxComplete( function( ev, xhr, options )  {
	  //get complete
  } );
}

function getCounter()  {
  $.getJSON( '/lib/counter.php',
    function( messages )  {
      if ( messages.length < 1 )  return;
      var tbody = $( 'table#messages > tbody' );
      tbody.empty();

      $( 'div#date' ).text(messages['datetime'] + ' 現在');
      $( 'div#m_countTotal' ).text(addFigure(messages['t_counter']));
      $( 'div#m_countPersonal' ).text(addFigure(messages['p_counter']));
      $( 'div#m_countCompany' ).text(addFigure(messages['o_counter']));
    }
  );
}


$( document ).ready( function() {
  initWindow();
  initMessages();
  getCounter();
  updateTimer = setInterval( 'getCounter()', 600000 );
} );
