/* 
 Fluid Router Tool 2.0 ALPHA
 Copyright 2016 Christopher Allen 
 */

properties.prototype.main = function () {
  var dfd_Properties_main = jQuery.Deferred();
  start_progress();
  $.when(this.load_left(), this.load_right()).done(function () {
    stop_progress();
    debug_print('AJAX complete');
    dfd_Properties_main.resolve("ajax_complete");
  });
  return dfd_Properties_main.promise();
};

properties.prototype.load_left = function (callback) {
  var dfd_PropertiesLeft = 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/properties/properties-left-header.php", "main-container-left-header");
  progress();
  var atldff = ajax_loader({id: this.id, filter: this.filter, selected: this.selected}, "app/properties/properties-left-footer.php", "main-container-left-footer");
  progress();
  var atldbf = ajax_loader({id: this.id, filter: this.filter, selected: this.selected}, "app/properties/properties-left-body.php", "main-container-left-body");
  progress();
  $.when(atldhf, atldff, atldbf).done(function () {
    progress();
    $("#main-container-left-body").removeClass("loading");
    debug_print('Left');
    if (typeof callback !== 'undefined' && cloneObject[callback]) {
      cloneObject[callback]();
    }
    dfd_PropertiesLeft.resolve("ajax_done_left");
  });
  return dfd_PropertiesLeft.promise();
};

properties.prototype.load_right = function (callback) {
  var dfd_PropertiesRight = jQuery.Deferred();
  $("#main-container-right-body").addClass("loading");
  var atrdhf = ajax_loader({id: this.id, filter: this.filter, tab: this.tab}, "app/properties/properties-right-header.php", "main-container-right-header");
  progress();
  var atrdff = ajax_loader({id: this.id, filter: this.filter, tab: this.tab}, "app/properties/properties-right-footer.php", "main-container-right-footer");
  progress();
  var atrdbf = ajax_loader({id: this.id, filter: this.filter, tab: this.tab}, "app/properties/properties-right-body.php", "main-container-right-body");
  $.when(atrdhf, atrdff, atrdbf).done(function () {
    /* Slide out right Effect for clicked profiles */
//    $('.rightSlide').css('opacity', '1');
//    $('.rightSlide')
//            .transition('fade right', '0ms')
//            .transition('fade right', '300ms');
    progress();
    $("#main-container-right-body").removeClass("loading");
    debug_print('Right');
    if (typeof callback !== 'undefined' && cloneObject[callback]) {
      cloneObject[callback]();
    }
    dfd_PropertiesRight.resolve("ajax_done_right");
  });
  return dfd_PropertiesRight.promise();
};

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

    debug_print('StartUp');
    // Unbind left mouse enter
    $("#main-container-left-body").off("mouseenter");
    // Hide search when in profile
    $("#propertySearchContainer").css("display", "none");
    // Show go back button
    $("#properties_back").css("display", "inline-block");
    // Unbind right mouse enter
    $("#main-container-right-body").off("mouseenter");
    //$('#propertyTable').tablesort();
    $('.phoneNum').mask('(000) 000-0000');
    $('.money').mask('#,##0.00', {reverse: true});
    $('#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'
    });
    $('.dropdown').dropdown(drop_effect);
    // 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('Properties/' + filter + '/' + id + '/Profile');
    });
    $('.ui.accordion').accordion();
    // 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'
    });
    $("time.timeago").timeago();

    done_loading();
    set_height();
    auto_right_menus();
};