TablaVG = new Array("#VG#v3#")
TablaYT = new Array("#YT#v3#")

function VG(datos){
  datos=datos.split("|")
  nv=datos[0]
  TablaVG[nv]=new DatosV(datos)
}

function YT(datos){
  datos=datos.split("|")
  nv=datos[0]
  TablaYT[nv]=new DatosV(datos)
}

function DatosV(datos){
  this.num=datos[0]
  this.cat=datos[1]
  this.nom=datos[2]
  this.pag=datos[3]
  this.vid=datos[4]
}

function CargaCategorias(){
  //lista de todas las categorías
  lista=new Array()
  sig=0
  for(video=1;video<TablaVG.length;video++)
  {
    if(!TablaVG[video]){continue}
    lista[sig]=TablaVG[video].cat
    sig=sig+1
  }
  for(video=1;video<TablaYT.length;video++)
  {
    if(!TablaYT[video]){continue}
    lista[sig]=TablaYT[video].cat
    sig=sig+1
  }

  //ordena categorías
  lista.sort()

  //select de categorías únicas
  cat=""
  sig=1
  for(elem=0;elem<lista.length;elem++)
  {
    if(lista[elem]!=cat)
    {
      cat=lista[elem]
      document.selector.cat[sig]=new Option(cat,cat)
      sig=sig+1
    }
  }
  document.selector.cat[0]=new Option("- Lista de Categorías ["+(sig-1)+"] -")
  document.selector.cat[0].selected=true
}

function CargaVideos(){
  //lista de vídeos de la categoría seleccionada
  cat=QueCategoria()
  lista=new Array()
  sig=0
  for(video=1;video<TablaVG.length;video++)
  {
    if(!TablaVG[video]){continue}
    if(TablaVG[video].cat==cat)
    {
      lista[sig]="vg|"+TablaVG[video].num
      sig=sig+1
    }
  }
  for(video=1;video<TablaYT.length;video++)
  {
    if(!TablaYT[video]){continue}
    if(TablaYT[video].cat==cat)
    {
      lista[sig]="yt|"+TablaYT[video].num
      sig=sig+1
    }
  }

  //select de vídeos
  sig=1
  for(elem=0;elem<lista.length;elem++)
  {
    datos=lista[elem].split("|")
    fuente=datos[0]
    numvid=datos[1]
    if(fuente=="vg"){nombre=TablaVG[numvid].nom}
    if(fuente=="yt"){nombre=TablaYT[numvid].nom}
    if(!nombre)
    {
      if(fuente=="vg"){fuente="Google Video"}
      if(fuente=="yt"){fuente="YouTube"}
      nombre=fuente+" #"+numvid
    }
    document.selector.vid[sig]=new Option(nombre,lista[elem])
    sig=sig+1
  }
  document.selector.vid[0]=new Option("- Lista de Vídeos ["+(sig-1)+"] -")
  document.selector.vid[0].selected=true
}

function CambiaCategoria(){
  LimpiaVideos()
  CargaVideos()
  AccesoListas()
  MuestraVisor()
  MuestraEnlaces()
}

function CambiaVideo(){
  if(QueVideo()==null)
  {MuestraVisor();MuestraEnlaces();return}

  qvideo=QueVideo()
  qvideo=qvideo.split("|")
  fuente=qvideo[0]
  numvid=qvideo[1]

  if(fuente=="vg"){codvid=TablaVG[numvid].vid}
  if(fuente=="yt"){codvid=TablaYT[numvid].vid}

  MuestraVisor(fuente,codvid)
  MuestraEnlaces(fuente,numvid)
}

function LimpiaCategorias(){
  for(x=document.selector.cat.length;x>=0;x--)
  {document.selector.cat[x]=null}
}

function LimpiaVideos(){
  for(x=document.selector.vid.length;x>=0;x--)
  {document.selector.vid[x]=null}
}

function QueCategoria(){
  indice=document.selector.cat.selectedIndex
  if(indice==0){return null}
  valor=document.selector.cat[indice].value
  return valor
}

function QueVideo(){
  indice=document.selector.vid.selectedIndex
  if(indice==0){return null}
  valor=document.selector.vid[indice].value
  return valor
}

function AccesoListas(){
  document.selector.vid.disabled=(QueCategoria()?false:true)
}

function MuestraVisor(fuente,codvid){
  if(fuente && codvid)
  {param="?f="+fuente+"&c="+codvid}
  else
  {param=""}

  document.getElementById("visor").src="visor.html"+param
}

