// requires library.js.php (for addMyLoadEvent), 
//  unless jQuery present

/* 

after loading, replace all pictures in page with names
containing '\d+_s' with their hires counterparts (ie same name 
with _s removed) in the background.

*/

var hImgs =[];

function _imgLoader(_src) {
  var _img= new Image; _img.src= _src; return _img;
}

function _loadImg(ix,aux) {
  if (typeof aux=='string') {
    hImgs[ix]= new _imgLoader(aux);
    aux= 0;
  }
  
  if (aux > 20)
    return;  // give up after ca. 10 sec
  else if (hImgs[ix].complete) // done
    document.images[ix].src= hImgs[ix].src;
  else
    setTimeout('_loadImg('+ix+','+(++aux)+')',500);
}

function _loadHiresImages(_n1,_n2,_n3) {
  if (window.COOLjsMenuPRO) {  // dynamic loading of more images after page has loaded -> image indexes change!
/* DOES NOT WORK.
    menus= _getInstances('navigation');
    menus= window._CMenus;
    // wait until all images have stopped loading
    for (im= 0; im < menus.length; im++)
      if (!menus[im]._isCompletelyLoaded) {
        setTimeout('_loadHiresImages()',1000);
        alert('bonk! mx='+im+'/'+menus.length);
        return;
      }
*/
    if (!(
        _n1!=undefined && _n2!=undefined && _n3!=undefined
     && _n1==_n2 && _n1==_n3 && _n2==_n3
    )) {
      ns= _n2+','+_n3+','+document.images.length;
      setTimeout('_loadHiresImages('+ns+')',1000);
      return;
    }  
  } 
      
  for (ix= 0; ix < document.images.length; ix++)
    if (sp= document.images[ix].src.match(/^(.+?\/\d+)_s\.(.*?)$/)) {
      _loadImg(ix,sp[1]+'.'+sp[2]);
    }
}

if (typeof jQuery != 'undefined')
  $(document).ready(_loadHiresImages);
else
  /*library.js.php;;*/window.addMyLoadEvent(_loadHiresImages);
