// Image Preloader Plug-In
jQuery.preloadImages = function() {
    for(var i = 0; i<arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
}

// Onload...
jQuery(function($){
    // Try and apply event to id="replay_teeth"
    if($('#replay_teeth').size() > 0) {
        $('#replay_teeth').click(function(){
            var src = $('#temp_body img').eq(0).attr('src');
            $('#temp_body img').eq(0).attr('src', null);
            $('#temp_body img').eq(0).attr('src', src);
            return false;
        });       
    }

    // Start Image Rotator
    if($('#office_images').size() > 0) {
        var office_images = ['office_traverse_city.png','office_beulah.png','office_grayling.png'];
        $.preloadImages(
            BLOGINFO_STYLESHEET_DIRECTORY + '/images/' + office_images[0],
            BLOGINFO_STYLESHEET_DIRECTORY + '/images/' + office_images[1],
            BLOGINFO_STYLESHEET_DIRECTORY + '/images/' + office_images[2]
        );
        var callback = function() {
            var images = office_images;
            var current_src = $('#office_images img').attr('src');
            var next_image_index = 1;
            $.each(images, function(index, value){
                if(current_src.indexOf(value) !== -1) {
                    switch(index) {
                        case 0:
                            next_image_index = 1;
                            break;
                        case 1:
                            next_image_index = 2;
                            break;
                        case 2:
                            next_image_index = 0;
                            break;
                    }
                }
            });
            $('#office_images img').fadeOut(1000,function(){
               $(this).attr('src', BLOGINFO_STYLESHEET_DIRECTORY + '/images/' + images[next_image_index]);
               $(this).fadeIn(1000);
            });
        }
        window.setInterval(callback, 6000);
    }
});
