/**
 * This file depends on the mootools and moofx javascript libraries.
 */

function hideDivs(container) {
    $(container).getChildren().filter(function(i) {
        return i.tagName == 'DIV';
    }).each(function(d) {
        d.setStyle('display', 'none');
    });
}

function showDiv(d) {
    hideDivs('primary');
    $(d).setStyle('display', 'block');
}

function showFromLocation(deflt) {
    var toShow = deflt;
    var id = location.href.indexOf('#');
    if (id != -1) {
        id = location.href.substring(id + 1);
        if ($(id)) toShow = id;
    }
    showDiv(toShow);
    $(toShow + '-tab').getElement('a').addClass('selected');
}

function tabClickHandler(e) {
    if (!e) var e = window.event;
    e = new Event(e);
    $ES('a', 'execorleader').each(function(a) { a.removeClass('selected'); });
    showDiv(e.target.href.substring(e.target.href.indexOf('#') + 1));
    e.target.addClass('selected');
    e.preventDefault();
}

function initializePage() {
    var ul = new Element('ul');
    ul.setProperty('id', 'execorleader');

    $ES('div', 'primary').each(function(d) {
        d.addClass('services-menu');

        var li = new Element('li');
        var a = new Element('a');

        a.setProperty('href', '#' + d.id);
        a.setHTML(d.getElement('h3').innerHTML);
        a.addEvent('click', tabClickHandler);
        a.injectInside(li);

        li.setProperty('id', d.id + '-tab');
        li.injectInside(ul);
    });

    ul.injectAfter($E('h2', 'primary'));

    $ES('h4', 'primary').each(function(h) {
        var a = h.getElement('a');
        var bq = h.getNext();
        var link = new Element('a');

        link.setProperty('href', a.href);
        link.setHTML(a.title);
        link.injectAfter(bq.getElement('p'));
        a.setProperty('title', 'Click to expand and collapse.');

        var slider = new Fx.Slide(bq);
        slider.hide();

        a.addEvent('click', function(e) {
            if (!e) var e = window.event;
            e = new Event(e);
            slider.toggle();
            e.preventDefault();
        });
    });

    showFromLocation('exec');

    // All done, show the primary content pane.
    $('primary').setStyle('visibility', 'visible');
}

if (document.createTextNode && document.getElementById) {
  // Hide the primary content pane while the page loads to prevent flicker.
  document.write('<style type="text/css">#primary { visibility: hidden; }</style>');
  window.onDomReady(initializePage);
}

