$(document).ready(function()
{
  var timer;
  var $candidate_id = -1;
  var $shifted_id = -1;
  
  var shiftDistance = $('.slider_div').width() - $('.slider_bar').width() - 4;

  $('.slider_bar').mouseleave(cancelShift);
  $('#photo_0_link').mouseleave(cancelShift);
                              
  function cancelShift()
  {
    $candidate_id = -1;
      
    $this_id = $(this).parent().attr('id').substring(11,12);
    $this_id = parseInt($this_id, 10);
    if ($this_id != $shifted_id)
    {
        $(this).css("background-position", "0 0");
    }
  }

  $('.slider_bar').mouseenter(slideWithTimeout);
  $('#photo_0_link').mouseenter(slideWithTimeout);
                              
  function slideWithTimeout()
  {
    $candidate_id = $(this).parent().attr('id').substring(11,12);
    $candidate_id = parseInt($candidate_id, 10);
    if ($candidate_id > 0)
    {
        $(this).css("background-position", "-30px 0");
    }
    
    if(timer)
    {
      clearTimeout(timer);
      timer = null;
    }
    timer = setTimeout(function()
    {
        if ($candidate_id == -1)
        {
            return;
        }
        $shifted_id = $candidate_id;
        $('.slider_div').each
        (
          function (index)
          {
            if (index != 0 && index <= $shifted_id && $(this).attr("shifted") != "true")
            {
              sliding = true;
              $(this).animate({left: "-=" + shiftDistance + "px"});
              $(this).attr("shifted", "true");
            }
            else if (index > $shifted_id && $(this).attr("shifted") == "true")
            {
              $(this).animate({left: "+=" + shiftDistance + "px"});
              $(this).attr("shifted", "false");
            }
            if (index == $shifted_id && $shifted_id != 0)
            {
                $(this).children('.slider_bar').css("background-position", "-30px 0");
            }
            else
            {
                $(this).children('.slider_bar').css("background-position", "0 0");
            }
          }
        );
    }, 500) //setTimeout
  }
  
  
  
}); //ready

