var home_image_array = new Array(); //["wedding01.jpg", "beach_toddler.jpg", "wedding02.jpg", "wedding03.jpg", "dog01.jpg", "wedding04.jpg", "ian01.jpg", "wedding05.jpg", "bw_girl.jpg", "wedding06.jpg", "mum&baby01.jpg", "wedding07.jpg", "wedding08.jpg", "baby01.jpg"];

var home_current_image = 0;
var home_image_fade = 1000, home_image_delay = 3000; //image duration (in milliseconds)

function home_get_image()
{
   //loop through array
   var filename = "gtcms/image_gallery/" + home_image_array[home_current_image];
   if (++home_current_image >= home_image_array.length)
      home_current_image = 0;
   return filename;
};

function home_show()
{
   //display top image
   //check to make sure image has loaded
   if ($("#image_front").attr("complete") == false)
   {
      //image not loaded; try again after 500 milliseconds
      setTimeout("home_show()", 500);
      return 0;
   };
   
   //display front image
   $("#image_front").fadeIn(home_image_fade)
     .queue(home_display_back_image);
};

function home_hide()
{
   //display back image
   //check to make sure image has loaded
   if ($("#image_back").attr("complete") == false)
   {
      //image not loaded; try again after 500 milliseconds
      setTimeout("home_hide()", 500);
      return 0;
   };
   
   //hide front image
   $("#image_front").fadeOut(home_image_fade)
     .queue(home_display_front_image);
};

function home_display_back_image()
{
   //front image fully loaded
   //change back image
   $("#image_back").attr("src", home_get_image()).show();
   
   //start timer for front image
   $(this).dequeue();
   setTimeout("home_hide()", home_image_delay);
};

function home_display_front_image()
{
   //back image fully loaded
   //chage front image while front image remains invisible
   $("#image_front").attr("src", home_get_image());
   
   //start timer for front image
   $(this).dequeue();
   setTimeout("home_show()", home_image_delay);
};

function home_main()
{
   //insert images; document fully loaded
   $("#slideshow_frame").append("<img id='image_front' src='" + home_get_image() + "' alt='' /><img id='image_back' src='' alt='' />");

   //start slideshow
   home_show();
};

// after images are loaded begin the slide show
jQuery(window).load(function() { 
  $('#slideshow_filenames span').each(function(i, el) {
    home_image_array[home_image_array.length] = $(el).text();
  });

  //start slideshow
  home_main();   
});

