var current_promo_div = 1;
var current_zindex = 10;
var current_large_image = 0;
var mcflashvars;

var timerIDs = new Array();



jQuery(document).ready( function() { 

    if ( typeof(number_of_large_pictures) == "number" ) {
        rotate_large_images( 'next', true );
        jQuery('.flashing_promo').hover(
            function() { large_arrows_move('in'); stop_timer( 'large' ); },
            function() { large_arrows_move('out'); start_timer( 'large' ); }
        );
    }

    jQuery('#flashing_promo_left_arrow').click( function() { rotate_large_images('previous', false); } );
    jQuery('#flashing_promo_right_arrow').click( function() { rotate_large_images('next', false); } );

    jQuery('.ngg-galleryoverview').append("<a href='/gallery' class='gallery_link'>Return to the Gallery</a>");

    if ( jQuery('#Newsletter_Frequency_Monthly').is(':visible') ) {

        var selected_default = jQuery('#Newsletter_Frequency_Default').val();
        if (selected_default.match(/Holiday/)) { selected_default = 'Holiday'; }
        jQuery('#Newsletter_Frequency_' + selected_default ).attr('checked', 'checked');
    }

});

function rotate_large_images( direction, start_timer ) {

    if ( !direction ) { direction = 'next'; }

    var next_large_image = (current_large_image * 1) + 1;
    if (next_large_image > number_of_large_pictures) { next_large_image = 1; }
    if (direction != 'next') { 
        var next_large_image = (current_large_image * 1) - 1;
        if (next_large_image == 0) { next_large_image = number_of_large_pictures; }
    }


    var image_path = '/wp-content/uploads/' + large_promo_picture_information[next_large_image]['filename'];
    var image_link = large_promo_picture_information[next_large_image]['link'];
    
    var new_content = "<a href='" + image_link + "'>";
    new_content += "<img src='" + image_path + "'>";
    new_content += "</a>";

    large_caption_move('out');
    jQuery('#flash_1').fadeOut(250, function() { 
        jQuery('#flash_1').html(new_content); 
        jQuery('#flashing_promo_caption').html( large_promo_picture_information[next_large_image]['caption'] );
        jQuery('#flash_1').fadeIn(250); 
        large_caption_move('in');
    } );

    current_large_image = next_large_image;    

    if (start_timer) { 
        timerIDs['large'] = setTimeout( "rotate_large_images( 'next', true );", 10000 );
    }

}

function large_arrows_move( direction ) {

    var amount = "-30px";
    if (direction == 'in') { amount = "0"; }

    jQuery("#flashing_promo_left_arrow").animate( { 'left': amount }, 'normal' );
    jQuery("#flashing_promo_right_arrow").animate( { 'right': amount }, 'normal' );

}

function large_caption_move( direction ) {

    var amount = "-27px";
    if (direction == 'in') { amount = "0"; }

    jQuery("#flashing_promo_caption").animate( { 'bottom': amount }, 'fast' );

}

function stop_timer( which ) {
    clearTimeout( timerIDs[which] );
    timerIDs[which] = null;
}

function start_timer( which ) { 
    if (which == 'small') { 
        timerIDs['small'] = setTimeout( "rotate_small_images();", 10000 );
    } else {
        timerIDs['large'] = setTimeout( "rotate_large_images( 'next', true );", 10000 );
    }
}

