

document.observe('dom:loaded', function() {
  active= 'active';
  rotator_delay= 7;
  feature_rotator_id= 'feature_rotator';

  // some functions to make rotating easier ---------------
  function activate_rotator_item(element){      
    element.addClassName(active);
    deactivate_siblings( element );
    show_active_image( element );
  };
  function deactivate_siblings(element){
    element.siblings().invoke( 'removeClassName', active );
  };
  function show_active_image(element){
    $(feature_rotator_id).down('img').setAttribute('src', element.down('img').src);
    $(feature_rotator_id).down('a').setAttribute('href', element.down('a').href);

  };
  function find_next_li_to_activate(element){
    if ( find_active_li( element ).nextSiblings().size() > 0) {
      return find_active_li( element ).next();
    } else { 
      return find_active_li( element ).previousSiblings().last();
    }
  };      
  function find_active_li(element){
    return element.descendants().find( function(e) {
      return e.match("li."+active);
    });
  };
  
  
  if ( $(feature_rotator_id) != null ) {
  
    // start on first then execute periodical rotator -------
    activate_rotator_item( $(feature_rotator_id).down('li') );
    var periodical_rotator= new PeriodicalExecuter(function(pe) {
      activate_rotator_item( find_next_li_to_activate($(feature_rotator_id)) );
    }, rotator_delay);

    // observes for mouseover on rotator items --------------
  	$(feature_rotator_id).descendants().findAll( function(e) {
      return e.match("li");
    }).each( function(li) {
      li.observe('mouseover', function() { 
        activate_rotator_item(li);
        periodical_rotator.stop();
      });
    });
	}
	
});