var SLIDEcounter = 1;
var SLIDEnow = true;
var SLIDEinterval = 3000;

window.addEvent('domready',function() {
  /* preloading */
  var imagesDir = base + 'images/slideshow/';
  var images = ['slide1-1.jpg','slide1-2.jpg','slide1-3.jpg','slide1-4.jpg'];
  var holder = $('slideshow-1');
  
  images.each(function(img,i){ images[i] = imagesDir + '' + img; }); //add dir to images
  var progressTemplate = 'Loading image {x} of ' + images.length;
  var updateProgress = function(num) {
    progress.set('text',progressTemplate.replace('{x}',num));
  };    
  var progress = $('progress');
  updateProgress('text','0');  
  var loader = new Asset.images(images, {
    onProgress: function(c,index) {
      updateProgress('text',index + 1);
    },
    onComplete: function() {
      var slides = [];
      /* put images into page */
      images.each(function(im) {
        slides.push(new Element('img',{
          src:im,
          width: 231,
          height: 200,
          styles: {
            opacity:0,
            top:0,
            left:0,
            position:'absolute',
            'z-index': 10
          }
        }).inject(holder));
      });
      var showInterval = SLIDEinterval;
      var index = 0;
      progress.set('text','Načítám obrázky.');
      (function() {slides[index].tween('opacity',1); }).delay(1000);
      var start = function() {
        (function() {
         if(SLIDEcounter == 1 && SLIDEnow == true) {
          holder.setStyle('background','');
          slides[index].fade(0);
          ++index;
          index = (slides[index] ? index : 0);
          slides[index].fade(1);
          SLIDEcounter = 2;
          SLIDEnow = false;
          }
        
        }).periodical(showInterval);
      };

      /* start the show */
      start();
    }
  });
});

window.addEvent('domready',function() {
  /* preloading */
  var imagesDir = base + 'images/slideshow/';
  var images = ['slide2-1.jpg','slide2-2.jpg','slide2-3.jpg','slide2-4.jpg'];
  var holder = $('slideshow-2');
  
  images.each(function(img,i){ images[i] = imagesDir + '' + img; }); //add dir to images
  var progressTemplate = 'Loading image {x} of ' + images.length;
  var updateProgress = function(num) {
    progress.set('text',progressTemplate.replace('{x}',num));
  };    
  var progress = $('progress');
  updateProgress('text','0');  
  var loader = new Asset.images(images, {
    onProgress: function(c,index) {
      updateProgress('text',index + 1);
    },
    onComplete: function() {
      var slides = [];
      /* put images into page */
      images.each(function(im) {
        slides.push(new Element('img',{
          src:im,
          width: 231,
          height: 200,
          styles: {
            opacity:0,
            top:0,
            left:0,
            position:'absolute',
            'z-index': 10
          }
        }).inject(holder));
      });
      var showInterval = SLIDEinterval;
      var index = 0;
      progress.set('text','Načítám obrázky.');
      (function() {slides[index].tween('opacity',1); }).delay(1000);
      var start = function() {
        (function() {
          if(SLIDEcounter == 2 && SLIDEnow == true) {
          holder.setStyle('background','');
          slides[index].fade(0);
          ++index;
          index = (slides[index] ? index : 0);
          slides[index].fade(1);
          SLIDEcounter = 3;
          SLIDEnow = false;
          }
        }).periodical(showInterval);
      };

      /* start the show */
      start();
    }
  });
});

window.addEvent('domready',function() {
  /* preloading */
  var imagesDir = base + 'images/slideshow/';
  var images = ['slide3-1.jpg','slide3-2.jpg','slide3-3.jpg','slide3-4.jpg'];
  var holder = $('slideshow-3');
  
  images.each(function(img,i){ images[i] = imagesDir + '' + img; }); //add dir to images
  var progressTemplate = 'Loading image {x} of ' + images.length;
  var updateProgress = function(num) {
    progress.set('text',progressTemplate.replace('{x}',num));
  };    
  var progress = $('progress');
  updateProgress('text','0');  
  var loader = new Asset.images(images, {
    onProgress: function(c,index) {
      updateProgress('text',index + 1);
    },
    onComplete: function() {
      var slides = [];
      /* put images into page */
      images.each(function(im) {
        slides.push(new Element('img',{
          src:im,
          width: 231,
          height: 200,
          styles: {
            opacity:0,
            top:0,
            left:0,
            position:'absolute',
            'z-index': 10
          }
        }).inject(holder));
      });
      var showInterval = SLIDEinterval;
      var index = 0;
      progress.set('text','Načítám obrázky.');
      (function() {slides[index].tween('opacity',1); }).delay(1000);
      var start = function() {
        (function() {
        if(SLIDEcounter == 3 && SLIDEnow == true) {
          holder.setStyle('background','');
          slides[index].fade(0);
          ++index;
          index = (slides[index] ? index : 0);
          slides[index].fade(1);
          SLIDEcounter = 1;
          SLIDEnow = false;
          }
          SLIDEnow = true;
          
        }).periodical(showInterval);
      };

      /* start the show */
      start();
    }
  });
});
