function swapFrontImageAndText() {
    var $activeImg = $('#gallery .active');
    var $nextImg = ($('#gallery .active').next().length > 0) ? $('#gallery .active').next() : $('#gallery img:first');
    var $activeText = $('#caption .active');
    var $nextText = ($('#caption .active').next().length > 0) ? $('#caption .active').next() : $('#caption h1:first');
    var $activeSubtext = $('#subcaption .active');
    var $nextSubtext = ($('#subcaption .active').next().length > 0) ? $('#subcaption .active').next() : $('#subcaption span:first');

    $activeText.fadeOut(parseInt(jQuery.trim($('#fadespeed').html()))).removeClass('active');
    $activeSubtext.fadeOut(parseInt(jQuery.trim($('#fadespeed').html()))).removeClass('active');
    if (!(i % 3)) {
        $activeImg.fadeOut(parseInt(jQuery.trim($('#fadespeed').html()))).removeClass('active');
    }

    $nextText.fadeIn(parseInt(jQuery.trim($('#fadespeed').html()))).addClass('active');
    $nextSubtext.fadeIn(parseInt(jQuery.trim($('#fadespeed').html()))).addClass('active');
    if (!(i % 3)) {
        $nextImg.fadeIn(parseInt(jQuery.trim($('#fadespeed').html()))).addClass('active');
    }
    i++;
}

$(document).ready(function() {
    i = 0;
    swapFrontImageAndText();
    setInterval(swapFrontImageAndText, jQuery.trim($('#textspeed').html()));

});
