// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

document.observe('dom:loaded', function (){
      $$('div.staffer').each(function(staffEl){
       staffEl.select('[id*="person-opener"]').each(function(personOpener){
         personOpener.observe('click', function(clickEvent){
           var openerId = personOpener.id.split('-')[2];
           $('person-summary-' + openerId).hide();
           Effect.Appear('person-description-' + openerId);
           clickEvent.stop();
           });
         });

        staffEl.select('[id*="person-closer"]').each(function(personCloser){
         personCloser.observe('click', function(clickEvent){
           var closerId = personCloser.id.split('-')[2];
           $('person-description-' + closerId).hide();
           Effect.Appear('person-summary-' + closerId);
           clickEvent.stop();
           });
         });
      });

      $$('div.enewsletter-listing').each(function(enews){
        var enewsId = enews.id.split('-')[2];
        $('enewsletter-listing-summary-toggle-' + enewsId).observe('click', function(clickEvent){
          $('enewsletter-listing-summary-' + enewsId).hide();
          Effect.Appear('enewsletter-listing-full-content-' + enewsId);
          clickEvent.stop();
        });
        $('enewsletter-listing-full-content-toggle-' + enewsId).observe('click', function(clickEvent){
          $('enewsletter-listing-full-content-' + enewsId).hide();
          Effect.Appear('enewsletter-listing-summary-' + enewsId);
          clickEvent.stop();
        });

      });

      $$('div.gallery').each(function(gallery){
         gallery.select('[id*="gallery-toggle"]').each(function(galleryOpener){
           galleryOpener.observe('click', function(clickEvent){
             var openerId = galleryOpener.id.split('-')[2];
             if($('gallery-images-list-' + openerId).visible()){
               Effect.Fade('gallery-images-list-' + openerId);
               $('gallery-toggle-' + openerId).update('view photos &raquo;');
               clickEvent.stop();
             } else {
               Effect.Appear('gallery-images-list-' + openerId);
               $('gallery-toggle-' + openerId).update('hide photos &laquo;');
               clickEvent.stop();
             }
           });
         });
     });

      $$('div.archived-event').each(function(archivedEvent){
         archivedEvent.select('[id*="archived-event-toggle"]').each(function(eventOpener){
           eventOpener.observe('click', function(clickEvent){
             var openerId = eventOpener.id.split('-')[3];
             if($('archived-events-image-list-' + openerId).visible()){
               Effect.Fade('archived-events-image-list-' + openerId);
               $('archived-event-toggle-' + openerId).update('view photos &raquo;');
               clickEvent.stop();
             } else {
               Effect.Appear('archived-events-image-list-' + openerId);
               $('archived-event-toggle-' + openerId).update('hide photos &laquo;');
               clickEvent.stop();
             }
           });
         });
     });

     if($('menu')){
       $('menu').select('[href="/about/annual-reports"]').each(function(annRepLink){
            annRepLink.addClassName('nolink');
              annRepLink.observe('click',function(clickEvent){
                clickEvent.stop();
              });
       });
      }
     if($('submenu')){
      $('submenu').select('[href="/about/annual-reports"]').each(function(annRepLink){
            annRepLink.addClassName('nolink');
            annRepLink.observe('click',function(clickEvent){
              $('annrepsubmenu').toggle();
              clickEvent.stop();
            });
            var containingLI = annRepLink.up();
            if(! containingLI.hasClassName('open')){
              containingLI.insert('<ul style="display: none;" id="annrepsubmenu"><li class="depth-2"><a href="/about/annual-reports/2009">2009 Report</a></li><li class="depth-2"><a href="/about/annual-reports/2008">2008 Report</a></li></ul>');
            } else {
              containingLI.insert('<span id="annrepsubmenu"></span>');
            }
       });
     }
});

TableKit.Sortable.addSortType(
    new TableKit.Sortable.Type('location_sort_customization', {
      normal : function(v) {
        if(v.match(/(.+),\s+MA/gm)){
          return RegExp.$1;
        } 
      }
    }
  )
);

sfHover = function() {
  var sfEls = document.getElementById("menu").getElementsByTagName("LI");
  for (var i=0; i<sfEls.length; i++) {
    sfEls[i].onmouseover=function() {
      this.className+=" sfhover";
    }
    sfEls[i].onmouseout=function() {
      this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
    }
  }
}

// IE-only method of attaching an event to window loading.
if (window.attachEvent) window.attachEvent("onload", sfHover);
