
var slider = new Slider();
var sliderInterval;
var sliderTimeout;

$(document).ready(function() {
  slider.timeout = 3000;
  slider.autoStart(true);
  $('#top-news').mouseover(function() {
    slider.stopAuto();
  }).mouseleave(function() {
    slider.startTimer();
  });
});

function Slider() {
  
  this.timeout;
  this.obj;
  this.speed = 1000;
  this.freez = false;
  
  this.autoStart = function(ival) {
    
    
      this.obj = $(this.obj).next().next('h2');
      if (!$(this.obj).html()) {
        this.obj = $('#top-news h2:first');
      }
    
    this.Go();
    if (ival) {
      sliderInterval = setInterval('slider.autoStart();', this.timeout);
    }
  
  }
  
  this.Go = function(obj) {
    
    clearTimeout(sliderTimeout);
    
    if (this.freez == false) {
      if (obj) { 
        this.obj = obj; 
      }
      
      $('#top-news .news').fadeOut(this.speed);
      $('#top-news h2').removeClass('selected');
      $(this.obj).addClass('selected').next('.news').fadeIn(this.speed,null,function() { slider.freez = true; });
    }
  
  }
  
  this.stopAuto = function() {
  
    this.speed = 0;
    clearInterval(sliderInterval);
  
  }
  
  this.startTimer = function() {
  
    this.speed = 1000;
    sliderTimeout = setTimeout('slider.autoStart(true);',2000);
    
  }
  
}
