
YAHOO.namespace("drewb.util.accordion");

/*
 * Sectional accordion animation
 */
YAHOO.drewb.util.accordion = new function()
{
  this.hdClass       = "";
  this.hdClassActive = "";
  this.bdClass       = "";
  this.duration      = "";
  this.section       = "";

  this.clickEventDependent = function(e)
  {
    location.hash = e.target.childNodes[0].nodeValue.replace(" ", "_");

    var oSelf = YAHOO.drewb.util.accordion;
    var elOldHd = YAHOO.util.Dom.getElementsByClassName(oSelf.hdClassActive)[0];
    
    if(elOldHd)
    {
      var elOldBd = oSelf.findContent(elOldHd);
      oSelf.closeSection(elOldHd, elOldBd);
    }
    
    var elNewBd = oSelf.findContent(this);
    
    oSelf.openSection(this, elNewBd);
  };
    
  this.clickEventIndependent = function(e)
  {
    var oSelf = YAHOO.drewb.util.accordion;
    var elNewBd = oSelf.findContent(this);
    
    if ( YAHOO.util.Dom.hasClass(this, oSelf.hdClassActive) )
    {
      oSelf.closeSection(this, elNewBd);
    }
    else
    {
      oSelf.openSection(this, elNewBd);
    }
  };
    
  this.findContent = function(el)
  {
    if(!el)
    {
      return null;
    }
        
    var el = el.nextSibling;
    
    while(el.className != this.bdClass)
    {
      el = el.nextSibling;
    }
        
    return el;
  };
    
  this.closeSection = function(elHd, elBd, duration)
  {
    var attributes = {
      height: { to: 0}
    };

    if ( (duration == undefined) || (isNaN(duration)) )
    {
      duration = this.duration;
    }
    
    var closeAnim = new YAHOO.util.Anim(elBd, attributes, duration);
    closeAnim.animate();
    YAHOO.util.Dom.removeClass(elHd, this.hdClassActive);
  };
  
  this.closeAll = function(duration)
  {
    var hds = YAHOO.util.Dom.getElementsByClassName(this.hdClass);

    if ( (duration == undefined) || (isNaN(duration)) )
    {
      duration = this.duration;
    }
    
    var hdsLength = hds.length;
    
    for ( var i = 0; i < hdsLength; i++ )
    {
      if ( ! YAHOO.util.Dom.getElementsByClassName(this.bdClass)[i] )
        {
          return null;
        }

      var hd = YAHOO.util.Dom.getElementsByClassName(this.hdClass)[i];
      var bd = YAHOO.util.Dom.getElementsByClassName(this.bdClass)[i];
      this.closeSection(hd, bd, duration);
    }
  };
  
  this.closeAllFast = function()
  {
    this.closeAll(0.0);  
  };
    
  this.openSection = function(elHd, elBd, duration)
  {
    var attributes = {
      height: { to: elBd.scrollHeight }
    };
    
    if ( (duration == undefined) || (isNaN(duration)) )
    {
      duration = this.duration;
    }
    
    var openAnim = new YAHOO.util.Anim(elBd, attributes, duration);
    openAnim.onComplete.subscribe(function(){
      var el = this.getEl();
      el.style.height="auto";
    });

    openAnim.animate();
    YAHOO.util.Dom.addClass(elHd, this.hdClassActive);
  };
  
  this.openAll = function(duration)
  {
    var hds = YAHOO.util.Dom.getElementsByClassName(this.hdClass);
    
    if ( (duration == undefined) || (isNaN(duration)) )
    {
      duration = this.duration;
    }

    var hdsLength = hds.length;
    
    for ( var i = 0; i < hdsLength; i++ )
    {
      if ( ! YAHOO.util.Dom.getElementsByClassName(this.bdClass)[i] )
        {
          return null;
        }
      
      var hd = YAHOO.util.Dom.getElementsByClassName(this.hdClass)[i];
      var bd = YAHOO.util.Dom.getElementsByClassName(this.bdClass)[i];
      this.openSection(hd, bd, duration);
    }
  };
  
  this.openAllFast = function()
  {
    this.openAll(0.0);
  };

  this.openFirst = function(duration)
  {
    if ( (duration == undefined) || (isNaN(duration)) )
    {
      duration = this.duration;
    }
    
    var hd = YAHOO.util.Dom.getElementsByClassName(this.hdClass)[0];
    var bd = YAHOO.util.Dom.getElementsByClassName(this.bdClass)[0];
    this.openSection(hd, bd, duration);
  };
  
  this.openFirstFast = function()
  {
    this.openFirst(0.0);
  };

  this.findSectionIdxByText = function(text)
  {
    var sections = YAHOO.util.Dom.getElementsByClassName(this.hdClass, "h4");
    var cnt = sections.length;

    for ( var i = 0; i < cnt; i++ )
    {
      if ( sections[i].innerHTML == text )
      {
        return i;
      }
    }

    return 0;
  };

  this.openSectionFast = function(text)
  {
    var sectionIdx = this.findSectionIdxByText(text);
    this.openNumber(sectionIdx);
  };

  this.openNumber = function(idx)
  {
    var hd = YAHOO.util.Dom.getElementsByClassName(this.hdClass)[idx];
    var bd = YAHOO.util.Dom.getElementsByClassName(this.bdClass)[idx];
    this.openSection(hd, bd);
  };
    
  this.init = function(behavior,
                       initial,
                       duration,
                       event,
                       hdClass, 
                       hdClassActive,
                       bdClass)
  {
    // behavior should be one of 'independent' (default) or 'dependent'. 
    // Here, dependent indicates that opening one accordion will close all 
    // others.
    behavior = ((behavior) ? behavior : 'independent');
    
    // initial should be one of 'all', 'none', or 'first'.
    initial = ((initial) ? initial : 'all');
    
    // animation duration setting in seconds.
    this.duration = (
      ((duration == undefined) || isNaN(duration)) ? 0.2 : duration
    );
    
    // event type;
    event = ((event) ? event : 'click');

    // these are the class identifiers for the elements to operate over.
    this.hdClass       = ((hdClass) ? hdClass : 'accordionToggle');
    this.bdClass       = ((bdClass) ? bdClass : 'accordionContent');
    this.hdClassActive = ((hdClassActive) ? hdClassActive : 'accordionToggleActive');

    ( initial == 'all' ) ? this.openAllFast() : this.closeAllFast();

   
    if (  location.hash != "" )
    {
      this.openSectionFast(location.hash.substr(1).replace("_", " "));
    }
    else if ( initial == 'first' )
    {
      this.openFirstFast();
    }

    eHandler = ((behavior == 'independent') ? 
      this.clickEventIndependent : this.clickEventDependent);

    YAHOO.util.Event.addListener(
      YAHOO.util.Dom.getElementsByClassName(this.hdClass),
      event,
      eHandler
    );
  };
};

