function resizeBackground(init){
    var winW=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth;

    //document.getElementById('image_wrapper').style.width=(730+Math.round((winW-940)/2))+'px';

    var i=images.length; var j=i;
    if(i>0)do{
        if(images[j-i].nfIsLoaded){
            handleImg(images[j-i]);
            if(j==i && init) introFade();
        }else{
            if(j!=i || !init) images[j-i].onload=function(){handleImg(this);}
            else images[j-i].onload=function(){handleImg(this); introFade();}
        }
    }while(--i);
}

function handleImg(img){
    if(img!=images[imageIndex]) img.style.visibility='hidden';
    img.style.display='block';
    img.style.width='auto';
    img.style.height='auto';
    img.nfIsLoaded=true;

    var w1=img.offsetWidth; var w2=document.getElementById('image_wrapper').offsetWidth;
    var h1=img.offsetHeight; var h2=document.getElementById('image_wrapper').offsetHeight;

    if(w2/w1>=h2/h1){
        img.style.width=w2+'px';
        img.style.height=(h1/w1*w2)+'px';
    }else{
        img.style.width=(w1/h1*h2)+'px';
        img.style.height=h2+'px';
    }

    if(img!=images[imageIndex]){
        img.style.display='none';
        img.style.visibility='visible';
    }
}

function introFade(){
    if(images.length>1) setInterval(fadeImg,5000);
    images[0].style.display='block';
    images[0].style.zIndex='2';
    new Effect(function(p){images[0].style.opacity=p; images[0].style.filter='alpha(opacity='+Math.round(p*100)+')'; if(p==1){images[0].style.zIndex='1';}},{duration:0.8, ease:false});
}

function fadeImg(){
    imageIndex++;
    if(imageIndex>=images.length) imageIndex=0;

    images[imageIndex].style.display='block';
    images[imageIndex].style.zIndex='2';
    new Effect(function(p){images[imageIndex].style.opacity=p; images[imageIndex].style.filter='alpha(opacity='+Math.round(p*100)+')'; if(p==1){images[lastIndex].style.opacity='0'; images[lastIndex].style.filter='alpha(opacity=0)'; images[lastIndex].style.display='none'; images[lastIndex].style.zIndex='-1'; images[imageIndex].style.zIndex='1'; lastIndex=imageIndex;}},{duration:0.8, ease:false});
}

images=document.getElementById('image_wrapper').getElementsByTagName('img');
lastIndex=0;
imageIndex=0;

if(!document.all) resizeBackground(false);
window.onload=function(){resizeBackground(true);};
window.onresize=function(){resizeBackground(false);};
