var images = Array();
images_counter=0;

function image_loaded (img) {
    if(typeof img.complete !="undefined" && !img.complete) {
        return false;
    }
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }
    return true;
}

function slideShow () {
    images = $("img.slideshow-image");
    setTimeout("advanceSlideShow();", 5000);
}

function advanceSlideShow() {
    //alert(images_counter);
    new_images_counter = images_counter + 1;
    if (new_images_counter>images.length-1) {
        new_images_counter = 0;
    }
    if (image_loaded(images[new_images_counter])) {
        prev_images_counter = images_counter;
        images_counter = new_images_counter;
        images[prev_images_counter].style.zIndex=100;
        images[images_counter].style.zIndex=90;
        $(images[prev_images_counter]).fadeOut('slow');
        $(images[images_counter]).fadeIn('slow');
        images[prev_images_counter].zIndex=80;
    }
    setTimeout("advanceSlideShow();", 5000);
}

$(document).ready(function(){
    slideShow();
});