#compdef mozilla mozilla-firefox mozilla-xremote-client

local curcontext="$curcontext" state line ret=1 suf
typeset -A opt_args

local popts="-installer -CreateProfile -P -ProfileWizard -ProfileManager -SelectProfile"

if [[ $service = *remote* ]]; then
  state=remote
else
  _x_arguments -C \
    '-height[height of startup window]:height' \
    '(-)'{-h,-help}'[show usage message]' \
    "($popts)-installer[start with 4.x migration window]" \
    '-width[width of startup window]:width' \
    '(-)'{-v,-version}'[show the version number and build date]' \
    "($popts)-CreateProfile:profile" \
    "($popts)-P[start with profile]:profile:compadd ~/.mozilla/*/*.slt(\:h\:t)" \
    "($popts)-ProfileWizard[start with profile wizard]" \
    "($popts)-ProfileManager[start with profile manager]" \
    "($popts)-SelectProfile[start with profile selection dialog]" \
    '-UILocale:locale' \
    '-contentLocale:locale' \
    '-remote[execute a command in an existing Mozilla]:remote command:->remote' \
    '-splash[enable splash screen]' \
    '-chat[start with IRC client]' \
    '-news[start with news]' \
    '-jsconsole[start with JavaScript Console]' \
    '-venkman[start with JavaScript debugger]' \
    '-terminal[start with command line terminal]' \
    '-edit[start with editor]:URL:_urls' \
    '-chrome[load the specified chrome]:URL:_urls' \
    '-mail[start with mail]' \
    '-compose[start with messenger compose]:URL:_urls' \
    '*:location:->urls' && ret=0
fi

[[ "$state" = "urls" ]] &&
  _files "$@" && return 0

# Handle mozilla remote commands
if [[ "$state" = "remote" ]]; then  
  local -a remote_commands
  remote_commands=(openURL openFile saveAs mailto addBookmark ping)

  compset -P '*\('
  if compset -S '(|\\)\)*'; then
    set - -S "" "$@"
  else
    set - -S"${${QIPREFIX:+)}:-\)}$compstate[quote] " "$@"
  fi
  case $IPREFIX in
    openURL*)
      if compset -P "*,"; then
        _wanted option expl 'option' compadd "$@" new-tab new-window && ret=0
      else
        compset -S ',*'
        state=urls
      fi
    ;;
    addBookmark*) state=urls;;
    openFile*) _files "$@" -W ~;;
    saveAs*) 
      if compset -P "*,"; then
        _wanted types expl 'data type' \
            compadd "$@" -M 'm:{a-zA-Z}={A-Za-z}' HTML Text PostScript && ret=0
      else
        compset -S ",*" || suf=","
        _files -qS "$suf" -W ~ && ret=0
      fi
    ;;
    mailto*)
      _email_addresses -s, -c && ret=0
    ;;
    *)
      compset -S '(|\\)\(*' || suf="${${QIPREFIX:+(}:-\(}"
      _wanted commands expl 'remote commands' \
          compadd -qS "$suf" -M 'm:{a-zA-Z}={A-Za-z}' -a \
                  remote_commands && ret=0
    ;;
  esac
fi

if [[ "$state" = "urls" ]]; then
  # Complete mozilla urls
  if compset -P about: ; then
    _wanted values expl 'about what' \
        compadd "$@" authors blank cache document fonts global hype image-cache \
            license logo memory-cache mozilla plugins && ret=0
  elif compset -P news: ; then
    _newsgroups "$@" && ret=0
  elif compset -P mailto: ; then
    _email_addresses -c && ret=0
  else
    _tags prefixes
    while _tags; do
      while _next_label prefixes expl 'URL prefix' "$@"; do
        _urls "$expl[@]" && ret=0
	compset -S '[^:]*'
        compadd -S '' "$expl[@]" about: news: mailto: mocha: javascript: && ret=0
      done
      (( ret )) || return 0
    done
  fi
fi

return ret
