var current_image = 0;
var background_images = new Array();

var counter = 1;
while (counter <= 18){
    var image_name_counter = counter.toString();
    if( image_name_counter.length < 2 ){
        image_name_counter = '0' + image_name_counter;
    }
    background_images[counter -1] = 'images/0' + image_name_counter  + 'RBD.jpg';
    counter += 1;
}
var image_length = background_images.length - 1;
var animating = false;

function setNavElements(){
    $('span#current_image').html(current_image + 1);
    $('span#total_image').html(image_length + 1);
}

$(document).ready(function() {

    setNavElements();

    $('#temple_info').click(function(){
        if( $('#overview_layer').is(':visible') ){
            $('#overview_layer').slideToggle('fast');
            $('#temple_overview').html('Overview');
            $('#hide_overview').html('');
        }
        $('#copy').addClass('display');
        $('#info_layer').slideToggle('fast', function(){
            if( !$(this).is(':visible') ){
                $('#temple_info').html('About');
                $('#hide_info').html('');
                setTimeout(function(){
                    $('#copy').removeClass('display');
                }, 100);
            } else {
                $('#temple_info').html('About');
                $('#hide_info').html('(hide)');
            }
        });
        return false;
    });

    $('#temple_overview').click(function(){
        if( $('#info_layer').is(':visible') ){
            $('#info_layer').slideToggle('fast');
            $('#temple_info').html('About');
            $('#hide_info').html('');
        }
        $('#copy').addClass('display');
        $('#overview_layer').slideToggle('fast', function(){
            if( !$(this).is(':visible') ){
                $('#temple_overview').html('Contact');
                $('#hide_overview').html('');
                setTimeout(function(){
                    $('#copy').removeClass('display');
                }, 100);
            } else {
                $('#temple_overview').html('Contact');
                $('#hide_overview').html('(hide)');
            }
        });
        return false;
    });
    

    $('a.pn').click(function(){
        if(!animating){
            animating = true;
            if( $(this).attr('id') == 'prev' ){
                current_image = current_image - 1;
                if( current_image < 0 ){
                    current_image = image_length;
                }
            } else {
                current_image = current_image + 1;
                if( current_image > image_length ){
                    current_image = 0
                }
            }

            $('#background_image')
                //.fadeTo('fast', .01, function(){
                .fadeOut('fast', function(){
                    $(this).attr('src', background_images[current_image]);
                        $('img').imagesLoaded(function(){
                            $('#background_image').bleedImage();
                            setNavElements();
                            setTimeout(function(){
                                //$('#background_image').fadeTo(400, 1, function(){
                                //$('#background_image').fadeIn(400, function(){
                                    animating = false;
                                    if( current_image < image_length ){
                                        jQuery.preLoadImages(background_images[current_image + 1]);
                                    }
                                //});
                            }, 600);

                        });
                });
            }
        return false;
    })
});

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
