////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Base Date Link Class
function calendar_link(id, startcal, endcal) {
  this.id = id;
  this.start = startcal;
  this.end = endcal;
  
  // Link sync functions
  this.start.sync_date = this.sync_date_start;
  this.start.sync_time = this.sync_time_start;
  this.end.sync_date = this.sync_date_end;
  this.end.sync_time = this.sync_time_end;
  
  this.start.add_sync_obj(this);
  this.end.add_sync_obj(this);
}

calendar_link.prototype.is_date_crossed = function() {
  if(this.start.get_date_str() > this.end.get_date_str())
    return true;
  return false;
}

calendar_link.prototype.is_date_equal = function() {
  if(this.start.get_date_str() == this.end.get_date_str())
    return true;
  return false;
}

calendar_link.prototype.is_time_crossed = function() {
  if(this.start.get_time_str() >= this.end.get_time_str())
    return true;
  return false;
}

calendar_link.prototype.is_time_equal = function() {
  if(this.start.get_time_str() == this.end.get_time_str())
    return true;
  return false;
}

calendar_link.prototype.get_start_prev_time = function() {
  if(!this.start.prev_hour()) {
    this.start.prev_min();
    this.start.prev_min();
    this.start.prev_min();
  }
}

calendar_link.prototype.get_end_next_time = function() {
  if(!this.end.next_hour()) {
    this.end.next_min();
    this.end.next_min();
    this.end.next_min();
  }
}

calendar_link.prototype.sync_date_start = function() {
  update_display_start = false;
  update_display_end = false;
  
  // Ensure dates are not crossed
  if(this.is_date_crossed()) {
    this.end.set_from_calendar(this.start, 'cur');
    this.end.set_from_calendar(this.start, 'display');
    update_display_end = true;
  }
  
  // Try to increment by an hour (or go back for start)
  if(this.is_date_equal() && this.is_time_crossed()) {
    this.end.set_hour(this.start.get_hour());
    this.end.set_min(this.start.get_min());
    update_display_end = true;
    
    // Shift Time
    /*
    this.get_end_next_time();
    if(this.is_time_equal()) {
      this.get_start_prev_time();
      update_display_start = true;
    }*/
    
    
  }
  
  if(update_display_start)  this.start.refresh_display();
  if(update_display_end)    this.end.refresh_display();
}

calendar_link.prototype.sync_date_end = function() {
  update_display_start = false;
  update_display_end = false;
  
  // Ensure dates are not crossed
  if(this.is_date_crossed()) {
    this.start.set_from_calendar(this.end, 'cur');
    this.start.set_from_calendar(this.end, 'display');
    update_display_start = true;
  }
  
  // If times are crossed, we first try to sync end times
  if(this.is_date_equal() && this.is_time_crossed()) {
    this.end.set_hour(this.start.get_hour());
    this.end.set_min(this.start.get_min());
    update_display_end = true;
    
    /*
    // Shift Time
    this.get_end_next_time();
    if(this.is_time_equal()) {
      this.get_start_prev_time();
      update_display_start = true;
    }
    
    // After Shift Check If we need to change date
    if(!this.is_date_equal()) {
      this.start.prev_min();
      this.end.set_day(this.start.get_day());
      this.end.set_hour(this.start.get_hour());
      this.end.set_min(this.start.get_min());
      this.end.next_min();
      update_display_start = true;
    }
    */
    
  }
  
  if(update_display_start)  this.start.refresh_display();
  if(update_display_end)    this.end.refresh_display();
}

calendar_link.prototype.sync_time_start = function() {
  update_display_start = false;
  update_display_end = false;
  
  if(this.is_date_equal() && this.is_time_crossed()) {
    this.end.set_hour(this.start.get_hour());
    this.end.set_min(this.start.get_min());
    update_display_end = true;
    
    /*
    // Shift Time
    this.get_end_next_time();
    if(this.is_time_equal()) {
      this.get_start_prev_time();
      update_display_start = true;
    }
    */
    
  }
  
  if(update_display_start)  this.start.refresh_display();
  if(update_display_end)    this.end.refresh_display();
}



calendar_link.prototype.sync_time_end = function() {
  update_display_start = false;
  update_display_end = false;
  
  if(this.is_date_equal() && this.is_time_crossed()) {
    this.start.set_hour(this.end.get_hour());
    this.start.set_min(this.end.get_min());
    update_display_start = true;
    
    /*
    // Shift Time
    this.get_start_prev_time();
    if(this.is_time_equal()) {
      this.get_end_next_time();
      update_display_end = true;
    }
    */
    
  }
  
  if(update_display_start)  this.start.refresh_display();
  if(update_display_end)    this.end.refresh_display();
}
