var currentImageTag=1;
var imageIndex=0;
function imageChanger(){
      var interval;
      var image=document.images['i'+currentImageTag];
      if(!image){return;}
      image.style.opacity=1;
      var fadeDirection=0;
      var fader=function(){
        if( fadeDirection==0 && image.style.opacity<=0 && !image.waitingForImageLoad){ // finished fading out
	  image.waitingForImageLoad=true;
	  if(!image.onload){
		image.onload=function(){ fadeDirection=1; image.waitingForImageLoad=false; clearInterval(interval); interval=setInterval( fader, 50 ); }
	  }
           if( imageIndex && imageIndex>imageList.length-1){imageIndex=0;}
          image.src= (typeof imageList!='undefined') ? imageList[imageIndex++] : '/img/rnd.php?'+Math.random();
          clearInterval(interval);
        }else{ //still fading
          image.style.opacity = (fadeDirection==0) ? parseFloat(image.style.opacity)-.2 : parseFloat(image.style.opacity)+.2;
          if(document.body && document.body.filters){
            image.style.filter = 'alpha(opacity='+(image.style.opacity*100)+');';
          }
          if( fadeDirection==1 && image.style.opacity>=.99 ){ // finished fading in
          fadeDirection=0;
          clearInterval(interval);
	  setTimeout(imageChanger, 4000);
          }
        }
        
      }
      
      var interval=setInterval( fader, 50 );
      currentImageTag++;
      if( ! document.images['i'+currentImageTag] ){currentImageTag=1};
  }
  
  function initByWindowSize(){
	var removethese=new Array(),img;
	if(screen.width<1024){
		removethese.push(document.images['i3']);
	}
	if(screen.width<800){
		removethese.push(document.images['i2']);
	}
	for(var i=0;i<removethese.length;i++){
		img=removethese[i];
		if( img && img.parentNode && img.parentNode.removeChild ){
			img.parentNode.removeChild(img);
		}else if(img && img.style){
			img.style.display='none';
			img.style.visibility='hidden';
			img.style.visibility='hide';
		}
	}
  }
if(document.images){
	setTimeout( initByWindowSize, 100 );
	setTimeout(imageChanger, 5000);
}
