var news_ticker;
var allow_restart = false;
var timer;
function start_news() {
  news_ticker = new NewsTicker( $('news'), {
    delay: 5.00,
    duration: 0.75
  });
  news_ticker.start();
  
  $$('#news li').invoke( 'observe', 'mouseover', pause_news );
  $$('#news li').invoke( 'observe', 'mouseout', restore_news );
  
  $$('#news_nav a').invoke( 'observe', 'click', change_news );
}

function pause_news(e) {
	news_ticker.pause_news();
}

function restore_news(e) {
	t = setTimeout("restart_news()", 3000);
	allow_restart = true;
}

function restart_news()
{
	if(allow_restart)
		news_ticker.restart()
}

function change_news(e)
{
	var atag = $(e.target);
	while(atag.tagName != "A")
		atag = atag.up('a');
	e.stop();
	var linknumber = atag.previousSiblings().length;
	if(linknumber == 0)
		news_ticker.move_back();	
	else 
		news_ticker.move_forward();
		
}

NewsTicker = Class.create({
  initialize: function( element, options ) {
    this.element = element;
    this.options = options;
    this.elements = element.childElements();
    this.current_element = element.down('.default') || this.elements[0];
    this.next_element = this.current_element.next() || this.elements[0];
    this.current_element.setStyle({ display: 'block' });
    this.current_element.removeClassName('default');
  },
  start: function() {
    if( this.current_element == this.next_element )
      return;

    if( this.effect ) {
      this.effect.finish_effect();
      this.next_effect.finish_effect();
    }
    this.effect = new Effect.Style( this.current_element, 'opacity', {
      start: 0.99,
      end: 0.0,
      unit: '',
      delay: this.options.delay,
      duration: this.options.duration,
      afterFinish: this._effect_finish.bind(this) } );
    this.next_effect = new Effect.Style( this.next_element, 'opacity', {
      start: 0.0,
      end: 0.99,
      unit: '',
      delay: this.options.delay,
      duration: this.options.duration,
      beforeStart: this._next_effect_start.bind(this) } );
    this.effect.start_effect();
    this.next_effect.start_effect();
  },
  restart: function() {
	if(!this.current_element)
		this.current_element = this.elements[0];
		
	//this.next_element = this.current_element.next() || this.elements[0];
	this.start();
  },
  stop: function() {
    if( this.effect ) {
      this.effect.finish_effect();
      this.next_effect.finish_effect();
      
      if(this.current_element.getOpacity() > .5)
      {
				this.current_element.setStyle( { display: 'block', opacity: 1 } );
				this.next_element.setStyle( { display: 'none' } );
			}
			else
			{
				this.current_element.setStyle( { display: 'none' } );
				this.next_element.setStyle( { display: 'block', opacity: 1  } );
		
		this.current_element = this.current_element.next() || this.elements[0];
		this.next_element = this.current_element.next() || this.elements[0];
      }
      /*
	  this.effect.cancel();
	  this.next_effect.cancel();
	  this.current_element.setStyle( { display: 'none', opacity: 0 } );
	  this.next_element.setStyle( { display: 'block', opacity: 1 } );
	  */
	  
	  
    }    
  },  
  pause_news: function() {
	this.stop();
	allow_restart = false;
	
  },
  get_index: function() {
	var index = 0;
	for(j = 0; j < this.elements.length; j++)
    {
		if(this.elements[j] == this.current_element)
		{
			index = j;
		}
    }	
    return index;
  },
  move_back: function() {
	this.stop();
	this.show_news( this.get_index() - 1 );
  },
  move_forward: function() {
	this.stop();
	this.show_news( this.get_index() + 1 );
  },
  show_news: function(index) {
	this.stop();
	if(index >= this.elements.length)
		index = 0;
	if(index < 0)
		index = this.elements.length - 1;
	
	clearTimeout(timer);
	timer = setTimeout("restart_news()", 3000);
	allow_restart = true;
	
    this.elements[index].setStyle( { display: 'block', opacity: 1 } );
    for(k = 0; k < this.elements.length; k++)
    {
		if(k != index)
			this.elements[k].setStyle( { display: 'none' } );
    }
    this.current_element = this.elements[index];
    this.next_element = this.current_element.next() || this.elements[0];
  },  
  _effect_finish: function() {
    this.current_element.setStyle({ display: 'none' });
    this.current_element = this.next_element;
    this.next_element = this.current_element.next() || this.elements[0];
    this.start();
  },
  _next_effect_start: function() {
    this.next_element.setStyle({ display: 'block', opacity: 0 });
  }
});