NUM_SLIDES = 0;
SLIDE_TIME = 5000;
SLIDE_FADE_TIME = 1000;
SLIDE_CHANGER = null;
CURRENT_SLIDE = 0;
FADING = false;

$(document).ready(function() {
   var slides = $("#slides");
   var slideshow = $("#slideshow");
   var slideshow_control = $("#slideshow_control");

   $("#slides img").hide().eq(0).addClass("visible").show();
   NUM_SLIDES = slides.children("a").length;

   var control_html = "";
   for (var i = 0; i < NUM_SLIDES; ++i) {
      control_html += "<li><a id='slide_ctl_"+i+"' href='javascript:click_slide("+i+")'>"+(i+1)+"</a></li>";
   }
   slideshow_control.html(control_html);

   $("#slideshow_control li:first-child a").addClass("active");

   SLIDE_CHANGER = setTimeout('change_slides()', SLIDE_TIME);
});

function change_slides()
{
   next_slide();
   SLIDE_CHANGER = setTimeout('change_slides()', SLIDE_TIME);
}

function next_slide()
{
   show_slide((CURRENT_SLIDE + 1) % NUM_SLIDES);
   CURRENT_SLIDE++;
}


function show_slide(slide_index)
{
   FADING = true;
   SLIDE_CHANGER = null;
   var slides = $("#slides");
   var visible = $("#slides .visible");

   $("#slides img").removeClass("visible");

   var slide = $("#slides img").eq(slide_index);

   slide.fadeIn(SLIDE_FADE_TIME);
   slide.addClass("visible");
   visible.fadeOut(SLIDE_FADE_TIME, function() { FADING = false; });

   $("#slideshow_control a").removeClass("active");
   $("#slide_ctl_"+slide_index).addClass("active");
}

function click_slide(slide_index)
{
   if (FADING) return;
   clearTimeout(SLIDE_CHANGER);
   show_slide(slide_index);
}
