window.onload= function () {
  
  window.onload= null;
  
  //Borra lo de que esta página requiere JavaScript
  var contenedor= document.getElementById("contenedor");
  var chivatoJS= document.getElementById("chivatoJS");
  if (chivatoJS) {
    chivatoJS.parentNode.removeChild(chivatoJS);
    chivatoJS= null;
  }
  var bottomImg= document.getElementById("bottomImg");
  if (bottomImg) {
    bottomImg.parentNode.removeChild(bottomImg);
  }
  
  var isIE= false /*@cc_on || true @*/;
  var isIE6= isIE /*@cc_on && !!(!window.XMLHttpRequest && document.compatMode) @*/;
  if (isIE6) {
    document.getElementById("titulo").style.width= contenedor.style.width= "640px";
  }

  function noPropagar (e) {
    e= e || event;
    if (window.event) {
      e.cancelBubble= true;
    } else {
      e.stopPropagation();
    }
  }

  function captureEvents () { return !1; }
  
  document.body.onselectstart = captureEvents;
  document.body.onselect = captureEvents;
  document.body.ondblclick = captureEvents;
  document.body.onmousedown = captureEvents;
    
  //datos se rellena con el script datos.js desde el .html
  var flipFlop;
  while (datos.length) {
    var dato= datos.shift();
    if (typeof dato !== "object") { break; }
    var imagen= document.createElement('img');
    imagen.src= dato.imagenes.length ? dato.imagenes[0] : "comunes/img/noFoto.jpg";
    imagen.className= "imgDato "+ (flipFlop ? "imgBorde1" : "imgBorde2")+ ((dato.imagenes.length > 1) ? " masFotos" : "");

    var titulo= document.createElement('h1');
    titulo.innerHTML= dato.tituloTxt;
    titulo.className= "tituloDato";
    var textoCorto= document.createElement('p');
    textoCorto.innerHTML= dato.cortoTxt;
    textoCorto.className= "textoCortoDato";
    var textoLargo= document.createElement('p');
    textoLargo.innerHTML= dato.largoTxt;
    textoLargo.className= "textoLargoDato";
    var pvp= document.createElement('p');
    pvp.innerHTML= "PVP "+ dato.pvp;
    pvp.className= "pvpDato";
    var ppal= document.createElement('p');
    ppal.className= "ppalDato";
    ppal.innerHTML= '<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="'+ dato.ppal +'"><input type="image" style="width:96px;" src="comunes/img/buyNow.gif" name="submit" alt=""><img alt="" border="0" src="https://www.paypal.com/es_ES/i/scr/pixel.gif" width="1" height="1"></form>';
    ppal.onclick= noPropagar;

    var linea= contenedor.appendChild(document.createElement('div'));
    linea.className= flipFlop ? "linea1" : "linea2";
    linea.appendChild(imagen);
    linea.appendChild(titulo);
    linea.appendChild(textoCorto);

    linea.onclick= (function () {
      var li= linea, corto= textoCorto, largo= textoLargo;
      var flipFlop, i= imagen, steps= [], stepsSave= [];
      var pPal= ppal, pVp= pvp, imagenes= dato.imagenes, currentImg= 0;
      
      if (isIE6) {
        li.style.height= "80px";
      }
      
      function zoomImg (sentido, f) {
        if (steps.length) {
          i.style.width= i.style.height= (sentido ? steps.shift() : steps.pop())+ "px";
        }
        
        if (steps.length) {
          setTimeout(function () { zoomImg(sentido, f); }, 33);
        } else {
          var n= 0;
          while (n < stepsSave.length) {
            steps[n]= stepsSave[n];
            n++;
          }
          f();
        }
      }

      function onClick (e) {
        e= e || event;
        
        flipFlop= !flipFlop;
        
        if (!stepsSave.length) {
          var imgWidth= 70;
          var target= 2* imgWidth;
          var diferencia, signo;
          steps[steps.length]= stepsSave[steps.length]= +imgWidth;
          while (diferencia= target- imgWidth) {
            signo= (diferencia < 0) ? -1 : 1;
            diferencia= Math.round(diferencia/2) || signo;
            steps[steps.length]= stepsSave[steps.length]= (imgWidth+= diferencia);
          }
        }
        
        if (flipFlop) {
          li.onclick= null;
          li.replaceChild(largo, corto);
          li.appendChild(pVp);
          li.appendChild(pPal);
          if (isIE6) {
            li.style.height= "";
          }
          zoomImg(1, function () {
            li.onclick= onClick;
            i.onclick= function (e) {
              if (imagenes.length) {
                if (++currentImg >= imagenes.length) {
                  currentImg= 0;
                }
                i.src= imagenes[currentImg];
              }
              noPropagar(e);
            };
          });
        } else {
          li.onclick= null;
          zoomImg(0, function () {
            li.replaceChild(corto, largo);
            pVp.parentNode.removeChild(pVp);
            pPal.parentNode.removeChild(pPal);
            if (isIE6) {
              li.style.height= "80px";
            }
            li.onclick= onClick;
            i.onclick= null;
          });
        }
        
        /*if (e.preventDefault) {
          e.preventDefault();
        } else {
          e.returnValue = false;
        }*/
      };
      return onClick;
    })();
    flipFlop= !flipFlop;
  }
  if (bottomImg) {
    contenedor.appendChild(bottomImg);
  }
  fixPNGs();
};
