var gio = 0;
function change_image(rootdir, to_pg) {
  working = true;
  var fade_interval = 50;
  var gi, gi2, gi_img1, gi_img2, pager;
  gi = document.getElementById('galery_image');
  gi2 = document.getElementById('galery_image2');
  gi_img1 = gi.firstChild;

  if (gio == 0 && gi2.childNodes.length == 0) {
    hide_description();
    var result = '';
    try {
      // Moz supports XMLHttpRequest. IE uses ActiveX. 
      // browser detction is bad. object detection works for any browser
      xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
      xmlhttp.open('GET', rootdir + 'common/def/get_image.php?galery_fader_pg=' + to_pg + '&rnd=' + Math.random(), false);
      xmlhttp.send(null);
      if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
        result = unescape(xmlhttp.responseText);
      }
    } catch (e) {
     // browser doesn't support ajax. handle however you want
    }
    result = result.split("\n");
    image_id = result[0];
    // return result;

    gi_img2 = gi_img1.cloneNode(false);
    gi_img2.src = rootdir + result[1];
    gi_img2.width = result[2];
    gi_img2.height = result[3];
    gi_img2.style.filter = 'alpha(opacity=' + (gio * 100) + ',finishopacity=' + (gio * 100) + ',style=0)';
    gi_img2.style.opacity = gio;
    gi2.appendChild(gi_img2);
  } else {
    gi_img2 = gi2.firstChild;
  }

  if (gio >= 1) {
    gi_img2.style.filter = 'alpha(opacity=100,finishopacity=100,style=0)';
    gi_img2.style.opacity = 1;
    gi.removeChild(gi_img1);
    gi_img1 = gi2.removeChild(gi_img2);
    gi.appendChild(gi_img1);
    gio = 0;
    pg = to_pg;
    pager = document.getElementById('pager');
    pager.innerHTML = (1*pg + 1*1) + '/' + (1*max_pg+1*1);
    pager = document.getElementById('galery_fader_pager');
    pager.style.width = gi_img1.clientWidth + 'px';
    working = false;
  } else {
    gio = gio + 0.05;
    gi_img2.style.filter = 'alpha(opacity=' + (gio * 100) + ',finishopacity=' + (gio * 100) + ',style=0)';
    gi_img2.style.opacity = gio;
    gi_img1.style.filter = 'alpha(opacity=' + ((1 - gio) * 100) + ',finishopacity=' + ((1 - gio) * 100) + ',style=0)';
    gi_img1.style.opacity = 1 - gio;
    setTimeout('change_image("' + rootdir + '", "' + to_pg + '")',fade_interval);
  }

}