function TotalVideos(lista){
  videos=0
  for(video=1;video<lista.length;video++)
  {if(lista[video]){videos=videos+1}}
  return videos
}

function MuestraTotalVideos(){
  total=TotalVideos(TablaVG)+TotalVideos(TablaYT)
  texto="Localizados "+total+" vídeos de fútbol"
  MuestraMensaje(texto)
}

function MuestraMensaje(texto){
  document.getElementById("mensaje").innerHTML=texto
}

function Aleatorio(){
  tabla=Math.floor(Math.random()*2)+1

  if(tabla==1){fuente="vg";videos=TablaVG.length-1}  
  if(tabla==2){fuente="yt";videos=TablaYT.length-1}

  numvid=Math.floor(Math.random()*videos)+1

  if(fuente=="vg"){if(!TablaVG[numvid]){Aleatorio();return}}
  if(fuente=="yt"){if(!TablaYT[numvid]){Aleatorio();return}}

  SeleccionaVideo(fuente,numvid)
}

function SeleccionaVideo(fuente,numvid){
  if(fuente=="vg"){catvid=TablaVG[numvid].cat}
  if(fuente=="yt"){catvid=TablaYT[numvid].cat}

  largoselect=document.selector.cat.length
  for(indice=1;indice<=largoselect;indice++)
  {
    if(document.selector.cat[indice].value==catvid)
    {
      document.selector.cat.options[indice].selected=true
      break
    }
  }

  LimpiaVideos()
  CargaVideos()

  fynvid=fuente+"|"+numvid

  largoselect=document.selector.vid.length
  for(indice=1;indice<=largoselect;indice++)
  {
    if(document.selector.vid[indice].value==fynvid)
    {
      document.selector.vid.options[indice].selected=true
      break
    }
  }

  AccesoListas()
  CambiaVideo()
}

function MuestraEnlaces(fuente,numvid){
  if(!fuente || !numvid)
  {document.getElementById("enlaces").innerHTML="...";return}

  if(fuente=="vg")
  {
    codpag=TablaVG[numvid].pag
    codvid=TablaVG[numvid].vid
    urlpag="http://video.google.com/videoplay?docid="+codpag
    txtpag="Encontrado en Google Video"
    //urldes="http://vp.video.google.com/videodownload?version=0&secureurl="+codvid
    //txtdes="Descargar .FLV"
    urldes="javascript:VideoDownLoader('"+urlpag+"')"
    txtdes="Descargar"
  }
  if(fuente=="yt")
  {
    codpag=TablaYT[numvid].pag
    codvid=TablaYT[numvid].vid
    urlpag="http://www.youtube.com/watch?v="+codpag
    txtpag="Encontrado en YouTube"
    //urldes="http://youtube.com/get_video?video_id="+codvid
    //urldes="javascript:alert('Descarga no disponible')"
    //txtdes="Descargar (renombrar a .FLV)"
    urldes="javascript:VideoDownLoader('"+urlpag+"')"
    txtdes="Descargar"
  }

  lnkpag='<a target="_blank" href="'+urlpag+'">'+txtpag+'</a>'
  lnkdes='<a href="'+urldes+'">'+txtdes+'</a>'
  lnkvid='Enlace... http://www.tacticasdefutbol.com/video.html?v='+fuente+numvid
  links=lnkpag+" - "+lnkdes+"<br><br>"+lnkvid
  document.getElementById("enlaces").innerHTML=links
}

function VideoDownLoader(urlpag)
{
  // http://videodownloader.net/get/?url=+urlpag
  urldes="http://www.flashload.net/?url="+urlpag
  window.open(urldes,"VideoDownLoader","width=680,height=620")
}

function MiraSiVerVideo(){
  if(!document.location.search){return}

  datos=document.location.search
  datos=datos.toLowerCase()
  fuente=datos.substr(3,2)
  numvid=datos.substr(5)

  if(fuente=="vg" || fuente=="yt"){okfuente=true}else{okfuente=false}

  if(fuente=="vg"){if(TablaVG[numvid]){okvideo=true}else{okvideo=false}}
  if(fuente=="yt"){if(TablaYT[numvid]){okvideo=true}else{okvideo=false}}

  if(!okfuente || !okvideo)
  {alert("Parámetros incorrectos ["+datos+"]");return}

  SeleccionaVideo(fuente,numvid)
}
