;+
;PROJECT
;           SolarB EIS
;
;NAME
;           EIS_CHECK_FIVEPC_RULE
;
;PURPOSE
;           Function to check 5% rule. The block of entries preceding
;           the cursor time is found - and more importantly its
;           stop_time. Output is the start_time of the new entry.
;
;CATEGORY
;           EIS Timeline Planning
;
;INPUTS
;           all_blocks -
;           cp_tai     - cursor time (in TAI)
;           tlbID      - widget ID of top level base
;KEYWORDS
;           None
;
;MODIFICATION HISTORY
;           Written by: John Rainnie, RAL - j.rainnie@rl.ac.uk (Dec 2005)
;
;-
;______________________________________________________________________________
FUNCTION eis_check_fivepc_rule , all_blocks , cp_tai , tlbID

; We want the previous block - if it exists!
; In which block is the cursor positioned?
; Prev block - get list of -ve diffs
stop_time_diff  = DBLARR(N_ELEMENTS(*all_blocks))
FOR i = 0, N_ELEMENTS(*all_blocks) -1 DO BEGIN

    stop_time_diff[i]  = (*all_blocks)[i].stop_time  - cp_tai

ENDFOR

prev_blocks = WHERE(stop_time_diff LT 0, count)
IF (count EQ 0) THEN BEGIN
    ; OK, there are NO preceding blocks - just return the curor time
    prev_block_index = -1
    RETURN , cp_tai
ENDIF ELSE BEGIN
    prev_block_index = MIN(ABS(prev_blocks))
    ; OK, get stop_time of prev_block
    stop_time = (*all_blocks)[prev_block_index].stop_time
ENDELSE

; Cool - now we know stop_time  of prev entry
;
; First job - check the 5% rule - relative to prev entry
; OK, get prev block
prev_block = (*all_blocks)[prev_block_index]
; Get prev block duration
block_duration = prev_block.stop_time-prev_block.start_time   ; in seconds
fivep_duration = block_duration * 0.05
; Get difference between block.stop_time and cursor time
diff = cp_tai - prev_block.stop_time

IF (diff LT fivep_duration) THEN BEGIN

    new_start = prev_block.stop_time + fivep_duration
    warning   = eis_five_pc_dialog(new_start , tlbID)
    CASE warning OF
        'Yes' :  start_time = new_start
        'No'  :  start_time = cp_tai
        ELSE  :  start_time = 0.D
    ENDCASE

ENDIF ELSE start_time = cp_tai

RETURN , start_time

END
