function style_bg_img(background) {
  var cssurl
  if (background.indexOf(".")!=-1) cssurl=["url(",")"]
  else cssurl=["",""]
  return cssurl[0]+background+cssurl[1]
}

function remove(obj) {
  if (obj.type="character") take_life(obj)
  obj.remove()
  delete obj
}

function getkeycode(e) {
  var keycode
  if (window.event) keycode=window.event.keyCode
  else if (e) keycode=e.which
  else keycode=0
  return keycode
}

function START__movement_detection_system() {
  characters=new Array()
  characters_time=setInterval(movement_detection_system,100)
}

function movement_detection_system() {
  for (var i=0;i<characters.length;i++) {
    characters[i].elems[5][2].style.display="block"
  }
}

function find_direction() {
  if (key_order[0]==1 && key_order[1]!=2 && key_order[1]!=4) direction=1
  else if ((key_order[0]==1 && key_order[1]==2) || (key_order[0]==2 && key_order[1]==1)) direction=2
  else if (key_order[0]==2 && key_order[1]!=1 && key_order[1]!=3) direction=3
  else if ((key_order[0]==2 && key_order[1]==3) || (key_order[0]==3 && key_order[1]==2)) direction=4
  else if (key_order[0]==3 && key_order[1]!=2 && key_order[1]!=4) direction=5
  else if ((key_order[0]==3 && key_order[1]==4) || (key_order[0]==4 && key_order[1]==3)) direction=6
  else if (key_order[0]==4 && key_order[1]!=3 && key_order[1]!=1) direction=7
  else if ((key_order[0]==4 && key_order[1]==1) || (key_order[0]==1 && key_order[1]==4)) direction=8
  else direction=0
  
  document.title=direction
}

function keydown(e) {
  var keycode=getkeycode(e)
    
  if (keycode==38 && !key_up) {
    key_up=true
    key_order.splice(0,0,1)
    key_order.splice(3,1)
  }
  else if (keycode==39 && !key_right) {
    key_right=true
    key_order.splice(0,0,2)
    key_order.splice(3,1)
  }
  else if (keycode==40 && !key_down) {
    key_down=true
    key_order.splice(0,0,3)
    key_order.splice(3,1)
  }
  else if (keycode==37 && !key_left) {
    key_left=true
    key_order.splice(0,0,4)
    key_order.splice(3,1)
  }
  else if (keycode==16) key_shift=true
  else if (keycode==17) key_control=true
  
  find_direction()
}

function keyup(e) {
  var keycode=getkeycode(e)
  
  if (keycode==38 && key_up) {
    key_up=false
    for (var i=0;i<4;i++) {
      if (key_order[i]==1) key_order.splice(i,1)
    }
    key_order.splice(3,0,0)
  }
  else if (keycode==39 && key_right) {
    key_right=false
    for (var i=0;i<4;i++) {
      if (key_order[i]==2) key_order.splice(i,1)
    }
    key_order.splice(3,0,0)
  }
  else if (keycode==40 && key_down) {
    key_down=false
    for (var i=0;i<4;i++) {
      if (key_order[i]==3) key_order.splice(i,1)
    }
    key_order.splice(3,0,0)
  }
  else if (keycode==37 && key_left) {
    key_left=false
    for (var i=0;i<4;i++) {
      if (key_order[i]==4) key_order.splice(i,1)
    }
    key_order.splice(3,0,0)
  }
  else if (keycode==16) key_shift=false
  else if (keycode==17) key_control=false
  
  find_direction()
}

function bring_to_life(cha) {
  characters[characters.length]=cha
}

function take_life(cha) {
  for (var i=0;i<characters.length;i++) {
    if (characters[i]==cha) characters.splice(i,1)
  }
}