var routeTab;

function maintenance(req) {
  clear_tenants_th();
//  this.filter = req.params.filter;
  this.tab = req.params.tab;
  this.id = req.params.id;
  this.lease_id = req.params.lease_id;
  this.user_id = req.user;
  routeTab = req.params.tab;
}

function maintenance_dash(req) {
  clear_tenants_th();
  //this.filter = req.params.filter;
  this.tab = req.params.tab;
  this.id = req.params.id;
  this.user_id = req.user;
  routeTab = req.params.tab;
}

function settings(req) {
  clear_tenants_th();
  this.filter = req.params.filter;
  this.tab = req.params.tab;
  this.id = req.params.id;  
  this.user_id = req.user;
  routeTab = req.params.tab;  
}

function tenants(req) {
  this.filter = req.params.filter;
  this.id = req.params.id;
  this.lease_id = req.params.lease_id;
  this.tab = req.params.tab;
  this.user_id = req.user;
  this.selected = "true";
  this.trans_type = req.params.trans_type;
  this.bal_type = req.params.bal_type;
  this.method = req.params.method;
  this.trans_id = req.params.trans_id;
  routeTab = req.params.tab;
  force_menu_height();
}

function tenants_dash(req) {
  this.filter = req.params.filter;
  this.tab = req.params.tab;
  this.user_id = req.user;
  this.selected = "";
  routeTab = req.params.tab;
}

function properties(req) {
  clear_tenants_th();
  this.filter = req.params.filter;
  this.id = req.params.id;
  this.tab = req.params.tab;
  this.user_id = req.user;
  this.selected = "true";
  routeTab = req.params.tab;
}

function properties_dash(req) {
  clear_tenants_th();
  this.filter = req.params.filter;
  this.tab = req.params.tab;
  this.user_id = req.user;
  this.selected = "";
  routeTab = req.params.tab;  
}

function page_not_found(req) {}

function clear_tenants_th() {
  $('#left-fixed-th-container').html('');
}

function router_empty_string(str) {
  return (typeof str == 'undefined' || !str || str.length === 0 || str === "" || !/[^\s]/.test(str) || /^\s*$/.test(str) || str.replace(/\s/g,"") === "") ? true : false;
}

function force_menu_height() {
  $('#main-container-right-body').html('');
  var right_container = "#ajax-menu-container-right-body";
  var fetch_right_container = "tenants/containers/right_menu_blank.html";
  if ( ! router_empty_string( $(right_container).html() ) ) {
     return true;
  } 
  
  if (routeTab == 'Transactions' || routeTab == 'ViewTransaction') {
    var storage = new window.app.store();
    if (storage === false) {
      $.get(fetch_right_container, function(data, status) {  
        $(right_container).html(data);
      });
    } else {
      var body = storage.get('right-body');
      if (body === null) {
        $.get(fetch_right_container, function(data, status) {  
          storage.set('right-body', data);
          $(right_container).html(data);
        });
      } else {
        $(right_container).html(body);
      }
    }
  } 
  auto_right_menus();
}

function fix_bugs() {
  var uih = window.uiHeight;
  $("#main-container-right-body").css('height', uih); /* Fixed Height Jumping - Rob 1/25/2019 */
 
  $('#ajax-menu-container-right-body').html('');
  
  $('#main-container-right-body').css('margin-top', '0'); /* Performance booster to clear margin-top*/
  
  // Prevent Modals from Staying Open on reload or Hash Change!
  $('.ui.modal').modal('hide');

  // Prevent Dimmers from Staying Open on reload or Hash Change!
  $('.ui.dimmable .dimmer').dimmer('hide');
  
  $('#right-secondary-content').html('');
}

/* How to Deal with 404 Pages below */
var empty_tab_pages = ['Dashboard', 'Reports', 'Profile', 'Maintenance', 'Tenant', 'Notes', 'Activity', 'NewProperty', 'Transactions', 'Modal'];
var do_404_page_on_bad_tabs = true;

/* Redirect user if no hash tag was found */
if (location.hash == '') {
  location.hash = 'Tenants/Active/Dashboard';
}

var router = new Grapnel();

router.on('navigate', function() {
  fix_bugs();
});

router.get('Maintenance/:tab', show_loader, user, function (req) {
  do_tabs('maintenance_dash', req);
});

router.get('Maintenance/:id/:tab/:lease_id', show_loader, user, function (req) {
  do_tabs('maintenance', req);
});

router.get('Settings/:tab', show_loader, user, function (req) {
  do_tabs('settings', req);
});

router.get('Settings/:id/:tab', show_loader, user, function (req) {
  do_tabs('settings', req);
});

router.get('Tenants/:filter/:id/Transactions/:lease_id/:tab/:trans_type/:bal_type/New', show_loader, user, function (req) {
  do_tabs('tenants', req); // NewTransaction tab
});

router.get('Tenants/:filter/:id/Transactions/:lease_id/:tab/:method/:trans_id/Edit', show_loader, user, function (req) {
  do_tabs('tenants', req); // EditTransaction tab
});

router.get('Tenants/:filter/:id/Transactions/:lease_id/:tab/:trans_id/View', show_loader, user, function (req) {
  do_tabs('tenants', req); // ViewTransaction tab
});

router.get('Tenants/:filter/:id/:tab/:lease_id', show_loader, user, function (req) {
  do_tabs('tenants', req);
});

router.get('Tenants/:filter/:tab', show_loader, user, function (req) {
  do_tabs('tenants_dash', req);
});

router.get('Properties/:filter/:id/:tab', show_loader, user, function (req) {
  do_tabs('properties', req);
});

router.get('Properties/:filter/:tab', show_loader, user, function (req) {
  do_tabs('properties_dash', req);
});

/* Keep this as the very last Route, for 404 to work right! */
router.get('*', function (req, e) {
  if (!e.parent()) {
    do_tabs('page_not_found', req); /* 404 Page on NON: [Tenants/Properties] hash routes */
  }
});
