var slideshow = {timeout : null, next_slide_time: 10000, start_time : 10000}

$(document).ready(function(){
  slideshow_run(true);
  
  setTimeout(init_grey_links, 300);  
});


function init_grey_links(){
  $('#grey .feed a').append('&nbsp;<span>&raquo;</span>');
}

function slideshow_run(init) {
  clearTimeout(slideshow.timeout);    
  if (init) {        
    //randomize 1 slide
    $('#slideshow img').hide();
    random = Math.floor(Math.random() * $('#slideshow img').length);
    $("#slideshow img").eq(random).show();
    slideshow_change(true, true);

    $('#slideshow .right').click(function(){ slideshow_change(true); });
    $('#slideshow .left').click(function(){ slideshow_change(false); });
    slideshow.timeout = setTimeout(function(){slideshow_run(false); } , slideshow.start_time);    
  } else {
    //move
    slideshow_change(true);
  }
}

function slideshow_change(front, fast) {
  clearTimeout(slideshow.timeout);
  slideshow.timeout = setTimeout(function(){slideshow_run(false); } , slideshow.next_slide_time);
  c_curr = $('#slideshow img:visible').attr('class');
  curr = $('#slideshow img.' + c_curr);
  if (front) {
    next =  $(curr).next();
    
    if (!$(next).length) {
      next =  $('#slideshow img:first');
    }
  }
  else {
    next =  $(curr).prev();
    
    if (!$(next).length) {
      next =  $('#slideshow img:last');
    }
  }
  
  c_next = $(next).attr('class');
  
  $(curr).fadeOut('slow');
  if (fast)
    $(next).show();
  else
    $(next).fadeIn('slow');
    
  $('#slideshow .details').html($('#slideshow .slides-content .' + c_next).html());  
}
