/// <reference path="jquery-1.4.1-vsdoc.js" />

var timer = null;
var sliderDelay = 5000;
var aaronProof = false;

$(function()
{
  $('.rCaption a').each(function()
  {
    var h = ($(this).parent().height() / 2) - ($(this).height() / 2) + 2;
    $(this).css('margin-top', h);
  });

  $('[id|=sliderPanel]').slice(1).hide();
  $('[id|=sliderButton] :first-child').slice(1).addClass('btnNormal');
  $('[id|=sliderButton] :first-child').first().addClass('btnHilite');

  $('[id|=sliderPanel]').mouseenter(stopAdvance);
  $('[id|=sliderPanel]').mouseleave(startAdvance);

  $('[id|=sliderButton]').mouseenter(stopAdvance);
  $('[id|=sliderButton]').mouseleave(startAdvance);

  $('[id|=sliderButton] :first-child').click(function()
  {
    if (aaronProof)
      return;

    var current = $('[id|=sliderPanel]:visible').first();
    var curId = current.attr('id').substr(12);
    var nextId = $(this).parent().attr('id').substr(13);
    if (curId == nextId)
      return;

    aaronProof = true;
    current.fadeOut('normal');
    $('#sliderPanel-' + nextId).fadeIn('normal', function() { aaronProof = false; });
    $('#sliderButton-' + curId + ' :first-child').switchClass('btnHilite', 'btnNormal', 250);
    $('#sliderButton-' + nextId + ' :first-child').switchClass('btnNormal', 'btnHilite', 250);
  });

  timer = setTimeout('advanceSlider()', sliderDelay);
});

function stopAdvance()
{
  if (timer != null)
  {
    clearTimeout(timer);
    timer = null;
  }
}

function startAdvance()
{
  timer = setTimeout('advanceSlider()', sliderDelay);
}

function advanceSlider()
{
  var current = $('[id|=sliderPanel]:visible').first();
  var next = $('[id|=sliderPanel]:visible ~ [id|=sliderPanel]');
  if (next.length == 0)
    next = $('[id|=sliderPanel]').first();
  else
    next = next.first();
  
  var curId = current.attr('id').substr(12);
  var nextId = next.attr('id').substr(12);
  current.fadeOut('normal');
  next.fadeIn('normal');
  $('#sliderButton-' + curId + ' :first-child').switchClass('btnHilite', 'btnNormal', 250);
  $('#sliderButton-' + nextId + ' :first-child').switchClass('btnNormal', 'btnHilite', 250);

  timer = setTimeout('advanceSlider()', sliderDelay);
}