/*
 * Hide / show panel
 */
YAHOO.namespace("drewb.util.panel");

YAHOO.drewb.util.panel = new function()
{
  this.rPanel   = '';
  this.rButton  = '';
  this.lContent = '';
  
  this.rPanelWidthInitial    = 0;
  this.rBlockWidthInitial    = 0;
  this.lContentWidthInitial  = 0;

  this.clickEvent = function(e)
  {
    var oSelf = YAHOO.drewb.util.panel;

    if ( YAHOO.util.Dom.hasClass(oSelf.rPanel, 'panelShown') )
    {
      oSelf.panelHide();
    }
    else
    {
      oSelf.showLessContents();
    }
  };
  
  this.panelHide = function()
  {
    var oSelf = YAHOO.drewb.util.panel;
    var attributes = { width: { to: oSelf.rButtonWidthInitial } };
    var duration = 0.8;
    var hideAnim = new YAHOO.util.Anim(oSelf.rPanel, attributes, duration);
    
    hideAnim.onComplete.subscribe(oSelf.showMoreContents);
    hideAnim.animate();
    
    YAHOO.util.Dom.removeClass(oSelf.rPanel, 'panelShown');
    YAHOO.util.Dom.addClass(oSelf.rPanel, 'panelHidden');
    YAHOO.util.Dom.removeClass(oSelf.rButton, 'panelOpened');
    YAHOO.util.Dom.addClass(oSelf.rButton, 'panelClosed');
  };
  
  this.showMoreContents = function()
  {
    var oSelf = YAHOO.drewb.util.panel;
    var newWidth = oSelf.lContentWidthInitial 
                  + oSelf.rPanelWidthInitial 
                  - oSelf.rButtonWidthInitial;
    var attributes = { width: { to: newWidth } };
    var duration = 0.2;
    var moreAnim = new YAHOO.util.Anim(oSelf.lContent, attributes, duration);

    moreAnim.animate();
  };
  
  this.showLessContents = function()
  {
    var oSelf = YAHOO.drewb.util.panel;
    var attributes = { width: { to: oSelf.lContentWidthInitial } };
    var duration = 0.2;
    var lessAnim = new YAHOO.util.Anim(oSelf.lContent, attributes, duration);
    
    lessAnim.onComplete.subscribe(oSelf.panelShow);
    lessAnim.animate();
  };
  
  this.panelShow = function()
  {
    var oSelf = YAHOO.drewb.util.panel;
    var attributes = { width: { to: oSelf.rPanelWidthInitial } };
    var duration = 0.8;
    var showAnim = new YAHOO.util.Anim(oSelf.rPanel, attributes, duration);
    
    showAnim.animate();
    
    YAHOO.util.Dom.removeClass(oSelf.rPanel, 'panelHidden');
    YAHOO.util.Dom.addClass(oSelf.rPanel, 'panelShown');
    YAHOO.util.Dom.removeClass(oSelf.rButton, 'panelClosed');
    YAHOO.util.Dom.addClass(oSelf.rButton, 'panelOpened');
  };
  
  this.init = function(initiallyHide)
  {    
    this.rPanel   = document.getElementById('rightPanel');
    this.rButton  = document.getElementById('rightPanelButton');
    this.lContent = document.getElementById('leftContents');
    
    this.rPanelWidthInitial   = parseInt(YAHOO.util.Dom.getStyle(this.rPanel, 'width'));
    this.rButtonWidthInitial  = parseInt(YAHOO.util.Dom.getStyle(this.rButton, 'width'));
    this.lContentWidthInitial = parseInt(YAHOO.util.Dom.getStyle(this.lContent, 'width'));

    YAHOO.util.Event.addListener(this.rButton, 'click', this.clickEvent);
    
    var lContentWidthNew = this.lContentWidthInitial + this.rPanelWidthInitial - this.rButtonWidthInitial;

    YAHOO.util.Dom.setStyle(this.rPanel, 'width', this.rButtonWidthInitial + 'px');
    YAHOO.util.Dom.setStyle(this.lContent, 'width', lContentWidthNew + 'px');

    YAHOO.util.Dom.removeClass(this.rPanel, 'panelShown');
    YAHOO.util.Dom.addClass(this.rPanel, 'panelHidden');
    YAHOO.util.Dom.removeClass(this.rButton, 'panelOpened');
    YAHOO.util.Dom.addClass(this.rButton, 'panelClosed');
    
    if ( initiallyHide )
    {
      return;
    }
    else
    {
      this.showLessContents();
    }
  };
};

