﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

var timer = null;
var sliderDelay = 5000;

$(function()
{
  var s = $('[id|=sliderButton]').first().attr('id').substr(13);
  $('[id|=sliderButton]').first().parent().addClass('ul-' + s);
  $('[id|=sliderPanel]').slice(1).hide();

  $('[id|=sliderButton]').mouseenter(function()
  {
    var s = $(this).attr('id').substr(13);
    if (!$(this).parent().hasClass('ul-' + s))
    {
      $(this).parent().removeClass().addClass('ul-' + s);
      $('[id|=sliderPanel]:visible').fadeOut('normal');
      $('#sliderPanel-' + s).fadeIn('normal');
    }

    if (timer != null)
    {
      clearTimeout(timer);
      timer = null;
    }
  });

  $('[id|=sliderButton]').mouseleave(function()
  {
    timer = setTimeout('advanceSlider()', sliderDelay);
  });

  $('[id|=sliderPanel]').mouseenter(function()
  {
    if (timer != null)
    {
      clearTimeout(timer);
      timer = null;
    }
  });

  $('[id|=sliderPanel]').mouseleave(function()
  {
    timer = setTimeout('advanceSlider()', sliderDelay);
  });

  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-' + nextId).parent().removeClass('ul-' + curId).addClass('ul-' + nextId);

  timer = setTimeout('advanceSlider()', sliderDelay);
}
