
$(document).ready(function() {

    if ($('#cycle').length > 0) {
        slideshow();
    }
})

function slideshow() {
    keyprs();
    $('#cycle').cycle({
        fx: 'scrollHorz',
        speed: 500,
        timeout: 4500,
        next: '#prv',
        prev: '#nxt',
        after: onAfter,
        before: onBefore
    });
}

function keyprs() {

    $("#nxt, #prv").click(function() {
        $('#cycle').cycle('pause');
    })

    $(document).keydown(function(e) {
        if (e.keyCode == 37) { //left
            $("#nxt").trigger('click').parent().addClass('trigged');
            return false;
        }
        if (e.keyCode == 39) { //right
            $("#prv").trigger('click').parent().addClass('trigged');
            return false;
        }
    });

    $(document).keyup(function(e) {
        if (e.keyCode == 37) { //left
            $("#nxt").parent().removeClass('trigged');
            return false;
        }
        if (e.keyCode == 39) { //right
            $("#prv").parent().removeClass('trigged');
            return false;
        }
    })

}

function onAfter(curr, next, opts) {
    if ($.trim($('#' + this.id + 'Cap').html()) != '') {
        $('#capCycle').fadeIn('slow');
        $('#capCycle p').html($('#' + this.id + 'Cap').html());
    }
}

function onBefore(curr, next, opts) {
    $('#capCycle').fadeOut('slow');
}

