function getSpeed(string) {
    var speed = string.split(' ').slice(-1).toString().split('-').slice(-1);
    return speed;
}

function calculateTimeout(currElement, nextElement, opts, isForward) {
    var index = opts.currSlide;
    var speed = getSpeed(slideContainer.children(".slide:eq(" + index + ")").attr('class'));
    return speed; 
}

function refreshPage() {
    $.blockUI({
        message:  '<h1>Updating Stowe Live Time...</h1>',
        fadeIn:  1000,
        css: {
            width: '30%',
            left: '35%', 
            backgroundColor: 'transparent',
            color: '#C70101',
            border: 'none'
        },
        overlayCSS:  { 
            backgroundColor: '#fff', 
            opacity: 1 
        }
    });

    // perform hard refresh
    $('#loader').fadeOut(800).remove();
    $.unblockUI
    window.setTimeout ("window.location = window.location.href.split('?')[0] + '?refresh=true'", 1000);

}

function timedOut() {
    // make ajax call to check for update trigger
    $.ajax({
        type: "POST",
        cache: false,
        url: "refresh.php",
        success: function(response){
            if (response == 2) {
                refreshPage();
            }
        }
    });

    id=window.setTimeout ('timedOut()', 15000);
}

function cancelTimeout() {
    // Cancel the timeout using the id returned from setTimeout()
    window.clearTimeout(id);
}

$(document).ready(function() {
    $('#loader').fadeIn(800);
    
    // begin refresh timer
    id=window.setTimeout ('timedOut()', 15000);

    slideContainer = $('#container');

    $(function() {
        slideContainer.cycle({
            speed:      '1000',
            delay:       5000,
            timeoutFn:  calculateTimeout
        });
    }); 

});