﻿// DOM Ready
$(function() {
    // remove top border
    $("table.news-container tr:last td").css("border", "none");

    var images = new Array();
    $("table.news-container td.news-image 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 = $("table.news-container td.news-image a").eq(index);
            // new image object
            var img = new Image();
            // image onload
            $(img).load(function() {
                $(this).css('display', 'none'); // since .hide() failed in safari
                $(curr).append(this);
                $(this).fadeIn('slow', 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]);
        }
    }
});


