;+
; PROJECT
;          SolarB EIS
;
; NAME
;          EIS_CHECK_SUFFICIENT_TIME
;
; PURPOSE
;          Function to check that there is sufficient time in which to
;          insert a new study (so that it doesn't overlap).
;
; CATEGORY
;          EIS Timeline Planning
;
; INPUTS
;          all_blocks -
;          cp_tai     - cursor time (in TAI)
;          tlbID      - widget ID of top level base
;          st_dur     - duration of new study
;
; KEYWORDS
;          append     - perhaps we don't need this anymore!?!?
;
; WRITTEN
;          John Rainnie, RAL, Dec-2005
;
; HISTORY
;          v0.1 JAR 11-Jun-2008
;               Removed 5% percent from calculation
;
;-
;______________________________________________________________________________
FUNCTION eis_check_sufficient_time , all_blocks    ,                        $
                                     cp_tai        ,                        $
                                     tlbID         ,                        $
                                     st_dur        ,                        $
                                     append = append

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

    start_time_diff[i] = (*all_blocks)[i].start_time - cp_tai

ENDFOR

next_blocks = WHERE(start_time_diff GT 0, count)

IF (count EQ 0) THEN BEGIN
    ; OK, I can't find any future blocks - so return valid!
    next_block_index = -1
    RETURN , 1
ENDIF ELSE BEGIN
    next_block_index = MIN(ABS(next_blocks))
    ; OK, get start_time of next_block
    start_time = (*all_blocks)[next_block_index].start_time
ENDELSE

; Cool - now we know start_time of next entry
; Check that this new study will not run into the preceding entry

; However, the more sophisticated approach is to calculate 5% of the
; new study duration - and ensure that the start_time of the next
; block is greater (or equal to) than that.

; For APPENDING entries only
;
; Actually need to calculate 5% of total block duration
; = new_study duration + original block duration
; Get stop_time of new entry
IF (KEYWORD_SET(append) EQ 1) THEN BEGIN

    prev_block      = (*all_blocks)[next_block_index - 1]
    prev_block_dur  = prev_block.stop_time - prev_block.start_time
    new_total       = st_dur + prev_block_dur
    new_stop_time   = prev_block.start_time + new_total ;+ (new_total*.05)

ENDIF ELSE BEGIN

    new_stop_time = cp_tai + st_dur ;+ (st_dur * 0.05)

ENDELSE

; OK, get next block
next_block      = (*all_blocks)[next_block_index]
next_start_time = next_block.start_time
IF (new_stop_time GE next_start_time) THEN BEGIN
    ; Oh oh! Problem!
    mText = ["No can do!" , "Placing this study here will overlap " +       $
             "with the following entry!" , "Aborting!"]
    BEEP
    d = DIALOG_MESSAGE(mText , /ERROR , DIALOG_PARENT = tlbID)
    RETURN , 0
ENDIF

; OK, not a problem - the new entry doesn't overlap the
; preceding entry

RETURN , 1

END
