/*    
 $Id: contentexpanderController.js 20541 2009-06-16 14:41:08Z jon $
 (c) 2006 - 2009 The New York Times Company
 Description: Shows and hides content using an animation for the transition.
*/

NYTD.ContentExpander = Class.create({

  initialize: function(button, content) {    
    this.button = button;
    this.content = content;
    this.toggledClass = 'toggled';
    this.openedText = "Close";
    this.closedText = this.button.innerHTML;
    Event.observe(button, 'click', this.openContent.bindAsEventListener(this));
  },
  
  openContent: function(e) {
    Event.stop(e);
    this.button.stopObserving('click');
    this.button.update(this.openedText).addClassName(this.toggledClass).blur();
    new Effect.BlindDown(this.content);
    Event.observe(this.button, 'click', this.closeContent.bindAsEventListener(this));
  },
  
  closeContent: function(e) {
    Event.stop(e);
    this.button.stopObserving('click');
    this.button.removeClassName(this.toggledClass).blur();
    var that = this;
    new Effect.BlindUp(this.content, {
      afterFinish: function() {
        that.button.update(that.closedText);
      }
    });
    Event.observe(this.button, 'click', this.openContent.bindAsEventListener(this));
  }
});

Event.observe(window, "load", function() {
  $$('.toggleContent').each(function(module) {
    var button = module.down('.showContent');
    var content = module.down('.hiddenContent');
    var contentExpander = new NYTD.ContentExpander(button, content);
  });
});

