function screen() {
  this.elem = document.createElement('div')
  this.elem.className = 'area'
  this.elem.style.width = '100%'
  wrapper.appendChild(this.elem)
  this.len = 0
  this.olen = 0
  this.wndpos = new Array()
  
  this.overlay = document.createElement('div')
  this.overlay.className = 'overlay'
  this.elem.appendChild(this.overlay)
  
  this.rethink()
  this.update()
}

screen.prototype.rethink = function() {
  var navil = navi.length
  var mh, th, borders, bl, border
  mh = 0
  th = 0
  for (var i = 0; i < navil; i++) {
    border = 0
    borders = navi[i].border
    bl = borders.length
    for (var ii = 0; ii < bl; ii += 2) {
      border += borders[ii]
    }
    mh += navi[i].height
    if (navi[i].pos[0] != -1)
      th += navi[i].height + border
    if (navi[i].pos[0] != -1 || navi[i].pos[2] != -1)
      mh += border
  }
  this.bi = mh
  this.elem.style.top = th + 'px'
}

screen.prototype.wh = function() {
  if (self.innerHeight) {
    h=self.innerHeight
    w=self.innerWidth
  }
  else if (document.documentElement && document.documentElement.clientHeight) {
    h=document.documentElement.clientHeight
    w=document.documentElement.clientWidth
  }
  else if (document.body) {
    h=document.body.clientHeight
    w=document.body.clientWidth
  }
  
  sh = h - this.bi
}

screen.prototype.change_focus = function(num, det) {
  if (det == undefined) var det = 0
  
  var num = num * 1
  for (var i = 0; i < this.len; i++) {
    if (this.wndpos[i] == num) this.wndpos.splice(i, 1)
  }
  if (det == 0)
    this.wndpos.splice(0, 0, num)
  else if (det == 1)
    this.wndpos.splice(this.len, this.len, num)
  else if (det == 2) {
    this.len--
    this.elem.removeChild(wnds[num].elem)
    navi[posis['appboxes']].appboxes.removeChild(wnds[num].appbox)
    
    for (i = 0; i < this.len; i++) {
      if (this.wndpos[i] > num) {
        wnds[this.wndpos[i]].renum(-1)
        this.wndpos[i]--
      }
    }
    
    if (moving_window > num) moving_window--
    else if (moving_window == num) this.move_end()
    
    if (adjusting_window > num) adjusting_window--
    else if (adjusting_window == num) this.adjust_end()
    
    delete wnds[num]
    wnds.splice(num, 1)
    
  }
  this.setindex()
}

screen.prototype.setindex = function() {
  for (var i = 1; i < this.len; i++) {
    wnds[this.wndpos[i]].elem.style.zIndex = this.len - i + 1
    wnds[this.wndpos[i]].elem.id = ''
    wnds[this.wndpos[i]].appbox.className = ''
  }
  if (this.len > 0) {
    wnds[this.wndpos[0]].elem.style.zIndex = this.len + 2
    wnds[this.wndpos[0]].elem.id = 'active'
    wnds[this.wndpos[0]].appbox.className = ''
    this.overlay.style.zIndex = this.len + 1
  }
  
  var vis = false
  for (i = 0; i < this.len; i++) {
    if (wnds[i].visible) vis = true
  }
  if (vis) wnds[this.wndpos[0]].appbox.className = 'current'
  
  navi[posis['appboxes']].check_abs_height(true)
}

screen.prototype.set_overlay = function() {
  this.overlay.style.display = 'block'
}

screen.prototype.del_overlay = function() {
  this.overlay.style.display = 'none'
}

screen.prototype.move_start = function(what) {
  moving_window = what * 1
  wnds[moving_window].cn('data_overlay').style.display = 'block'
  this.set_overlay()
  
  diff_x = x - wnds[moving_window].elem.offsetLeft
  diff_y = y - wnds[moving_window].elem.offsetTop
}

screen.prototype.move_end = function() {
  if (moving_window > -1)
    wnds[moving_window].cn('data_overlay').style.display = 'none'
  moving_window = -1
  this.blurfocus()
}

