document.observe("dom:loaded", function(){
  vertical_center_callouts();
  masthead_rotator();
  nav_set_here();
});

function masthead_rotator(){
  var images = [];
  var showing_image = 0;
  var fade_time = 2; //in seconds
  var image_time = 10; // in seconds
  var time = 5;
  // images = $('masthead').select('img');
  if($('masthead')){
    images = $('masthead').select('img');
    // if(console.log){console.log("Found masthead, rotating images");}
    images.each(function(i){
      i.hide();
      i.style.zIndex = '-100';
    });
    images[0].show();
    images[0].style.zIndex='0';
  
    new PeriodicalExecuter(function(){
      var previous_image = 0;
      if(showing_image >= (images.length - 1)){
        showing_image = 0;
        previous_image = images.length - 1;
      } else {
        showing_image++;
        previous_image = showing_image - 1;
      }
    
      images[previous_image].style.zIndex = '1';
    
      images[showing_image].show();
      images[showing_image].style.zIndex = '0';
    
      new Effect.Fade(images[previous_image], {duration:fade_time});
      setTimeout(function(){images[previous_image].style.zIndex = '-5';}, ((fade_time * 1000) + 200));
    
    }, image_time);
  }
}

function nav_set_here(){
  $('nav').select('a').each(function(anch){
    hreffer = anch.href.match(/[a-zA-Z0-9_-]+.html/)[0];
    matches = document.location.href.match(hreffer)
    if(matches && matches.length > 0) {
      anch.addClassName('here');
    }
  });
}

function vertical_center_callouts(){
  $$('div.callout').each(function(el){
    var div_height = el.getHeight();
    var p_height = el.down().getHeight();
    
    el.down().absolutize();
    var loop_safety = 0;
    while(loop_safety < 10 && p_height > div_height){

      el.down().setStyle({fontSize:Math.round((el.down().getStyle('font-size').replace(/[^0-9.]+/,'') * 0.75)) + 'px'});
      p_height = el.down().getHeight();
      loop_safety ++;
    }
    el.down().setStyle({'top': ((div_height - p_height)/2) + "px"});
    
  })
}
