#####################################################
# Create the menu for the "Tool" button                                     #
#####################################################

#--------------
# initially
#--------------
#set tool1 1
#set tool3 1
set tool_timeout_counter 0
set tool 0

proc create_tool_submenu2 { } {
   global tool

   set tool_win .ventana2.bar.tool.menu
   menu $tool_win

   #--------------------------------------------#
   # Initially tool is tool 5 -> None 		#
   # The selection is initialized inside file 	#
   # "tool_submenu.tcl"                  	#
   #--------------------------------------------#

   $tool_win add radio -label "Stbd Drawer" \
 	-variable tool -value tool1 -command {tool_select 1}
   $tool_win add separator
 	
   $tool_win add radio -label "Port Swing Arm" \
 	-variable tool -value tool2 -command {tool_select 2}
   $tool_win add separator

   $tool_win add radio -label "VB Flood (Dn Close)" \
 	-variable tool -value tool3 -command {tool_select 3; loop_tool_select_timeout 1}
   $tool_win add separator

   $tool_win add radio -label "Port Drawer" \
 	-variable tool -value tool4 -command {tool_select 4}
   $tool_win add separator

   $tool_win add radio -label "None" \
 	-variable tool -value tool5 -command {tool_select 5; loop_tool_select_timeout 0}
   $tool_win add separator


}

proc loop_tool_select_timeout {tool_timeout_counter} {

	global tool 
	
	if { $tool_timeout_counter == 160 } {
		timeout_message_popup	
	}
	
	if { $tool_timeout_counter == 180 } {
		set tool_timeout_counter 0
		tool_select 5
		set tool 5
		return
		}
	if { $tool_timeout_counter != 0 } {
		incr tool_timeout_counter
	} else {
		return
		}
			
	after 1000 [list loop_tool_select_timeout $tool_timeout_counter]
	
	
}

proc timeout_message_popup { } {
	
	global env
	set env(DISPLAY) :0.1
	
	
	if [winfo exists .timeout_message] {destroy .timeout_message}	
	toplevel .timeout_message
	wm title .timeout_message "VB Valve Timeout"
	
	label .timeout_message.label -text "VB valve is about to timeout.
Do not active tool now.
You must re-engage the VB Valve to operate."
	
	button .timeout_message.ok -text "Ok" \
		-command {set tool_timeout_counter 179; destroy .timeout_message } -width 5 -height 2
	
	pack .timeout_message.label -side top -pady 2m -padx 2m
	pack .timeout_message.ok -side left  -pady 2m -padx 2m
	
	
}