
 var pic;
 var timer;
 var second_pic;
 
 var pictureId;
 var slideshow_started;
 
 // Параметры управления временем эффекта. Полное время м/у слайдами( полный цикл ) = <что-то>*step_time + pause_time (мсек)
 //    мсек - миллисекунда ( = 0.01 секунды)  <что-то> - зависит от функции нарастания opacity 
 var step_opacity;  // На сколько процентов меняется прозрачность за одну итерацию
 var step_time;    // Пауза между итерациями (мсек)
 var pause_time; // Пауза между тем, как закончит проявляться очередной слайд, и начнёт проявляться следующий (мсек)
 var curr_step; 

 // Путь к слайдам
 var picture_dir;

 // Список слайдов
 var pictures;      
 // Статьи с текстом
 var content;
 var total_pictures;

var before;
var after;

 function changePicture() {

    if ( second_pic != 0) {
 
       ShowNextImage();
       return false;
    }
    if( timer != null ) clearTimeout(timer); 
    //pic = new Image();
    pic.src = picture_dir + pictures[pictureId];
    timer = setTimeout("checkLoading()", 100); 
 }

 function checkLoading() {
    if( timer != null ) clearTimeout(timer);
    if (pic.complete == true) {  
       step_opacity = 4;
       curr_step = step_time;
       HideContent(); 
       changeSlide();  
       if( after.src != pic.src ){  
         after.style.opacity = 0;
         after.style.filter = "alpha(opacity=0)";
         after.src = pic.src;  
        // after.style.topMargin = - Math.round( pic.height / 2 ) + "px";   
       }  
       timer = setTimeout("changeSlide()", step_time);

   } else {

       timer = setTimeout("checkLoading()", 1000);
   }
 }

 function changeSlide() {
   if( timer != null ) clearTimeout(timer);

/*   for( ;second_pic < 99; second_pic += step_opacity ){
     after.style.opacity = second_pic / 100;
     after.style.filter = "alpha(opacity=" + second_pic + ")";
   }
 
   before.src = pic.src;    
   //before.style.topMargin = - pic.height / 2;    
   after.style.opacity = 0;
   after.style.filter = "alpha(opacity=0)";      
   second_pic = 0;  
   ShowContent( pictureId );
   if (slideshow_started == true) {
       timer = setTimeout("ShowNextImage()", pause_time );
   } 
*/
   second_pic += step_opacity;

   after.style.opacity = second_pic / 100;
   after.style.filter = "alpha(opacity=" + second_pic + ")";

   if (second_pic > 99) {
      before.src = pic.src;    
      //before.style.topMargin = - pic.height / 2;    
      after.style.opacity = 0;
      after.style.filter = "alpha(opacity=0)";      
      second_pic = 0;  
      ShowContent( pictureId );
      if (slideshow_started == true) {
         timer = setTimeout("ShowNextImage()", pause_time );
      }

   } else {
     timer = setTimeout("changeSlide()", curr_step);
   }

 }

 function ShowNextImage() {
   pictureId = (pictureId + 1) % total_pictures;
   changePicture(); 
 } 

 function StartSlideShow() {
   if (slideshow_started == false) {

      slideshow_started = true;
      ShowNextImage();

   } else {

      slideshow_started = false;
   }
 }

// Функция - при клике на картинку загружает следующий слайд, 
//   не дожидаясь конца эффекта (для нетерпиливых юзеров) 
// Для отключения этой возможности убрать "onclick=f_click();"  в index.html, в <img id="after"... 
 function f_click(){
   if (slideshow_started == true) {
     if( timer != null ) clearTimeout(timer);  
     after.style.opacity = 1;
     after.style.filter = "alpha(opacity=" + 100 + ")";
     before.src = pic.src;  
     //before.style.topMargin = - pic.height / 2;   
     after.style.opacity = 0;
     after.style.filter = "alpha(opacity=0)";      
     second_pic = 0; 
     ShowContent( pictureId ); 
     ShowNextImage();
   }else StartSlideShow(); 
}   

function ShowContent( _id ){
  for( i = 0; i < content.length; i++ ){
    if( i == _id ){
       content[ i ].style.display = "block";
    }else content[ i ].style.display = "none";   
  }
}

function HideContent(){
  for( i = 0; i < content.length; i++ ){
    content[ i ].style.display = "none";   
  }
}


 function findPos(){
  var a = Number( after.height / 2 );
  after.style.marginTop = -a + "px";  
  before.style.marginTop = -a + "px";

}

function f_onload(){
  pic = new Image();
  second_pic = 0;
 
  pictureId = 0;
  slideshow_started = false;
  step_time = 1;        // Пауза между итерациями (мсек)
  curr_step = step_time;
  pause_time = 5000;     // Пауза между тем, как закончит проявляться очередной слайд, и начнёт проявляться следующий (мсек)
  picture_dir = "/img/";
  // Список слайдов
  pictures = new Array('a1.jpg', 'a1.jpg', 'a3.jpg', 'a4.jpg', 'a14.jpg', 'a5.jpg', 'a6.jpg', 'a2.jpg', 'a7.jpg', 'a8.jpg', 'a9.jpg', 'a10.jpg', 'a11.jpg', 'a12.jpg');  
   // Статьи с текстом    
  content = new Array( document.getElementById("a1"),
                       document.getElementById("a1"),
                       document.getElementById("a3"),
                       document.getElementById("a4"),
                       document.getElementById("a14"),
                       document.getElementById("a5"),
                       document.getElementById("a6"),
                       document.getElementById("a2"),
                       document.getElementById("a7"),
                       document.getElementById("a8"),
                       document.getElementById("a9"),
                       document.getElementById("a10"),
                       document.getElementById("a11"),
                       document.getElementById("a12") );
  total_pictures = pictures.length; 
  before = document.getElementById("before");
  after = document.getElementById("after"); 
  
  WinResize();     
  after.style.opacity = 1;
  after.style.filter = "alpha(opacity=100)";  
  before.style.opacity = 1;
  before.style.filter = "alpha(opacity=100)";
  StartSlideShow();    
}   
        
function WinResize() {
  findPos();
}

