TablaAT = new Array("#Areas#v1#")
TablaET = new Array("#Ejercicios#v1#")
TablaVT = new Array("#Videos#v1#")

function CAT(datos){
  datos=datos.split("|")
  na=TablaAT.length
  TablaAT[na]=new DatosCAT(na,datos)
}

function CET(datos){
  datos=datos.split("|")
  ne=TablaET.length
  TablaET[ne]=new DatosCET(ne,datos)
}

function CVT(datos){
  datos=datos.split("|")
  ne=TablaVT.length
  TablaVT[ne]=new DatosCVT(ne,datos)
}

function DatosCAT(numero,datos){
  this.num     = numero
  this.tienda  = datos[0]
  this.carpeta = datos[1]
  this.titulo  = datos[2]
}

function DatosCET(numero,datos){
  this.num      = numero
  this.tienda   = datos[0]
  this.carpeta  = datos[1]
  this.titulo   = datos[2]
  this.nombarch = datos[3]
}

function DatosCVT(numero,datos){
  this.num      = numero
  this.carpeta  = datos[0]
  this.nombarch = datos[1]
  this.fuente   = datos[2]
  this.video    = datos[3]
}

function CargaAreas(){
  sig=1
  for(elem=1;elem<TablaAT.length;elem++)
  {
    area=TablaAT[elem].titulo
    carp=TablaAT[elem].carpeta
    document.selector.area[sig]=new Option(area,carp+"|"+elem)
    sig=sig+1
  }
  document.selector.area[0]=new Option("- Lista de Áreas ["+(sig-1)+"] -")
  document.selector.area[0].selected=true
}

function CargaEjercicios(){
  area=QueArea()
  sig=1
  for(elem=1;elem<TablaET.length;elem++)
  {
    if(TablaET[elem].carpeta==area)
    {
      ejer=TablaET[elem].titulo
      arch=TablaET[elem].nombarch
      document.selector.ejer[sig]=new Option(ejer,arch+"|"+elem)
      sig=sig+1
    }
  }
  document.selector.ejer[0]=new Option("- Lista de Ejercicios ["+(sig-1)+"] -")
  document.selector.ejer[0].selected=true
}

function CargaVideos(){
  area=QueArea()
  ejer=QueEjercicio()
  sig=1
  for(elem=1;elem<TablaVT.length;elem++)
  {
    if(TablaVT[elem].carpeta==area && TablaVT[elem].nombarch==ejer)
    {
      document.selector.nvid[sig]=new Option(sig,sig+"|"+elem)
      sig=sig+1
    }
  }
  document.selector.nvid[0]=new Option("- ["+(sig-1)+"] -")
  document.selector.nvid[0].selected=true
}

function QueArea(dato){
  indice=document.selector.area.selectedIndex
  if(indice==0){return null}
  valor=document.selector.area[indice].value
  valor=valor.split("|")
  if(!dato){dato=0}
  valor=valor[dato]
  return valor
}

function QueEjercicio(dato){
  indice=document.selector.ejer.selectedIndex
  if(indice==0){return null}
  valor=document.selector.ejer[indice].value
  valor=valor.split("|")
  if(!dato){dato=0}
  valor=valor[dato]
  return valor
}

function QueVideo(dato){
  indice=document.selector.nvid.selectedIndex
  if(indice==0){return null}
  valor=document.selector.nvid[indice].value
  valor=valor.split("|")
  if(!dato){dato=0}
  valor=valor[dato]
  return valor
}

function CambiaArea(){
  LimpiaEjercicios()
  CargaEjercicios()
  LimpiaVideos()
  CargaVideos()
  AccesoListas()
  MuestraVisor()
  MuestraComentario()
  MuestraEnlaceTienda()
}

function CambiaEjercicio(){
  LimpiaVideos()
  CargaVideos()
  AccesoListas()
  MuestraVisor()
  MuestraComentario()
  MuestraEnlaceTienda()
}

function CambiaVideo(){
  if(QueVideo()==null)
  {MuestraVisor();MuestraComentario();return}

  qvid=QueVideo(1)
  fvid=TablaVT[qvid].fuente
  cvid=TablaVT[qvid].video
  MuestraVisor(fvid,cvid)

  area=QueArea()
  ejer=QueEjercicio()
  nvid=QueVideo(0)
  MuestraComentario(area,ejer,nvid)
}

function LimpiaEjercicios(){
  for(x=document.selector.ejer.length;x>=0;x--)
  {document.selector.ejer[x]=null}
}

function LimpiaVideos(){
  for(x=document.selector.nvid.length;x>=0;x--)
  {document.selector.nvid[x]=null}
}

function AccesoListas(){
  document.selector.ejer.disabled=(QueArea()?false:true)
  document.selector.nvid.disabled=(QueEjercicio()?false:true)
}

function MuestraMensaje(texto){
  document.getElementById("mensaje").innerHTML=texto
}

function MuestraTotalVideos(){
  total=TablaVT.length-1
  texto="Localizados "+total+" vídeos comentados de táctica de fútbol"
  MuestraMensaje(texto)
}

function MuestraVisor(fuente,codvid){
  if(fuente && codvid)
  {param="?f="+fuente+"&c="+codvid}
  else
  {param=""}

  document.getElementById("visor").src="visor.html"+param
}

function MuestraComentario(area,ejer,nvid){
  if(area && ejer && nvid)
  {archcoment=area+"/"+ejer+"-"+nvid+".html"}
  else
  {archcoment="comentario.html"}

  document.getElementById("comentario").src=archcoment
}

function MuestraEnlaceTienda(){
  area=QueArea()
  ejer=QueEjercicio()

  if(area)
  {
    nuarea=QueArea(1)
    tiarea=TablaAT[nuarea].titulo
    ctarea=TablaAT[nuarea].tienda
    lkarea="../tienda/index.php?cPath="+ctarea
    lkarea='<a href="'+lkarea+'">'+tiarea+'</a>'
  }
  else
  {
    nuarea=null;tiarea=null;ctarea=null;lkarea=null
  }

  if(ejer)
  {
    nuejer=QueEjercicio(1)
    tiejer=TablaET[nuejer].titulo
    ctejer=TablaET[nuejer].tienda
    lkejer="../tienda/product_info.php?products_id="+ctejer
    lkejer='<a href="'+lkejer+'">'+tiejer+'</a>'
  }
  else
  {
    nuejer=null;tiejer=null;ctejer=null;lkejer=null
  }

  if(ctarea>0 || ctejer>0)
  {
    texto='En el catálogo de '+'<a href="../tienda/index.php">nuestra tienda</a>'+' encontrará más información sobre<br>'
    if(ctarea>0){texto=texto+lkarea}
    if(ctarea>0 && ctejer>0){texto=texto+" y "}
    if(ctejer>0){texto=texto+lkejer}
  }
  else
  {texto="..."}

  document.getElementById("tienda").innerHTML=texto
}
