function state = flash_update(h,prop,value,delay)

% flash_update - flash updated of object properties
% -------------------------------------------------
%
% state = flash_update(h,prop,value,delay)
%
% Input:
% ------
%  h - handle
%  prop - properties to update
%  value - values to flash
%  delay - duration of flash, negative is indefinite (def: 0.1)
%
% Output:
% -------
%  state - used to restore state for negative delays

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 1789 $
% $Date: 2005-09-15 17:23:27 -0400 (Thu, 15 Sep 2005) $
%--------------------------------

%--------------------------------
% HANDLE INPUT
%--------------------------------

%--
% set default delay
%--

% NOTE: this typically takes longer

if (nargin < 4) || isempty(delay)
	delay = 0.2;
end

%--
% simple check on properties and values
%--

% NOTE: put prop and value into cell if needed

if ischar(prop)
	prop = {prop};
end

if ~iscellstr(prop)
	error('Properties must be a cell array of strings.');
end

if ~iscell(value)
	value = {value};
end
	
if length(prop) ~= length(value)
	error('Properties and values do not match.');
end

%--------------------------------
% FLASH UPDATE
%--------------------------------

%--
% get object properties
%--

if length(h) > 1
	
	if length(unique(get(h, 'type'))) > 1
		error('Object to flash update must be of same ''type''.');
	end
	
end

props = lower(fieldnames(get(h(1))));

%--
% check and remove properties if needed
%--

for k = length(prop):-1:1
	
	if isempty(find(strcmp(props, prop{k})))
		prop(k) = []; value(k) = [];
	end
	
end

if isempty(prop)
	return;
end

%--
% get starting values for properties
%--

for j = 1:length(h)
	
	for k = 1:length(prop)
		init{j}{k} = get(h(j), prop{k});
	end

end

%--
% flash property values
%--

% NOTE: set flash values, pause, and reset to initial values

% NOTE: handle improper property values exception and remove from list

for j = 1:length(h)
	
	for k = length(prop):-1:1
		
		try
			set(h(j), prop{k}, value{k});
		catch
			prop(k) = []; init(k) = [];
		end

	end
	
end

% NOTE: negative delay sets the flash values until restore is called

if delay > 0
	
	pause(delay);

	% NOTE: we obtained this values from the object we know they are proper

	for j = 1:length(h)
		
		for k = 1:length(prop)
			try
				set(h(j), prop{k}, init{j}{k});
			end
		end

	end
	
end

%--
% store output state
%--

if nargout
	state.handles = h; state.prop = prop; state.init = init;
end