OK, looks like you need three conditions:
alias togglewin {
if ($active == $1-) {
;; It's active. Hide it.
window -h $qt($1-)
}
elseif (!$istok(minimized hidden,$window($1-).state,32)) {
;; It's not active, but it is visible. Hide it.
window -h $qt($1-)
}
else {
;; It's not visible, activate it.
window -a $qt($1-)
}
}
Now the problem is that if your three windows overlap such that you cannot see all of one of them it will take two clicks to activate it since the code believes that it is, in fact, visible.
Part of the problem is that when you are using a dialog, the active window is that from which the dialog was launched, not the most recently active window. I can't think of a way to get around that at the moment.