
var categorias = [
  { titulo: "Obras",
    directorio: null,
    subcategorias: [ "Grabados", "Pintura sobre papel", "Pintura sobre lienzo","Portadas para Libros","Ilustraciones para Revistas" ],
    agotadas: [false, false, false]
  },
  { titulo: "Series",
    directorio: "series",
    subcategorias: ["Dioses Menores", "Dolce Vita", "Figuras y Modelos", "Fósiles del Siglo XX", "Guantánamo" ],
    agotadas: [true, true, false, false]
  }
]

var categoria = -1
var href = window.location.href
if (href.indexOf('?') != -1) {
  href = href.substring((href.indexOf('?')) + 1)
  categoria = href.substring((href.indexOf('=')) + 1)
}

var style = 0
function changeStyle() {
  document.styleSheets[style+1].disabled=true
  style = document.styleSheets.length == style+2 ? 0 : style+1
  document.styleSheets[style+1].disabled=false
}

function populateMenu(isGallery) {
  if (isGallery && categoria == -1) {
    categoria = 0
  }
  for (i=0, k=0; i<categorias.length; i++) {
    document.writeln("<li>" + categorias[i].titulo + ":<ul class='submenu'>")
    for (j=0; j<categorias[i].subcategorias.length; j++, k++) {
      var agotada = categorias[i].agotadas[j]
      if (categoria == k) {
        document.writeln("<li class='current'>" + categorias[i].subcategorias[j] + "</li>")
      } else {
        document.writeln("<li><a href='obras.html?cat=" + k + "'>" + categorias[i].subcategorias[j] + "</a></li>")
      }
    }
    document.writeln("</ul></li>")
  }
}

function toFileName(name) {
  return name.replace(/\//,"-").replace(/¿/,"").replace(/\?/,"").replace(/ñ/,"n").replace(/á/,"a").replace(/é/,"e").replace(/í/,"i").replace(/ó/,"o").replace(/ú/,"u")
}

function populateGallery() {
  var nombreCategoria
  for (i=0, k=0; !nombreCategoria && i<categorias.length; i++) {
    for (j=0; !nombreCategoria && j<categorias[i].subcategorias.length; j++, k++) {
      if (categoria == k) {
        nombreCategoria = toFileName(categorias[i].subcategorias[j])
        if (categorias[i].directorio)
          nombreCategoria = categorias[i].directorio + "/" + nombreCategoria
      }
    }
  }
  for (i=0; i < obras[categoria].length; i++) {
    var ref = obras[categoria][i][0]
    var titulo = obras[categoria][i][1]
    if (!titulo)
      titulo = "Sin título"
    var dir = ref ? "obras/" : "obras/" + nombreCategoria + "/"
    var file = (ref ? ref : toFileName(titulo)) + ".jpg"
    var tecnica = obras[categoria][i][2]
    var medidas = obras[categoria][i][3]
    var precio = obras[categoria][i][4]
    var disponible = null
    var tirada = obras[categoria][i][5]
    var disponibles = obras[categoria][i][6]
    var info = obras[categoria][i][7]
    var estiloPrecio = "price"
    if (disponibles == 0) {
      estiloPrecio = "pricena"
      disponible = tirada == 1 ? "Vendido" : "Agotado"
    } else if (!precio) {
      estiloPrecio = "pricena"
      precio = "Consultar"
    }
    document.writeln("<div class='hr'></div>")
    document.writeln("<table><tr><td class='gallery' align='center'>")
    document.writeln("<a href='" + dir + file + "' target='_blank' class='img'><img class='gallery' src='" + dir + "sm/" + file + "'></a>")
    document.writeln("</td><td><p>")
    document.write("<em class='label'>Título:</em> <em>\"" + titulo + "\"</em><br>")
    if (medidas)
      document.writeln("<em class='label'>Medidas:</em> <em>" + medidas + "</em><br>")
    document.writeln("<em class='label'>Precio:</em> ")
    if (precio)
      document.writeln("<em class='" + estiloPrecio + "'>" + precio + "</em> ")
    if (disponible)
      document.writeln("<em class='na'>" + disponible + "</em>")
    document.writeln("<br>")
    document.writeln("</p><p>")
    document.writeln(tecnica + "<br>")
    if (tirada == -1 || tirada > 1) {
      if (tirada > 1) {
        document.write("Tirada de " + tirada + " ejemplares. ")
      }
      if (disponibles > 0) {
        if (disponibles == 1) {
          document.write("1 ejemplar disponible.")
        } else {
          document.write(disponibles + " ejemplares disponibles.")
        }
      }
      document.writeln("")
    }
    document.writeln("</p>")
    if (info) {
      document.writeln("<i>" + info + "</i>")
    }
    document.writeln("</td></tr></table>")
  } 
}
