function preload_gallery_images(path, i){
    var image = new Image(670, 412);
  
    var src = path + i.toString() + ".jpg";

    image.onload = function() {          
        $("#preload").append('<img style="display: none" " alt="" height="412" src="' + this.src + '" />'); 
        j = i + 1;
        preload_gallery_images(path, j);
    }
    image.onerror = function() {         
        preload_gallery_thumbs(path, 1, i-1);
    }

    image.src = src;
}

function preload_gallery_thumbs(path, i, max){
    var image = new Image(60, 60);
  
    var src = path + i.toString()  + "_thumb.jpg";

    image.onload = function() {          
        $("#preload").append('<img style="display: none" " alt="" height="412" src="' + this.src + '" />');        
        j = i + 1;
        if (j <= max){
            preload_gallery_thumbs(path, j, max);
        }
    }
    image.onerror = function() { }

    image.src = src;
}


function preload_casestudy_images(path, dir, image_id, casestudy_id, max){

    var image = new Image(375, 412);
    var src = path + dir + image_id.toString() + ".jpg";

    image.onload = function() {          
        $("#preload").append('<img style="display: none" " alt="" height="412" src="' + this.src + '" />');
        new_casestudy_id = casestudy_id + 1;
        if ((max == 0) || (new_casestudy_id < max)) {
            preload_casestudy_images(path, 'casestudy'+ new_casestudy_id.toString() +'/', image_id, new_casestudy_id, max);
        }
    }
    image.onerror = function() { 
        if (image_id < 3) {
            var new_image_id = image_id + 1;
            preload_casestudy_images(path, 'casestudy1/', new_image_id, 1, casestudy_id - 1);
        } else {
            preload_casestudy_thumbs(path, 'casestudy1/', 1, 1, casestudy_id - 1);        
        }
        
    }

    image.src = src;
}

function preload_casestudy_thumbs(path, dir, image_id, casestudy_id, max){
    var image = new Image(60, 60);
  
    var src = path + dir + image_id.toString()  + "_thumb.jpg";

    image.onload = function() {          
        $("#preload").append('<img style="display: none" " alt="" height="412" src="' + this.src + '" />');
        var new_casestudy_id = casestudy_id + 1;
        if (new_casestudy_id < max) {
            preload_casestudy_thumbs(path, 'casestudy'+ new_casestudy_id.toString() +'/', image_id, new_casestudy_id, max);
        }
    }
    image.onerror = function() { 
        if (image_id < 3) {
            var new_image_id = image_id + 1;
            preload_casestudy_thumbs(path, 'casestudy1/', new_image_id, 1);
        }
    
    }

    image.src = src;
}


function preload_gallery(path)
{
    preload_gallery_images(path,  1);
}

function preload_casestudies(path)
{
        preload_casestudy_images(path, 'casestudy1/', 1, 1, 0);
        
}

function do_preload() {
   preload_gallery('/images/gallery/exhilaration/');
    preload_casestudies('/images/casestudies/');
    preload_gallery('/images/gallery/tranquility/');
    preload_gallery('/images/gallery/vitality/');
    preload_gallery('/images/gallery/inspiration/');    


}
$(document).ready(function() {
    setTimeout("do_preload()", 2000);
 });