

$(document).ready(function () {
   loadSlideshow()
});


$(window).resize(function () {
    resizeImages()
});


function loadSlideshow() {
    if ($('#slideshowContainer').length > 0) {
        $("#slideshowContainer").load('/ajax/slide.aspx?idPage=' + $('#slideshowContainer').attr('idPage') + '&idLan=' + $('#slideshowContainer').attr('idLan'), function () {
            callbackFunctions(actions);
        });
    }
}

function callbackFunctions(callback) {
            callback(resizeImages(), loadImages());
}

function loadImages() {
    //loads & shows the first image
    $('#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('fast') })
    });

    //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()
            }
        });
    });
}

function actions(a, b) {
    a();
    b();
}

function startCycle() {
    $('#slideshow').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 4000,
        sync: true,
        next: '.nxt',
        prev: '.prv',
        after: onAfter,
        before: onBefore,
        containerResize: false,
        slideResize: false

    });
    $('.prv, .nxt').click(function () {
        $('#slideshow').cycle('pause');
        if ($('#top, #bottom, #footer').is(':visible')) {
            $('#top, #bottom, #footer').fadeOut("medium", "swing");
            $('#full').removeClass('open').addClass('close');
        }
    })

    $('#full').click(function () {
        if ($('#top, #bottom, #footer').is(':visible')) {
            $('#top, #bottom, #footer').fadeOut("medium", "swing");
            $('#full').removeClass('open').addClass('close');
        } else {
            $('#top, #bottom, #footer').fadeIn("medium", "swing");
            $('#full').removeClass('close').addClass('open');
        }
    })

}


function onAfter(curr, next, opts) {
    var uid = $(this).attr('uid');
    var cpt = $('#' + uid).attr('cpt');
    if (cpt.length > 0) {
        $('#capCycle').fadeIn('fast');
        $('#capCycle p').html(cpt);
    }

}

function onBefore(curr, next, opts) {
    $('#capCycle').fadeOut('fast', function () {
        $(this).find('p').empty();
    });
}


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;
    }

    $('.slide #slideshow img').height(fh).width(fw);
    $('.slide #slideshow div').height(fh).width(fw);
    if (fw >= ww) { $('.slide #slideshow').css('left', -(fw - ww) / 2).css('top', -(fh - wh) / 2) }

    $('.inner #slideshow img').width(fw);
}
