﻿// DOM Ready
$(function () {
    var images = new Array();
    $(".photo a").each(function () {
        images.push($(this).attr("href"));
    });
    var max = $(images).length;
    // at least 1 image exist
    if (max > 0) {
        // load the first image
        LoadImage(0, max);
    }
    // function of loading image
    // params: (int) index of image in array, (int) length of images array
    function LoadImage(index, max) {
        // if current index is lower then max element (max-1)
        if (index < max) {
            var curr = $(".photo a").eq(index);
            // new image object
            var img = new Image();
            // image onload
            $(img).load(function () {
                //$(this).parents().filter('div.photoholder').css('display', 'none'); // since .hide() failed in safari
                $(curr).append(this);
                $(this).parents().filter('div.photoholder').fadeIn(500, function () {
                    LoadImage(index + 1, max);
                });
            }).error(function () {
                // on error remove current
                $(curr).remove();
                // trigger the next image
                LoadImage(index + 1, max);
            }).attr('src', images[index]);
        }
    }
});


