       position :
         top    : 20
 
-    {advanced, domain, addDomain} = @modalTabs.forms.Settings.inputs
-    {hostnameAlias} = @getData()
-    {label} = advanced.getOptions()
+    {hostnameAlias}    = @getData()
+    {advanced, domain} = @modalTabs.forms.Settings.inputs
+    {label}            = advanced.getOptions()
 
     advanced.hide()
     label.setClass 'advanced'

      title    : 'Re-initialize VM'
      callback : -> KD.singletons.vmController.reinitialize hostnameAlias, ->

    advanced.addSubView deleteButton = new KDButtonView
      style    : 'solid compact red'
      title    : 'Delete VM'
      callback : =>

        unless deleteButton.beingConfirmed
          deleteButton.setTitle 'Are you sure?'
          deleteButton.beingConfirmed = yes
          return

        KD.singletons.vmController.deleteVmByHostname hostnameAlias, (err) =>

          return KD.showError err  if err

          {dock} = KD.singletons
          dock.fetchVMs (vms) ->
            dock.vmsList.removeAllItems()
            dock.listVMs vms

          @destroy()


    label.on 'click', (event) =>
      KD.utils.stopDOMEvent event
      label.toggleClass 'expanded'
      advanced.toggleClass 'hidden'

    @modalTabs.forms.Settings.fields.addDomain.hide()
    domain.on 'change', =>
      if domain.getValue() is 'add-domain'
      then @showAddDomain()
      else
        @hideAddDomain()
        @linkDomain domain.getValue()

    addDomain.on 'keyup', (event) =>
      @hideAddDomain()  if event.which is 27

    # FIXME - SY
    # domain.setValue to the domain which vm is connected to
    # alwaysOn.setValue if the vm is always on


  hideAddDomain: ->

    @modalTabs.forms.Settings.fields.domain.show()
    @modalTabs.forms.Settings.fields.addDomain.hide()

  showAddDomain: ->

    @modalTabs.forms.Settings.fields.domain.hide()
    @modalTabs.forms.Settings.fields.addDomain.show()
    @modalTabs.forms.Settings.inputs.addDomain.setFocus()



  linkDomain: (domainId) ->

    return  if @asking

    @asking = yes
    {hostnameAlias} = @getData()

    [jDomain] = @currentDomains.filter (domain) -> domain._id is domainId

    jDomain.bindVM {hostnameAlias}, (err) =>

      @asking = no
      return  if KD.showError err


  prepareDomainOptions: (callback) ->

    KD.remote.api.JDomain.fetchDomains (err, domains) =>

      @currentDomains = domains

      if err
        @modalTabs.forms.Settings.fields.domain.hide()
        return callback [ title : "Couldn't retrieve domains!" ]


      options = domains.map (jdomain) -> title : jdomain.domain, value : jdomain._id
      options.unshift title : 'No domain', value : 'no-domain'
      options.push    title : '+ Add another domain...', value : 'add-domain'

      callback options

        Settings            :
          fields            :
            domain          :
              label         : 'Point to'
              name          : 'addDomain'
              placeholder   : 'type a domain name'
              keyup         : @bound 'handleChange'
              change        : @bound 'handleChange'
              nextElement   :
                rootDomain  :
                  itemClass : KDCustomHTMLView
                  cssClass  : 'root-domain hidden'
                  partial   : ".#{KD.whoami().profile.nickname}.kd.io"
                Save        :
                  itemClass : KDButtonView
                  title     : 'Save'
                  cssClass  : 'solid compact green hidden fl'
                  callback  : @bound 'linkDomain'
                toggleLink  :
                  itemClass : KDToggleButton
                  cssClass  : 'solid compact clear fr subdomain-toggler'
                  loader    : yes
                  states    : [
                    { title : 'point to a subdomain',    callback : @bound 'convertToSubdomainField' }
                    { title : 'point to my root domain', callback : @bound 'revertToDomainField' }
                  ]
            alwaysOn        :
              label         : 'Keep your VM always on'
              name          : 'alwaysOn'
