function loadSlide(el)
{
  if(!el) return;
  $this = $j(el);
  var pic = $j('<img class="picture" />');
      pic.load(function(){
        loadSlide($j('#home-slideshow a.picture').get(0));
      });
      pic.attr('src', $this.attr('href')); 
      $this.replaceWith(pic);
}



var required_flash = '9.0.0';

if(swfobject.hasFlashPlayerVersion(required_flash))
swfobject.embedSWF('/media/Flash/home.swf', 'home-slideshow', 818, 630, '9.0.0', null, {galleryDir: '/media/Flash/pics/'}, { wmode: 'opaque', scale: 'noscale', salign: 'lt' });
else
$j(function(){
  // queue loading
  loadSlide($j('#home-slideshow a.picture').get(0));
  
  // start slideshow
  HomeSlideshow.start();
});

var HomeSlideshow = {

  currentIndex: 0,
  timeout: null,
  start: function() {
    clearTimeout(this.timeout);
    this.timeout = setTimeout("HomeSlideshow.next()", 3000);
  },
  next: function()
  {
    clearTimeout(this.timeout);
    var current = $j(this.getCurrent());
    // check if next slide loaded
    var next = $j(current.is(':last-child') ? $j('#home-slideshow > ul > li').get(0) : current.next());
    var nextImage = next.children('img.picture');
    //if(nextImage) alert(nextImage[0].height);
    if(nextImage && nextImage[0].height > 100)
    {
      next.fadeIn();
      current.fadeOut();
      this.currentIndex = next.index();
      this.timeout = setTimeout("HomeSlideshow.next()", 5000);
    } else {
      //alert('postpone');
      this.timeout = setTimeout("HomeSlideshow.next()", 500);
    }
  
  },
  
  getCurrent: function()
  {
    var slide = $j('#home-slideshow > ul > li').get(this.currentIndex);
    return slide;
  }
}

