(function($){
  $.fn.simpleSlider = function() {
    index = 0,
    timer = null;
    timerBlocked = false;
  
    function displayIndex(i) {
      $("#slider_images > li").css("z-index", 30).eq(i).css("z-index", 40);
      $("#slider_buttons li").removeClass("active").eq(i).addClass("active");
      index = i;
      
      if ( timer ) clearTimeout(timer);
      if ( ! timerBlocked ) timer = setTimeout(goNext, 5000);
    }
    
    function blockTimer() {
      if ( timer ) clearTimeout(timer);
      timerBlocked = true;
    }
    
    function goNext() {
      if ( index+1 >= $("#slider_images > li").length ) displayIndex(0);
      else displayIndex(index+1);
    }
  
    // Triggers
    $("#slider_buttons a").hover(function() {
      blockTimer();
      var index = $("#slider_buttons a").index(this);
      displayIndex(index);
    }, function() {
      timerBlocked = false;
      if ( timer ) clearTimeout(timer);
      timer = setTimeout(goNext, 5000);
    });
    
    // Init
    displayIndex(0);
  };

  $(document).ready(function() {
    $().simpleSlider();
  });
})(jQuery);
