set alarmx 0
set alarmy 0
set alarm_surveh 0
set alarm_diganal  0
set alarm_elem_num 0
set alarm_name ""
set new_alarm 0
set xalarm 300
set yalarm 200

set timer 0

proc set_alarm_pos { x y } {
   global alarmx
   global alarmy

   set alarmx $x
   set alarmy $y

}


proc set_alarm_name {name} {
	
	global alarm_name
	set alarm_name $name
	
}


proc alarm { } {

        global al
	global new_alarm
	global alarm_name
	global timer
	global xalarm yalarm
	
	#----------------------------------------------------
	# calling the c++ function to check whether there
	# is a new alarm came up or not
	#----------------------------------------------------
	
	set new_alarm [check_alarm]
	set alarm_name [get_alarm_name]
	
	if { $new_alarm == 1 } {
	
		set timer 1
		global al al2
		
		# If there is a new alarm
		set al .ventana.editor.al$alarm_name
		set al2 .ventana2.editor.al$alarm_name	
		
		
		if [winfo exists $al] {
			destroy $al
		}
		
		if [winfo exists $al2] {
			destroy $al2
		}
		
		
		#---------------------------------------------------------------------#
		# Display alarm on the FIRST screen
		#---------------------------------------------------------------------#
			
		global env
		set env(DISPLAY) :0
		toplevel $al 
		wm geometry $al 90x50+$xalarm+$yalarm
 		
		button $al.b -bg red  \
			-text $alarm_name -width 10 -wraplength 90 -height 3
			
		
		#---------------------------------------------------------------------#
		# Bind the toplevel
		#---------------------------------------------------------------------#
							
		bind $al <ButtonRelease-1> {
			
			set tmp %W
			set name [lindex [$tmp configure -text] 4]
   			
   			#---------------------------------------------------------------------#
			# Sending alarm name to database
			#---------------------------------------------------------------------#
			
			add_alarm_to_db $name
       		 	
			#---------------------------------------------------------------------#
			# Turn the button "Alarm List"  red
			#---------------------------------------------------------------------#
       		 	
			.ventana.bar.alarm configure -bg red
				
			if [winfo exists .ventana2.editor] {
				.ventana2.bar.alarm configure -bg red
       		 	}
       		 	                                            		 	
			destroy .ventana.editor.al$name
			destroy  .ventana2.editor.al$name
  
  		# end binding     		 	
		}	
		
		#---------------------------------------------------------------------#
		# Pack things up
		#---------------------------------------------------------------------#
		 
		pack $al.b
		
		#---------------------------------------------------------------------#
		# Flash the alarm
		#---------------------------------------------------------------------#
		 
		after 1 [blink $al.b -bg red white 500 1]
		
		
		#---------------------------------------------------------------------#
		# Display alarm on the SECOND screen
		#---------------------------------------------------------------------#
		
		global env
		set env(DISPLAY) :0.1		
		toplevel $al2 
		wm geometry $al2 90x50+$xalarm+$yalarm
		
			
		button $al2.b -bg red -relief flat \
			-text $alarm_name -width 10 -wraplength 90 -height 3
				 
		
		#---------------------------------------------------------------------#
		# Bind the toplevel
		#---------------------------------------------------------------------#
		 
		bind $al2 <ButtonRelease-1> {
			
			global tmp2
			
			set tmp2 %W
			set name [lindex [$tmp2 configure -text] 4]
   			
   			#---------------------------------------------------------------------#
			# Sending alarm name to database
			#---------------------------------------------------------------------#
			
       		 	add_alarm_to_db $name
       		 	
       		 	#---------------------------------------------------------------------#
       		 	# Turn the button "Alarm List" red
       		 	#---------------------------------------------------------------------#
       		 	
			.ventana2.bar.alarm configure -bg red
       		 	
       		 	if [winfo exists .ventana.editor] {
       		 		.ventana.bar.alarm configure -bg red
       		 	}       		 	      		 	
       		 	                                            		 	
       		 	destroy .ventana.editor.al$name
       		 	destroy  .ventana2.editor.al$name
       		 	
       		 	
		# end binding     		 	
		}	
	
		#---------------------------------------------------------------------#
		# Pack things up
		#---------------------------------------------------------------------#
			
		pack $al2.b
		
		#---------------------------------------------------------------------#
		# Flash the alarm 
		#---------------------------------------------------------------------#
		
		after 1 [blink $al2.b -bg red white 500 2]								
		
		
		#---------------------------------------------------------------------#
		# Set the x y coordinate of the alarm display position
		#---------------------------------------------------------------------#
		
		incr yalarm 30
		
		if { $yalarm >= 300 } { incr xalarm 50; set yalarm 200 }
 	
		#---------------------------------------------------------------------#
		# set the global alarm status in c++ back to 0
		#---------------------------------------------------------------------#
		
		reset_alarm
		
        }

		
	#---------------------------------------------------------------------#
	# Keep looping to check any new alarm 
	#---------------------------------------------------------------------#
	after 200 [list alarm]
	
	
}


proc check_new_alarm {name} {
	
	global new_alarm
	global alarm_name
	
	set new_alarm 1
	set alarm_name $name
	
}


proc blink {w option value1 value2 interval screen} {


	global timer

	if {$timer  == 0 || ![winfo exists $w]} {
		return
	}
	
	if {$screen == 0} {
		set env(DISPLAY) :0
	} else {
		set env(DISPLAY) :0.1		
	}
	
	$w config $option $value1
	after $interval  [list blink $w $option $value2 $value1 $interval $screen]

	# make it stay on top
	raise $w
}
