 /* a couple of globals to track the number if images we have tried to load and whether */
 /* we have found an image that fails to load */
 var image_load_count = 1;
 
 var failed_to_load = false;
 var failed_to_load_thumb = false;
 var failed_to_load_photo = false;
 var initialize_pages = new Array();
 var current_page = 0;
 var cookie_path = '';
 
 function set_navigation(i)
 {
    var prev_link = document.getElementById("m4_prev");
    var next_link = document.getElementById("m4_next");
    var hide_count = 0;
    if (i == 0) { 
        prev_link.style.display = 'none';
        hide_count = hide_count + 1;
    } else {
       prev_link.style.display = 'block';
    }
    if ((i == 9) || (initialize_pages[i+1] == false)) {
       next_link.style.display = 'none';
       hide_count = hide_count + 1;
    } else {
     next_link.style.display = 'block'; 
    }
    
    if (hide_count == 2) {
        $("#m4").css('padding-top', '15px');        
    }
    $("#m4").show();
 }
 function next_page()
 {
    var current = current_page + 1;
    var next_page = current + 1; 
    var next_menu = document.getElementById("m4_" + next_page.toString());
    var current_menu = document.getElementById("m4_" + current.toString());
    
    current_menu.style.display = 'none';
    next_menu.style.display = 'inline';
    set_navigation( current_page + 1);
    current_page = current_page + 1;
 }
 
 function prev_page()
 {
    var current = current_page + 1;
    var prev_page = current - 1; 
    var prev_menu = document.getElementById("m4_" + prev_page.toString());
    var current_menu = document.getElementById("m4_" + current.toString());
    
    current_menu.style.display = 'none';
    prev_menu.style.display = 'inline';
    set_navigation( current_page - 1);
    current_page = current_page - 1;
 }
 
 function initialize(path)
 {
    initialize_pages[0] = false;
    initialize_pages[1] = false;
    initialize_pages[2] = false;
    initialize_pages[3] = false;
    initialize_pages[4] = false;
    initialize_pages[5] = false;
    initialize_pages[6] = false;
    initialize_pages[7] = false;
    initialize_pages[8] = false;
    initialize_pages[9] = false;
    cookie_path = path;
 }
 
 /* Every time we get an image load or error event we do some accounting */
 function handle_image_load()
 {
    /* increment the number of images that we have see thus far */
    image_load_count = image_load_count + 1;
    
    /* If this is the last image, get ready to hide the overlay... we actually */
    /* set a timeout for closing the overlay of one second.. this prevents a flicker */
    /* should the images load right after we bring up the overlay */
 
    if ((failed_to_load_thumb) && (failed_to_load_photo)) {
        set_navigation(0);
        setTimeout('hide_loading_overlay()', 500);
        setCookie(cookie_path, "preloaded", 3);
        $("#m4").show();
    }    
 }
 
 /* cheap function to pause execution for a number of milliseconds */
 function pause(millis) 
 {
        var date = new Date();
        var curDate = null;

        do { curDate = new Date(); } 
        while(curDate-date < millis)
 } 
 
 /* Given an index, this function will show the appropriate image in the large gallery window */
 function show_image(i) {
 
    $(".bigphoto_images").hide();
   
    var bigphoto = document.getElementById("image_" + i);
  
    bigphoto.style.display = 'inline';
    
 }
 
 /* Given a div (where the gallery images live, a path to the image and and index, this */
 /* will write the gallery image to a div where the display is initially set to none */
 function write_big_photo(bigphoto, path, i){
 
    if (true) {
         var image = new Image(670, 412);
         var src = path + i.toString() +".jpg";

         /* image.onload = preload_bigphoto(src, bigphoto, i); */
         
         image.onload = function() {          
            bigphoto.innerHTML += '<img style="display: none" name="bigphoto_images" class="bigphoto_images" id="image_' + i.toString() + '" alt="" height="412" src="' + this.src + '" />'; 
            handle_image_load();
            j = i + 1;
            write_big_photo(bigphoto, path, j);
            
         }
         image.onerror = function() { failed_to_load = true; failed_to_load_photo = true; handle_image_load();     write_thumbnail_area(path, i-1);   }
         
         image.src = src;
    }
 }
 
 function write_thumbnail(path, i, max) {
    if (false === failed_to_load_thumb) {
        var page = Math.floor((i-1)/5) +1;
        var image = new Image(102, 68);
        var src = path + i.toString() + "_thumb.jpg";
        var analytics_src = path.substring(2) + i.toString() + ".jpg";
        
        var thumbnails = document.getElementById("m4_" + page.toString());
        image.onload = function() {           
           
            initialize_pages[Math.floor((i-1)/5)] = true;
            thumbnails.innerHTML +='<div onmouseover="show_image('+ i.toString() +'); quietPageTrack(\''+ analytics_src +'\')" id="thumb' + i.toString() +'" class="gallery_tbnail"><img id="thumb_image_' + i.toString() +'" alt="" height="68" src="' + this.src + '" /></div>'; 
            if (i == max) { failed_to_load_thumb = true; }
            handle_image_load();      
              
        } 
        image.onerror = function() {failed_to_load = true; failed_to_load_thumb = true;  handle_image_load(); }     
        
        image.src = src;
    }
 }
 
 function write_big_photo_area(path){
    var bigphoto = document.getElementById("m3");
    bigphoto.innerHTML='<img name="bigphoto_images" class="bigphoto_images" id="image_1" alt="" height="412" src="' + path + '1.jpg" style="display: inline" />';
    write_big_photo(bigphoto, path, 1);      
 }
 
 function write_thumbnail_area(path, max)
 {
    
        for (var i = 1; i <= max; i++) { 
            write_thumbnail(path, i, max);      
        }
 }
 
 function show_loading_overlay()
 {
     var preloaded = getCookie(cookie_path);  
     
    if ("" === preloaded) {
    var loading = document.getElementById("loading");
 
    if ((false == failed_to_load_thumb) || (false == failed_to_load_photo )  ) {
        loading.style.display = 'inline';
    }
    }
  
 }
 
 function hide_loading_overlay()
 {
    var loading = document.getElementById("loading");
    loading.style.display = 'none';
 }
 
 function gallery_init(path) {    
    initialize(path);
    show_loading_overlay();
    write_big_photo_area(path);
 }
 
