

$(document).ready(function () {
   loadSlideshow()
});


$(window).resize(function () {
    resizeImages()
});


function loadSlideshow() {
    if ($('#slideshowContainer').length > 0) {
        $("#slideshowContainer").load('/ajax/slide.aspx?idPage=' + $('#slideshowContainer').attr('idPage'), function () {
            callbackFunctions(actions);
        });
    }
}

function callbackFunctions(callback) {
            callback(resizeImages(), loadImages());
}

function loadImages() {

    //loads & shows the first image
    if ($('#img1').length > 0) {

        $('#img1').attr('src', $("#img1Src").attr("src")).load(function () {
            $('#slideshow #img1').css("position", "absolute").css("top", "0px").css("left", "0px").css("z-index", "5").css("opacity", "1");
            $('#loader').fadeOut('fast', function () { $('#slideshow, .navigation').fadeIn() })
        });

        //loads the other images and then starts the cycle
        $('#slideshow .cycleImages').each(function (index) {
            $(this).attr('src', $("#" + $(this).attr("id") + "Src").attr("src")).load(function () {
                if ($(this).attr("id") == $('#slideshow .cycleImages:last').attr("id")) {
                    $("#slideshowSrc").remove();
                    startCycle()
                }
            });
        });

    } else {
        $('#loader').hide()
    }
}

function actions(a, b) {
    resizeImages();
    loadImages();
}

function startCycle() {

    $('#slideshow').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 5000,
        sync: false,
        next: '.nxt',
        prev: '.prv',
        containerResize: false,
        slideResize: false

    });

    $('#prv, #nxt').click(function () {
        $('#slideshow').cycle('pause');
        if ($('#full').hasClass('open')) {
            $('.toHide').fadeOut(function () { $('#full').removeClass('open').addClass('close'); });

        }

    })

    $('#full').click(function () {
        if ($(this).hasClass('open')) {
            $('.toHide').fadeOut(function () { $('#full').removeClass('open').addClass('close'); });
        } else {
            $('.toHide').fadeIn(function () {
                $('#slideshow').cycle('resume');
                $('#full').removeClass('close').addClass('open');
            });
        }



    })

    //13, 102, 139
    if (($('#page_13').length > 0) || ($('#page_102').length > 0) || ($('#page_139').length > 0)) {
        $('#slideshow').cycle('pause');
        $('#full').hide();
        $('#subpages').removeClass('toHide');
    }



}

function resizeImages() {

    var wh = $(window).height();
    var ww = $(window).width();
    var ratio = 1.6;
    var imgw = wh * ratio;
    var fh;
    var fw;
    if (imgw <= ww) {
        fh = ww / ratio;
        fw = ww;
    } else {
        fh = wh;
        fw = imgw;
    }

    $('#slideshow img').height(fh).width(fw);
    $('#slideshow div').height(fh).width(fw);
    if (fw >= ww) { $('#slideshow').css('left', -(fw - ww) / 2).css('top', -(fh - wh) / 2) }

}
