var home_array;
var home_current_image = 0;
var home_next_image = 0;
var home_image_delay = 3000; //in milliseconds
var home_image_transition_delay = 1000; //in milliseconds
var home_image_load_delay = 300; //time to wait (in milliseconds) if image is not fully loaded
var home_can_control = false; //disable buttons when image is transitioning
var home_timer;

function home_main()
{
   //init slideshow
   home_array = $("#slideshow_image_frame").children("img.slide");
   home_current_image = 0;
   if ($(home_array[home_current_image]).attr("complete") == false)
   {
      setTimeout("home_main()", home_image_load_delay); //wait until first image is loaded
      return;
   };
   home_load_next_image(true);
   home_timer = setTimeout("home_advance()", home_image_delay);
   $("#left_arrow").click(home_left_arrow_click);
   $("#right_arrow").click(home_right_arrow_click);
   home_can_control = true;
};

//start slideshow after page is loaded
$(window).load(home_main);

function home_load_next_image(advance)
{
   if (advance)
   {
      //advance to next slide
      home_next_image = home_current_image + 1;
   }
   else
   {
      //go to previous slide
      home_next_image = home_current_image - 1;
   };
   if (home_next_image < 0)
      home_next_image = home_array.length - 1;
   if (home_next_image >= home_array.length)
      home_next_image = 0;
};

function home_left_arrow_click()
{
   //user clicked "rewind slide"
   if (!home_can_control)
      return;
   home_can_control = false;
   home_load_next_image(false);
   clearTimeout(home_timer);
   home_rewind();
}

function home_right_arrow_click()
{
   //user clicked "advance slide"
   if (!home_can_control)
      return;
   home_can_control = false;
   clearTimeout(home_timer);
   home_advance();
}

function home_advance()
{
  //delay if next image is not loaded
   if ($(home_array[home_next_image]).attr('complete') == false)
      home_timer = setTimeout("home_advance()", home_image_load_delay);
   
   //animate slide
   home_can_control = false;
   $(home_array[home_next_image]).css({'top':'0', 'left':'828px', 'display':'inline'});
   $(home_array[home_current_image]).animate({'left': '-828px'}, home_image_transition_delay).hide(1);
   $(home_array[home_next_image]).animate({'left': '0'}, home_image_transition_delay, home_animation_complete);
};

function home_rewind()
{
  //delay if next image is not loaded
   if ($(home_array[home_next_image]).attr('complete') == false)
      home_timer = setTimeout("home_rewind()", home_image_load_delay);
   
   //animate slide
   home_can_control = false;
   $(home_array[home_next_image]).css({'top':'0', 'left':'-828px', 'display':'inline'});
   $(home_array[home_current_image]).animate({'left': '828px'}, home_image_transition_delay).hide(1);
   //the "1" in hide(1) prevents hide() from executing immediately
   $(home_array[home_next_image]).animate({'left': '0'}, home_image_transition_delay, home_animation_complete);
}

function home_animation_complete()
{
   //advance to next image
   home_current_image = home_next_image;
   home_load_next_image(true);
   
   //reactivate timer for slideshow
   home_timer = setTimeout("home_advance()", home_image_delay);
   home_can_control = true;
};

