var current_image, current_link, previous_image = 0;

jQuery( document ).ready( function( $ ){
    link_promo_images();
    init_slideshow();
});

function init_slideshow()
{
    jQuery('.slideshow-images').cycle('stop').cycle({
            fx:'fade',
            sync: 1,
            timeout: 5000,
            after: update_bullet,
            pager: '.bullets',
            pagerAnchorBuilder: function( idx, slide ) { return '.bullets li:eq(' + (idx) + ') a'; }
    });
}

function link_promo_images()
{
    jQuery( function( $ ) {
        $( '.promo1' ).click( function(){
            window.location = $( '.promo1 a' ).attr('href');
        } ).hover( function(){
            $( '.promo1 p a' ).css({'color':'#ff6633'});
        }, function(){
            $( '.promo1 p a' ).css({color:'white'});
        } );

        $( '.promo2' ).click( function(){
            window.location = $( '.promo2 a' ).attr('href');
        } ).hover( function(){
            $( '.promo2 p a' ).css({'color':'#ff6633'});
        }, function(){
            $( '.promo2 p a' ).css({color:'white'});
        } );

        $( '.promo3' ).click( function(){
            window.location = $( '.promo3 a' ).attr('href');
        } ).hover( function(){
            $( '.promo3 p a' ).css({'color':'#ff6633'});
        }, function(){
            $( '.promo3 p a' ).css({color:'white'});
        } );
    } );
}

function update_bullet( current_element, next_element, options, forward )
{
    previous_image = current_image;
    current_image = options.currSlide + 1;
    jQuery('.image-' + current_image ).attr( 'class', 'image-' + current_image+ ' on' );
    jQuery('.image-' + previous_image ).attr( 'class', 'image-' + previous_image );
    jQuery( options.elements[ current_image - 1 ] ).children().each(function(){
        current_link = jQuery(this).attr('href');
        jQuery( this ).children().each( function(){
            jQuery('.image-description p').html( jQuery('<a>').attr( 'href', current_link ).attr( 'class', 'slideshow-image-link' ).text( jQuery(this).attr('alt') ) );
        });
    });
}