limite = -310

var i_subiendo_a
var i_subiendo_b
var i_subiendo_c
var i_subiendo_d
var i_subiendo_e

var i_bajando_a
var i_bajando_b
var i_bajando_c
var i_bajando_d
var i_bajando_e

// 0 abajo
// 1 subiendo
// 2 arriba
// 3 bajando
var a_estado = 0
var b_estado = 0
var c_estado = 0
var d_estado = 0
var e_estado = 0

// var moviendo = false

function subiendo(x) {
  eval(x + "_estado=1")
  pos_actual = parseInt(document.getElementById('capa_'+x).style.top)
  if (pos_actual > limite)
    document.getElementById('capa_'+x).style.top = (pos_actual - velocidad_subiendo(pos_actual)) + "px"
  else {
    document.getElementById('capa_'+x).style.top = limite + "px"
    eval(x + "_estado=2")
    clearInterval(eval("i_subiendo_" + x))
  }
}

function bajando(x) {
  eval(x + "_estado=3")
  pos_actual = parseInt(document.getElementById('capa_'+x).style.top)
  if (pos_actual < 0)
    document.getElementById('capa_'+x).style.top = (pos_actual + velocidad_bajando(pos_actual)) + "px"
  else {
    document.getElementById('capa_'+x).style.top = "0px"
    eval(x + "_estado=0")
    clearInterval(eval("i_bajando_" + x))
  }
}

function subir(x) {
  // alert('subir' + x)
  parar(x)
  eval("i_subiendo_" + x + " = setInterval(\"subiendo('\" + x +\"')\",50)")
}

function bajar(x) {
  // alert('bajar' + x)
  parar(x)
  eval("i_bajando_" + x + " = setInterval(\"bajando('\" + x +\"')\",50)")
}

function parar(x) {
  clearInterval(eval("i_subiendo_" + x))
  clearInterval(eval("i_bajando_" + x))
}

function velocidad_bajando(x) {
  if (x < -32)
    return 20
  else
    return 5
}

function velocidad_subiendo(x) {
  if (x < -275)
    return 2
  else
    return 25
}

function pinchar(x) {
  clearInterval(i_bajando_a)
  clearInterval(i_bajando_b)
  clearInterval(i_bajando_c)
  clearInterval(i_bajando_d)
  clearInterval(i_bajando_e)

  clearInterval(i_subiendo_a)
  clearInterval(i_subiendo_b)
  clearInterval(i_subiendo_c)
  clearInterval(i_subiendo_d)
  clearInterval(i_subiendo_e)

  if (x=='a') {
    if (a_estado==0 || a_estado==3) { subir('a'); bajar('b'); bajar('c'); bajar('d'); bajar('e'); }
    else                            { bajar('a'); }
  }
  if (x=='b') {
    if (b_estado==0 || b_estado==3) { bajar('a'); subir('b'); bajar('c'); bajar('d'); bajar('e'); }
    else                            { bajar('b'); }
  }
  if (x=='c') {
    if (c_estado==0 || c_estado==3) { bajar('a'); bajar('b'); subir('c'); bajar('d'); bajar('e'); }
    else                            { bajar('c'); }
  }
  if (x=='d') {
    if (d_estado==0 || d_estado==3) { bajar('a'); bajar('b');  bajar('c'); subir('d'); bajar('e'); }
    else                            { bajar('d'); }
  }
  if (x=='e') {
    if (e_estado==0 || e_estado==3) { bajar('a'); bajar('b');  bajar('c'); bajar('d'); subir('e'); }
    else                            { bajar('e'); }
  }
}

