var scrollers = [];

function newsScroller( newsContainer, newsElements, delay ) {
  this.items     = [];
  this.n_num     = 0;
  this.o_cont    = jQuery('#' + newsContainer).get(0);
  this.n_step    = 1;
  this.delay     = delay;
  this.n_cur     = 0;
  this.waiting   = false;
  this.h_cont    = 0;
  this.n_timeout = null;

  var i = 0;
  var l = [];
  var h = 0;
  jQuery('#' + newsContainer + ' ' + newsElements).each( function() {
		                                                   var t          = (i == 0) ? 0:(l[i-1].top + l[i-1].height);
												           this.style.top = t + 'px';
		                                                   l.push( { 'element' : this,
                                                                     'pause_b' : 0,
                                                                     'top'     : t,
                                                                     'height'  : this.offsetHeight } );
	                                                       h += l[i].height;
	                                                       i += 1;
	                                                     } );
  this.items  = l;
  this.n_num  = this.items.length;
  this.h_cont = h;

  if (this.h_cont > this.o_cont.offsetHeight) {
    scrollers.push( this );
  }
  
  jQuery('#' + newsContainer).bind( 'mouseover', { n : (scrollers.length - 1) }, hstop );
  jQuery('#' + newsContainer).bind( 'mouseout', { n : (scrollers.length - 1) }, hmove );
}

function start() {
  var n = scrollers.length;
  for(var i = 0 ; i < n ; i++) {
    move( i );
  }
}

function hmove( event ) {
  move( event.data.n );
}  
function move( n ) {
  var s = scrollers[n];
  if (!s.waiting && (s.items[s.n_cur].top >= 0) && (s.items[s.n_cur].top - s.n_step < 0)) {
	s.waiting = true;
	s.n_timeout = setTimeout('move(' + n + ')', s.items[s.n_cur].pause_b * 1000);
  }	else {
	s.waiting = false;
	for (var i = 0; i < s.n_num; i++) {
	  s.items[i].top -= s.n_step;
	  if ((i == s.n_cur) && (s.items[i].top + s.items[i].height < 0)) {
		s.items[i].top = (i == 0) ? s.items[s.n_num - 1].top + s.items[s.n_num - 1].height : s.items[i - 1].top + s.items[i - 1].height;
		s.items[i].top = (s.items[i].top < s.o_cont.offsetHeight) ? s.o_cont.offsetHeight : s.items[i].top;
		s.n_cur = (s.n_cur == s.n_num - 1) ? 0 : s.n_cur + 1;
	  }
	  s.items[i].element.style.top = s.items[i].top + 'px';
	}
	s.n_timeout = setTimeout('move(' + n + ')', s.delay * 1000);
  }
}
  
function hstop(event) {
  stop( event.data.n );
}
function stop( n ) {
  var s = scrollers[n];
  clearTimeout(s.n_timeout);
}

