
/**
* helper function to get the attributes of an object as text buffer
*/
function inspect(o) {
  var buffer="";
  for(i in o) {
    buffer = buffer+ i + "=" + o[i] + "\n";
  }
  return buffer;
}

/* global */
var hilighted_elements= $A();
var target_color= 0;
var current_color= 255;
var blend = 0.0;
var rate= 0.02;

var normal_color=[122,122,122];
var highlight_start_color=[109,43,14];
var highlight_end_color=[156, 115, 50];

var animator= new PeriodicalExecuter(function(pe) {
  if(blend > 0) {
    blend -= rate;
    if(blend < 0) {
      blend = 0;
    }
    new_color= blendColors(highlight_end_color, highlight_start_color, blend );

    hilighted_elements.each( function(e) {
      e.style.color= colorArrayToString( new_color );
    });  
  }
  //if (!confirm('Want me to annoy you again later?'))
  //    pe.stop();
    
}, 0.01);


function animateHighlightColor() {
  /* set the color of animated color to red */
  blend= 1.0;  
}


function colorArrayToString (rgb) {
  return "rgb("+ rgb[0]  +","+ rgb[1] + ","+ rgb[2] + ")";
}

function blendColors(c1, c2, a){
  co= [];
  for(i=0 ; i < c1.length; i++) {
    co[i] = Math.floor( c1[i] + (c2[i]- c1[i]) * a); 
  }
  return co;
}

Event.observe(window, 'load', function() {
  /**
  * observe all productions to hilight keywords on mouse over
  */
  $$('div.production').each( function(element) {
    Event.observe( element, 'mouseover', function (event) {
      
      var element = Event.element(event);
      if(element == undefined) {
        return;
      }
      while ( element.tagName != 'DIV') {
        element= element.parentNode;
        if(!element) {
          return;
        }
      }
      if(element && undefined != element.id) {
        if( crossref[ element.id ]  ) {
          
          blend = 1.0;
          $$('div.header ul li a').each( function(element) {
            element.style.color= colorArrayToString( normal_color );
            
          });
          
          
          crossref[ element.id ].each( function(highlight_id) {
            //alert(highlight_id);
            $$('a#' + highlight_id).each( function(hilight_element) {
              hilight_element.addClassName('hilight');
              hilighted_elements.push( hilight_element );
            });
          });
        }
      }
      Event.stop(event);
      return false;
      
    });
    Event.observe( element, 'mouseout', function (event) {
      hilighted_elements.each( function(e){
        e.removeClassName('hilight');
        e.style.color= colorArrayToString( normal_color );
      });  
      Event.stop(event);    
      hilighted_elements= $A();
      return false;      
    });
    
  })
});