screen.prototype.adjust_start = function(what, dir) {
  adjusting_window = what * 1
  adjusting_dir = dir * 1
  wnds[adjusting_window].cn('data_overlay').style.display = 'block'
  this.set_overlay()
  
  if (adjusting_dir != 1 && adjusting_dir != 5) { 
    start_x = x
    start_w = wnds[adjusting_window].width
    diff_x = start_x - wnds[adjusting_window].elem.offsetLeft
  }
  if (adjusting_dir != 3 && adjusting_dir != 7) { 
    start_y = y
    start_h = wnds[adjusting_window].height
    diff_y = start_y - wnds[adjusting_window].elem.offsetTop
  }
}

screen.prototype.adjust_end = function() {
  if (adjusting_window > -1)
    wnds[adjusting_window].cn('data_overlay').style.display = 'none'
  adjusting_window = -1
  adjusting_dir = -1
  this.blurfocus()
}

screen.prototype.blurfocus = function() {
  this.del_overlay()
  highlightfix.focus()
  highlightfix.select()
}

screen.prototype.setwindow = function(a, b, c, d, e, f, g, h, i, j, k, l, m, n) {
  wnds[wnds.length] = new scr_window(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
  
  this.wndpos.splice(0, 0, this.len)
  this.len++
  this.olen++
  this.setindex()
}

screen.prototype.move_window = function() {
  var cx = x - diff_x
  if (cx < -diff_x) cx = -diff_x
  else if (cx > w - diff_x) cx = w - diff_x
  var cy = y - diff_y
  if (cy < 0) cy = 0
  else if (cy > sh - this.hheight) cy = sh - this.hheight
  if (this.len > moving_window) {
    wnds[moving_window].elem.style.left = cx + 'px'
    wnds[moving_window].elem.style.top = cy + 'px'
  }
}

screen.prototype.adjust_window = function() {
  if (adjusting_dir != 1 && adjusting_dir != 5) {
    var cx, cw, cxs
    cxs = false
    if (adjusting_dir == 0 || adjusting_dir == 6 || adjusting_dir == 7) {
      cx = x - diff_x
      cw = start_w + start_x - x
      if (x - diff_x < 0) {
        cx = 0
        cxs = true
      }
    }
    else {
      cx = start_x - start_w + start_w - diff_x
      cw = start_w + x - start_x
      if (x + start_w - diff_x > w) cxs = true
    }
    
    if (cw < width_min) cxs = true
    
    if (!cxs && this.len > adjusting_window) {
      wnds[adjusting_window].elem.style.left = cx + 'px'
      wnds[adjusting_window].width = cw
    }
  }
  
  if (adjusting_dir != 3 && adjusting_dir != 7) {
    var cy, ch, cys
    cys = false
    if (adjusting_dir == 0 || adjusting_dir == 1 || adjusting_dir == 2) {
      cy = y - diff_y
      ch = start_h + start_y - y
      if (y - diff_y < 0) {
        cy = 0
        cys = true
      }
    }
    else {
      cy = start_y - start_y + start_y - diff_y
      ch = start_h + y - start_y
      if (y + start_h - diff_y > sh) cys = true
    }
    
    if (ch < height_min) cys = true
    
    if (!cys && this.len > adjusting_window) {
      wnds[adjusting_window].elem.style.top = cy + 'px'
      wnds[adjusting_window].height = ch
    }
  }
  
  wnds[adjusting_window].update()
}

screen.prototype.set_coor = function(e) {
  if (!e) var e = window.event
  if (e.pageX || e.pageY) {
    x = e.pageX
    y = e.pageY
  }
  else if (e.clientX || e.clientY) {
    x = e.clientX + document.body.scrollLeft
    y = e.clientY + document.body.scrollTop
  }
  
  if (moving_window != -1)
    this.move_window()
  else if (adjusting_window != -1)
    this.adjust_window()
}

screen.prototype.update = function() {
  this.wh()
  this.elem.style.height = sh + 'px'
}