var NPhoto;
var imgSuffix;

var dK = 1;


var PathPhoto;
var SlideImage;
 
var time_1, time_2;
var iStart, iCur, iNext;
var dAlpha;

var SlideWidth = 0;
var SlideHeight = 0;

//-------------------------------------------------------------------------
//                    ChangePhoto
//-------------------------------------------------------------------------
function ChangePhoto(CountPhoto, PhotoSuffix)
{
NPhoto = CountPhoto;
imgSuffix = PhotoSuffix;

PrepareSlides();
//AllocateSlide();

for(var i=0; i<NPhoto-2; i++) {
    var ii = i+iCur+2;
    if(!(ii < NPhoto))
  	    ii -= NPhoto;
    SlideImage[ii].src = PathPhoto[ii];
 }	

Photo1.src = SlideImage[iCur].src;

time_1 = setInterval("MultPhoto()",2000);
}

//----------------------------------------------------------------------------
//              Prepare
//----------------------------------------------------------------------------
function PrepareSlides()
{
 PathPhoto = new Array(NPhoto);
 SlideImage = new Array(NPhoto);

 for(var i=0; i<NPhoto; i++) {
	 PathPhoto[i] = imgSuffix + i + ".jpg";
	 SlideImage[i] = new Image();
  }	
   
 iStart = Math.round(Math.random()*(NPhoto-1));
 iCur = iStart;
 iNext = 1;
 dAlpha = 0;
 

 SlideImage[iCur].src = PathPhoto[iCur];

 if(iCur==NPhoto-1)
    SlideImage[0].src = PathPhoto[0];
 else   
    SlideImage[iCur+1].src = PathPhoto[iCur+1];

} 
  
//-------------------------------------------------------------------------
//                    AllocateSlide
//-------------------------------------------------------------------------
function AllocateSlide()
{
 Photo1.width = SlideWidth*dK;
 Photo1.height = SlideHeight*dK;
 Photo2.width = SlideWidth*dK;
 Photo2.height = SlideHeight*dK;

}
 
 //-------------------------------------------------------------------------
//                    SlidesLoaded
//-------------------------------------------------------------------------
function StartSlideLoaded()
{
 if(SlideWidth != 0) 
    return true;

 if(!SlideImage[iStart].complete) 
     return false;
	 
 SlideWidth = SlideImage[iStart].width;
 SlideHeight = SlideImage[iStart].height;

 Photo1.width = SlideWidth*dK;
 Photo1.height = SlideHeight*dK;

 Photo2.width = SlideWidth*dK;
 Photo2.height = SlideHeight*dK;
 
 return true;  
}
   
//-------------------------------------------------------------------------
//                    MultPhoto
//-------------------------------------------------------------------------
function MultPhoto()
{
 if(!StartSlideLoaded()) return;
 

 if(iCur < NPhoto) {
    if(iCur==NPhoto-1)
       iNext = 0;
    else 
       iNext = iCur + 1;
  }
 else {
   iCur = 0;
   iNext = 1;
  }


if(dAlpha==0) {
   Photo1.src = SlideImage[iCur].src;
   Photo2.src = SlideImage[iNext].src;

   if(!SlideImage[iCur].complete) 
       return;
   if(!SlideImage[iNext].complete)
       return;

   dAlpha = 1;
   time_2 = setInterval("Effect()",1);
   iCur++;
 }  
   
}

//-------------------------------------------------------------------------
//                    Effect
//-------------------------------------------------------------------------
function Effect()
{
if(dAlpha==1)
   clearInterval(time_1);

if(dAlpha < 100) {
   Photo2.filters[0].opacity = dAlpha;
   dAlpha += 2;
 }  
else {
   Photo1.src = SlideImage[iNext].src;
   Photo2.filters[0].opacity = 0;
   clearInterval(time_2);
   dAlpha = 0;
   time_1 = setInterval("MultPhoto()",2000);
 }

}

//-------------------------------------------------------------------------
//                    Tstop
//-------------------------------------------------------------------------
function UnloadPhoto()
{
 clearInterval(time_1);
}

