$(document).ready(function()
{
  var delay = 10000; // milliseconds
  var current = 1;
  var old = 0;
  var width = 940;
  
  if($("#slide_backgrounds").children().length > 1)
  {
    // generate buttons
    $("#rotator_container").append("<div class=\"rotator_menu\"></div>");
    for(var i = 0; i < $("#slide_backgrounds").children().length; i++)
    {
      $(".rotator_menu").append("<a href=\"#\" class=\"b" + (i+1) + "\"><img src=\"/images/b_rotator-up.gif\" /></a>");
    }
    
    var t = setTimeout(next,delay);
    $(".rotator_menu a:nth-child("+ current +") img").attr("src", "/images/b_rotator-over.gif");
  }
  
  //$("#slide_backgrounds li:nth-child("+ current +")").attr("style", "left: 0px;");
  //$("#slide_contents li:nth-child("+ current +")").attr("style", "display: block;");
  
  $(".rotator_menu a").click(function()
  {
    clearTimeout(t);
    old = current;
    current = parseInt($(this).attr('class').slice(1));

    $(".rotator_menu a:nth-child("+ old +") img").attr("src", "/images/b_rotator-up.gif");
    $(".rotator_menu a:nth-child("+ current +") img").attr("src", "/images/b_rotator-over.gif");

    if (current >= old) {animateLeft()}
    if (current < old) {animateRight()}
    return false;
  });
  
  $("#feature_left a").click(function()
  {
    clearTimeout(t);
    old = current;
    current--;
    
    if (current == 0)
    {current = $("#slide_backgrounds").children().length}
    
    animateRight();
    return false;
  });
                             
  $("#feature_right a").click(function()
  {
    clearTimeout(t);
    old = current;
    current++;
    if (current == $("#slide_backgrounds").children().length + 1)
    {current = 1}
    
    animateLeft();
    return false;
  });
  
  function next()
  {
    old = current;
    current++;
    if (current == $("#slide_backgrounds").children().length + 1)
    {current = 1}
 
    animateLeft();
    t = setTimeout(next,delay);
  }

  function animateLeft()
  {
    $(".rotator_menu a:nth-child("+ old +") img").attr("src", "/images/b_rotator-up.gif");
    $(".rotator_menu a:nth-child("+ current +") img").attr("src", "/images/b_rotator-over.gif");
    
    $("#slide_backgrounds li:nth-child("+ current +")").css("left", width +"px");
    $("#slide_backgrounds li:nth-child("+ current +")").animate({"left": "0px"}, 400, "swing");
    $("#slide_backgrounds li:nth-child("+ old +")").animate({"left": -width+"px"}, 400, "swing");
    $("#slide_contents li:nth-child("+ current +")").fadeIn();
    $("#slide_contents li:nth-child("+ old +")").fadeOut();
  }
  
  function animateRight()
  {
    $(".rotator_menu a:nth-child("+ old +") img").attr("src", "/images/b_rotator-up.gif");
    $(".rotator_menu a:nth-child("+ current +") img").attr("src", "/images/b_rotator-over.gif");
    
    $("#slide_backgrounds li:nth-child("+ current +")").css("left", -width +"px");
    $("#slide_backgrounds li:nth-child("+ current +")").animate({"left": "0px"}, 400, "swing");
    $("#slide_backgrounds li:nth-child("+ old +")").animate({"left": width+"px"}, 400, "swing");
    $("#slide_contents li:nth-child("+ current +")").fadeIn();
    $("#slide_contents li:nth-child("+ old +")").fadeOut();
  }
});
