
// Main object with custom code for Covenant's website.
//
var Covenant = new Class({

  initialize: function()
  {
    this.menu = new DropMenu(1);
    this.makeParentTabActive();
    this.collapseFeatures();
    this.setTabBoxTabs();
    this.makePullQuotes();
    this.warnOldBrowser();
  },

  // Find the parent tab of the current location by looking for a link that matches.
  // Make the parent active.
  makeParentTabActive: function()
  {
    if (!$$('#menu li[id^=tab]').some(function(tab) {
        return tab.getElements('a').some(function(a) {
          if (location.href.search(a.href) != -1 && !a.hasClass('tabIgnore')) {
            tab.addClass('active');
            return true; // stop loops
          }})
        })
      ) {
      $$('#tabHome').addClass('active');
    }
  },

  collapseFeatures: function()
  {
    $$('#features .post').each(function(p) {
      var story = p.getElement('.story');
      var s = new Fx.Slide(story);
      s.hide();
      story.setStyle('display', 'block');  // override initial CSS hiding
      p.getElement('.title').addEvent('click', function(e) { e = new Event(e); s.toggle(); e.stop(); });
      p.getElement('div.date').addEvent('click', function(e) { e = new Event(e); s.toggle(); e.stop(); });
    });
  },

  setTabBoxTabs: function()
  {
    if ($('articleTabBox')) {
      new TabSwapper({
        selectedClass: 'on',
        deselectedClass: 'off',
        mouseoverClass: 'over',
        tabs: $$('#articleTabBox li'),
        clickers: $$('#articleTabBox li a'),
        sections: $$('div.panelSet div.panel'),
        smooth: true,
        smoothSize: true
      });
      $$('#articleTabBox .panel').each(function(e){e.setStyle('visibility','visible')});
      $('articleTabBox').getElement('div.panelSet').removeClass('preload');
    }
  },

  makePullQuotes: function()
  {
    $$('#posts .story span.pullquote').each(function(s) {
        s.clone().addClass('pullquoteShow').injectBefore(s.parentNode);
      });
  },

  warnOldBrowser: function()
  {
    if (window.ie6) {
      var d = document.createElement('div');
      d = $(d);
      d.setStyles({
        padding: '2px 4px 2px',
        marginBottom: '10px',
        color: '#a00',
        backgroundColor: '#f0f0f0',
        border: '1px solid #bbb',
        fontWeight: 'bold',
        fontSize: '11px',
        textAlign: 'center'
        });
      d.innerHTML = 'We recommend upgrading your old browser to <a href="http://www.microsoft.com/windows/downloads/ie/getitnow.mspx">IE 7</a> or <a href="http://www.getfirefox.com/">Firefox 3</a> for optimal viewing of this site.';
      d.injectTop('bar');

    }
  }

});

// Add drop down behavior to the nav menu tabs
//
var DropMenu = new Class({

  initialize: function()
  {
    this.hideDelay = 400;
    this.pendTimer = null;
    this.viz = -1;
    this.tabs = $$('#menu ul.top a.tab');
    this.drops = $$('#menu div.drop');

    for (var i = 0; i < this.tabs.length; i++)
    {
      // Tabs:
      //  enter: show drop, detect switch to hide
      //  leave: set timer to hide
      //
      this.tabs[i].addEvents({

        'mouseenter': function(ev, i) {
            // switching tabs: hide the other one
            if (this.viz != i) {
              if (this.viz >= 0)
                this.hideDrop(this.viz);
              this.showDrop(i);
            }
            if (this.pendTimer !== null) {
              $clear(this.pendTimer);
              this.pendTimer = null;
            }
          }.bindWithEvent(this, i),

        'mouseleave': function(ev, i) {
            var d = this.drops[i].childNodes.length > 0 ? this.hideDelay: 10;
            this.pendTimer = function(i) {
              if (this.pendTimer !== null)
                this.hideDrop(i);
            }.delay(d, this, i);

          }.bindWithEvent(this, i)

      });

      // Drops:
      //  enter: cancel timer
      //  leave: set timer
      //
      this.drops[i].addEvents({
        'mouseenter': function(ev) {
          if (this.pendTimer !== null) {
            $clear(this.pendTimer);
            this.pendTimer = null;
          }
        }.bindWithEvent(this),

        'mouseleave': function(ev, i) {
            this.pendTimer = function(i) {
              if (this.pendTimer !== null)
                this.hideDrop(i);
            }.delay(this.hideDelay, this, i);

          }.bindWithEvent(this, i)

      });
    }
  },

  hideDrop: function(i)
  {
    this.tabs[i].removeClass('hover');
    this.drops[i].setStyle('display','none');
    this.viz = -1;
    if (this.pendTimer !== null) {
      $clear(this.pendTimer);
      this.pendTimer = null;
    }
  },

  showDrop: function(i)
  {
    this.tabs[i].addClass('hover');
    if (this.drops[i].childNodes.length > 0)
      this.drops[i].setStyle('display','block');
    this.viz = i;
  }

});

Window.addEvent('domready', function() { new Covenant(1); });


