/* 
 Fluid Router Tool 2.0 ALPHA
 Copyright 2016 Christopher Allen 
 */
var the_tab;
maintenance_dash.prototype.main = function () {
  return false;
  the_tab = this.tab;
  var dfd_MaintenanceDash_main = jQuery.Deferred();
  start_progress();
  $.when(this.load_left(), this.load_right()).done(function () {
    stop_progress();
    debug_print('AJAX complete');
    dfd_MaintenanceDash_main.resolve("ajax_complete");
  });
  return dfd_MaintenanceDash_main.promise();
};

maintenance_dash.prototype.load_left = function () {
  var dfd_MaintenanceLeft = jQuery.Deferred();
  $("#main-container-left-body").addClass("loading");
  $("main-container-left-footer").html('&nbsp;&nbsp;'); /* keep padding while loading */
  var atldhf = ajax_loader({id: this.id, filter: this.filter}, "app/maintenance/maintenance-left-header.php", "main-container-left-header");
  progress();
  var atldff = ajax_loader({id: this.id, filter: this.filter, selected: this.selected}, "app/maintenance/maintenance-left-footer.php", "main-container-left-footer");
  progress();
  var atldbf = ajax_loader({id: this.id, filter: this.filter, selected: this.selected}, "app/maintenance/maintenance-left-body.php", "main-container-left-body");
  progress();
  $.when(atldhf, atldff, atldbf).done(function () {
    progress();
    $("#main-container-left-body").removeClass("loading");
    debug_print('Left');
    dfd_MaintenanceLeft.resolve("ajax_done_left");
  });
  return dfd_MaintenanceLeft.promise();
};

maintenance_dash.prototype.load_right = function () {
  var dfd_Maintenance = jQuery.Deferred();
  $("#main-container-right-body").addClass("loading");
  var atrdhf = ajax_loader({id: this.id, filter: this.filter, tab: this.tab}, "app/maintenance/maintenance-right-header-dash.php", "main-container-right-header");
  progress();
  var atrdff = ajax_loader({id: this.id, filter: this.filter, tab: this.tab}, "app/maintenance/maintenance-right-footer-dash.php", "main-container-right-footer");
  progress();
  var atrdbf = ajax_loader({id: this.id, filter: this.filter, tab: this.tab}, "app/maintenance/maintenance-right-body-dash.php", "main-container-right-body");
  if (the_tab == 'NewMaintenance') { 
    var atrdmm = ajax_loader({id: this.id, filter: this.filter, tab: this.tab }, "app/page/right-menu-containers.php", "ajax-menu-container-right-body");
  } 
  $.when(atrdhf, atrdff, atrdbf).done(function () {
    progress();
    $("#main-container-right-body").removeClass("loading");
    debug_print('Right');
    dfd_Maintenance.resolve("ajax_done_right");
  });
  return dfd_Maintenance.promise();
};

maintenance_dash.prototype.start_up = function () {
  var filter = this.filter; /* This Filter is used for HASH router navigate */
  var user_id = this.user_id; /* This user ID is used for AJAX Save */

    debug_print('StartUp');
    // Scrool to the top of left container onLoad
    $("#main-container-left-body").scrollTop(-1);
    // Activate the Dropdowns
    $('.dropdown').dropdown(drop_effect);
    
    /*
//    $('#filter, #sections').dropdown({action: 'select', on: 'hover'});
    $('#propertyTable').tablesort();
    $('#propertyTable').on('tablesort:start', function (event, tablesort) {
      debug_print("Starting the sort...");
      showLoading();
    });
    $('#propertyTable').on('tablesort:complete', function (event, tablesort) {
      debug_print("Sort finished!");
      done_loading();
    });
    $('#propertyTable').searchable({
      searchField: '#propertySearch',
      searchType: 'fuzzy',
      onSearchActive: function () {
        $("#main-container-left-body").scrollTop(0);
        $("#topSearch").css("display", "none"); 
        $("#topSearchClose").css("display", "inline-block");
      },
      onSearchEmpty: function (elem) {
        $("#topSearchClose").css("display", "none"); 
        $("#topSearch").css("display", "inline-block"); 
      }
    });

    $('#propertyTable tr:last').after('<tr id="noResults" class="noCount" style="display: none; cursor: default;"><td colspan="4" style="text-align: center; font-weight: 800;">No Tenants Found...</td></tr>');
    $("#propertySearch").on("keyup", function () {
      
      var activeRows = $('#propertyTable tr:not([style*="display: none"]):not(".noCount")').length;
      var realRows = activeRows - 1;
      $('#propertyCount').html(realRows);
      if (activeRows == 1) {
        $("#noResults").css("display", "");
      } else {
        $("#noResults").css("display", "none");
      }
    });

    // Button Clear the search value
    $("#topSearchClose").off("click"); 
    $("#topSearchClose").on("click", function () {
      $("#propertySearch").val("");
      $("#propertySearch").keyup();
      $("#propertySearch").focus();
    });

    // Row click function for Tenant Profile
    $(".list-row").on("click", function () {
      var id = $(this).data("id");
      // Profile the the Default route when you click a profile        
      router.navigate('Maintenance/' + filter + '/' + id + '/Profile');
    });
    // Activate Popups (There is a CSS version, update to that ASAP)    
    $('.item').popup({
      hoverable: true,
      position: 'bottom center',
      delay: {
        show: 100,
        hide: 0
      },
      transition: 'Vertical Flip'
    });

*/  
    done_loading();
    set_height();
    auto_right_menus();
   // $("#propertySearch").focus();

};