// function with is used by a datatabe to reload from ajax source
$.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource ) {
 
  if ( typeof sNewSource !== 'undefined' ) {
    oSettings.sAjaxSource = sNewSource;
  }
 
  if( typeof oSettings.iProcessingCount === 'undefined' )
    oSettings.iProcessingCount = 0;
 
  this.fnClearTable( this );
  
  oSettings.iProcessingCount += 1;
  this.oApi._fnProcessingDisplay( oSettings, true );
 
  $.getJSON( oSettings.sAjaxSource, null, $.proxy(function(json) {
 
    for ( var i=0 ; i<json.aaData.length ; i++ ) {
    this.oApi._fnAddData( oSettings, json.aaData[i] );
    }
 
    oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
    this.fnDraw( this );
    
    oSettings.iProcessingCount -= 1;
    
    if(oSettings.iProcessingCount === 0)
      this.oApi._fnProcessingDisplay( oSettings, false );
  }, this));
};



var oneSelected = null;
var userTable = null;
var user_id = null;

var twoSelected = null;
var toolTable = null;
var template_id = null;
var tool_user_id = null;

var threeSelected = null;
var privilegTable = null;
var privileg_id = null;

var fourSelected = null;
var extraPrivilegTable = null;
var extra_privileg_id = null;

var userInfoTable = null;
var user_login = null;

$(function() {
  
  //#################
  //# UserTable
  //################
  
  userTable = $('.usertable').dataTable({
		"bProcessing": true,
		"bServerSide": true,
		"sAjaxSource": "./ajaxusertable.php",
    "bJQueryUI": true,
    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
      if ( aData.DT_RowId === oneSelected ) {
          $(nRow).addClass('row_selected');
      }
    },
    "sPaginationType": "full_numbers"
	});
  
  /* Click event handler */
  $(document).on('click', '.usertable tbody tr', function () {
    var id = this.id;

    if( id === oneSelected) {
      oneSelected = null;
      user_id = null;
    } else {
      $('.usertable tbody tr').removeClass('row_selected');
      oneSelected = id;

      // Get the position of the current data from the node
      var aPos = userTable.fnGetPosition( this );

      // Get the data array for this row
      var data = userTable.fnGetData( aPos );
      user_id = data[ 0 ];
      user_login = data[ 1 ];
      
    }

    toolTable.fnReloadAjax("./ajaxtooltable.php?user_id=" + user_id);
    template_id = null;
    tool_user_id = null;
    privilegTable.fnReloadAjax("./ajaxprivilegtable.php?template_id=" + template_id);
    extraPrivilegTable.fnReloadAjax("./ajaxextraprivilegtable.php?tool_user_id=" + tool_user_id);
    userInfoTable.fnReloadAjax("./ajaxuserinfotable.php?user_login=" + user_login);

    $(this).toggleClass('row_selected');
  });
  
  
  //#################
  //# UserInfoTable
  //################
  
  userInfoTable = $('.userinfotable').dataTable({
                "bProcessing": true,
                "bServerSide": true,
                "sAjaxSource": "./ajaxuserinfotable.php",
                "bJQueryUI": true,
                "sPaginationType": "full_numbers"
	});
  
  
  //#################
  //# ToolTable
  //################
  
  toolTable = $('.tooltable').dataTable({
		"bProcessing": true,
		"bServerSide": true,
		"sAjaxSource": "./ajaxtooltable.php",
    "bJQueryUI": true,
    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
      if ( aData.DT_RowId === twoSelected ) {
          $(nRow).addClass('row_selected');
      }
    },
    "fnServerParams": function ( aoData ) {
      aoData.push( { name: 'user_id', value: user_id } );
    },
    "sPaginationType": "full_numbers"
	});
  
  
  /* Click event handler */
  $(document).on('click', '.tooltable tbody tr', function () {
    var id = this.id;

    if( id === twoSelected) {
      twoSelected = null;
      template_id = null;
      tool_user_id = null;
    } else {
      $('.tooltable tbody tr').removeClass('row_selected');
      twoSelected = id;

      // Get the position of the current data from the node
      var aPos = toolTable.fnGetPosition( this );

      // Get the data array for this row
      var data = toolTable.fnGetData( aPos );
      template_id = data[ 5 ];
      tool_user_id = data[ 0 ];

    }
    
    privilegTable.fnReloadAjax("./ajaxprivilegtable.php?template_id=" + template_id);
    extraPrivilegTable.fnReloadAjax("./ajaxextraprivilegtable.php?tool_user_id=" + tool_user_id);

    $(this).toggleClass('row_selected');
  });
  
  
  
  //#################
  //# PrivilegTable
  //################
  
  privilegTable = $('.privilegtable').dataTable({
		"bProcessing": true,
		"bServerSide": true,
		"sAjaxSource": "./ajaxprivilegtable.php",
    "bJQueryUI": true,
    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
      if ( aData.DT_RowId === threeSelected ) {
          $(nRow).addClass('row_selected');
      }
    },
    "fnServerParams": function ( aoData ) {
    },
    "sPaginationType": "full_numbers"
	});
  
  
  /* Click event handler */
  $(document).on('click', '.privilegtable tbody tr', function () {
    var id = this.id;

    if( id === threeSelected) {
      threeSelected = null;
      privileg_id = null;
    } else {
      $('.privilegtable tbody tr').removeClass('row_selected');
      threeSelected = id;

      // Get the position of the current data from the node
      var aPos = privilegTable.fnGetPosition( this );

      // Get the data array for this row
      var data = privilegTable.fnGetData( aPos );
      privileg_id = data[ 0 ];

    }


    $(this).toggleClass('row_selected');
  });
  
  
  
  //#################
  //# ExtraPrivilegTable
  //################
  
  extraPrivilegTable = $('.extraprivilegtable').dataTable({
		"bProcessing": true,
		"bServerSide": true,
		"sAjaxSource": "./ajaxextraprivilegtable.php",
    "bJQueryUI": true,
    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
      if ( aData.DT_RowId === fourSelected ) {
          $(nRow).addClass('row_selected');
      }
    },
    "fnServerParams": function ( aoData ) {
    },
    "sPaginationType": "full_numbers"
	});
  
  
  /* Click event handler */
  $(document).on('click', '.extraprivilegtable tbody tr', function () {
    var id = this.id;

    if( id === fourSelected) {
      fourSelected = null;
      extra_privileg_id = null;
    } else {
      $('.extraprivilegtable tbody tr').removeClass('row_selected');
      fourSelected = id;

      // Get the position of the current data from the node
      var aPos = extraPrivilegTable.fnGetPosition( this );

      // Get the data array for this row
      var data = extraPrivilegTable.fnGetData( aPos );
      extra_privileg_id = data[ 0 ];

    }


    $(this).toggleClass('row_selected');
  });
  
});