function stripe( tag, parentElement )
{
    var rowClass = "odd";

    cssQuery( tag, parentElement ).each( function( row )
                                          {
                                              addClassName( row, rowClass );

                                              rowClass = rowClass == 'odd' ? 'even' : 'odd';
                                          }
                                        );
}

// Creates an element with a given name
function ce( tag, name )
{
    if ( name && window.ActiveXObject )
    {
        element = document.createElement( '<' + tag + ' name="' + name + '">' );
    }
    else
    {
        element = document.createElement( tag );

        element.setAttribute( 'name', name );
    }

    return element;
}

// Adds a dependent fieldset before the specified element
function addDependent( before )
{
    // Go over, in a loop, all existing dependents to figure out the last one and add 1 to it.
    var count       = 1;
    var testElement = $i( 'dependent' + count + 'Name' );

    while ( exists( testElement ) )
    {
        var testElement = $i( 'dependent' + ++count + 'Name' );
    }

    // IE doesn't like adding name attributes this way, by the looks of it.  Let's see if this works; otherwise, the above 'ce' method may have to be used.
    var legend = createElement( 'legend', 'text', 'Enter dependent\'s details' );

    var input1 = createElement( 'input', 'name', 'dependent' + count, 'type', 'text', 'title', 'Enter the dependent\'s name', 'id','dependent' + count );
    var div1   = createElement( 'div', createElement( 'label', 'for', 'dependent' + count, 'text', 'Dependent\'s name' ), input1 );

    var input2 = createElement( 'input', 'name', 'dependentAge' + count, 'type', 'text', 'title', 'Enter the dependent\'s age', 'id','dependentAge' + count );
    var div2   = createElement( 'div', createElement( 'label', 'for', 'dependentAge' + count, 'text', 'Dependent\'s age' ), input2 );

    var select = createElement( 'select', 'name', 'dependentRelationship' + count, 'id', 'dependentRelationship' + count,
                                 createElement( 'option', 'value', 'Child', 'text', 'Child' ),
                                 createElement( 'option', 'value', 'Parent', 'text', 'Parent' ),
                                 createElement( 'option', 'value', 'Other', 'text', 'Other' )
                               );
    var div3   = createElement( 'div', createElement( 'label', 'for', 'dependentRelationship' + count, 'text', 'Relationship to you' ), select );

    var fieldset1 = createElement( 'fieldset', 'id', 'dependent' + count + 'Name', legend, div1, div2, div3 );

    before.parentNode.insertBefore( fieldset1, before );

    MyBehaviour.refresh();

    // Make sure the link doesn't do anything
    return false;
}

// If the specified field has no value, sets the value to the title (useful when the page first loads or when the element loses focus but is empty).
//
// Changed by Salman Halim on March 20, 2007 (14:13:37) to only work on those fields that are contained within divs that have a class of required.
function setFieldValue( el )
{
    if ( Element.extend( el ).up( 'div' ).hasClassName( 'required' ) && !isEmpty( el.title ) && isEmpty( el.value ) )
    {
        el.value = el.title;
    }
}

// If the specified field's value is the same as its title, empties out the value (useful when the field gets focus or during form submission).
function resetFieldValue( el )
{
    if ( el.title == el.value )
    {
        el.value = '';
    }
}

// Goes over all text fields and text areas in the form and resets those whose values are the same as their titles (were unchanged by the user).
function resetFields( theForm )
{
    if ( !isEmpty( theForm.id ) )
    {
        $$( '#' + theForm.id + ' input[type=text]' ).each( resetFieldValue );
        $$( '#' + theForm.id + ' textarea' ).each( resetFieldValue );
    }
}

// var myrules =
// {
//     'input' : function( el )
//     {
//         addEvent( el, 'focus', function() { addClassName( this, 'focus' ); } );
//         addEvent( el, 'blur', function() { removeClassName( this, 'focus' ); } );
//         addEvent( el, 'mouseover', function() { addClassName( this, 'mouseover' ); } );
//         addEvent( el, 'mouseout', function() { removeClassName( this, 'mouseover' ); } );
//     }
//
//     ,'input[type=text]' : function( el )
//     {
//         setFieldValue( el );
//         addEvent( el, 'focus', function() { resetFieldValue( this ); } );
//         addEvent( el, 'blur', function() { setFieldValue( this ); } );
//     }
//
//     ,'textarea' : function( el )
//     {
//         addEvent( el, 'focus', function() { addClassName( this, 'focus' ); resetFieldValue( this ); } );
//         addEvent( el, 'blur', function() { removeClassName( this, 'focus' ); setFieldValue( this ); } );
//         addEvent( el, 'mouseover', function() { addClassName( this, 'mouseover' ); } );
//         addEvent( el, 'mouseout', function() { removeClassName( this, 'mouseover' ); } );
//
//         setFieldValue( el );
//     }
//
//     ,'select' : function( el )
//     {
//         addEvent( el, 'focus', function() { addClassName( this, 'focus' ); resetFieldValue( this ); } );
//         addEvent( el, 'blur', function() { removeClassName( this, 'focus' ); setFieldValue( this ); } );
//         addEvent( el, 'mouseover', function() { addClassName( this, 'mouseover' ); } );
//         addEvent( el, 'mouseout', function() { removeClassName( this, 'mouseover' ); } );
//     }
//
//     ,'form' : function( el )
//     {
//         addEvent( el, 'submit', function() { resetFields( el ); } );
//     }
//
//     ,'label' : function( el )
//     {
//         var forId = getLabelId( el );
//
//         if ( !isEmpty( forId ) && exists( $( forId ) ) )
//         {
//             addEvent( el, 'mouseover', function() { addClassName( this, 'mouseover' ); addClassName( $( getLabelId( this ) ), 'mouseover' ); } );
//             addEvent( el, 'mouseout', function() { removeClassName( this, 'mouseover' ); removeClassName( $( getLabelId( this ) ), 'mouseover' ); } );
//         }
//     }
//
//     ,'#additionalDonation' : function( el )
//     {
//         addEvent( el, 'change', function() { $i( 'totalFees' ).value = parseInt( $i( 'fees' ).value ) + parseInt( this.value ); } );
//     }
//
//     ,'ol#bylaws h2' : function( el )
//     {
//         var anchor = createElement( 'a' );
//
//         anchor.setAttribute( 'title', 'Click to expand/collapse the contents of the "' + getInnerText( el ) + '" section.' );
//         anchor.setAttribute( 'href', 'javascript:void(0)' );
//         addEvent( anchor, 'click', function() { Element.extend( this ).up().next( 'ol' ).toggle(); return false; } );
//         anchor.innerHTML = el.innerHTML;
//
//         el.innerHTML = '';
//         el.appendChild( anchor );
//
//         Element.extend( el ).next( 'ol' ).hide();
//     }
//
//     ,'ol#bylaws' : function( el )
//     {
//         var anchor = createElement( 'a' );
//
//         addEvent( anchor, 'click', function()
//                   {
//                       // Does this element need to be extended in IE6 for the show to work?
//                       cssQuery( 'ol#bylaws h2 + ol' ).each( function( ol ) { ol.show(); } );
//                       /*
//                       $$( 'ol#bylaws h2' ).each( function( h2 )
//                                                  {
//                                                      var ol = h2.next( 'ol' );
//
//                                                      // For some reason, the Prototype selector doesn't match just the H2s.
//                                                      if ( exists( ol ) )
//                                                      {
//                                                          ol.show();
//                                                      }
//                                                  }
//                                                );
//                        */
//                   }
//                 );
//
//         anchor.appendChild( document.createTextNode( 'Expand all sections' ) );
//         anchor.setAttribute( 'href', 'javascript:void(0)' );
//         addClassName( anchor, 'wide' );
//
//         var anchor2 = createElement( 'a' );
//
//         addEvent( anchor2, 'click', function()
//                   {
//                       // Does this element need to be extended in IE6 for the hide to work?
//                       cssQuery( 'ol#bylaws h2 + ol' ).each( function( ol ) { ol.hide(); } );
//                       /*
//                       $$( 'ol#bylaws h2' ).each( function( h2 )
//                                                  {
//                                                      var ol = h2.next( 'ol' );
//
//                                                      // For some reason, the Prototype selector doesn't match just the H2s.
//                                                      if ( exists( ol ) )
//                                                      {
//                                                          ol.hide();
//                                                      }
//                                                  }
//                                                );
//                        */
//                   }
//                 );
//
//         anchor2.appendChild( document.createTextNode( 'Collapse all sections' ) );
//         anchor2.setAttribute( 'href', 'javascript:void(0)' );
//         addClassName( anchor2, 'wide' );
//
//         el.parentNode.insertBefore( anchor, el );
//         el.parentNode.insertBefore( document.createTextNode( ' / ' ), el );
//         el.parentNode.insertBefore( anchor2, el );
//     }
//
//     /*
//     ,'form div' : function( el )
//     {
//         addEvent( el, 'mouseover', function() { addClassName( this, 'mouseover' ); } );
//         addEvent( el, 'mouseout', function() { removeClassName( this, 'mouseover' ); } );
//     }
//      */
// };
//
// Behaviour.register( myrules );

var newRules =
{
    'input, textarea' : function( el )
    {
        addFocusBlur( el );
    }

    ,'input, textarea, select, fieldset, .striped tr, ul.events li, ul#boardMembers li' : function( el )
    {
        addHover( el );
    }

    ,'input[type=text], textarea' : function( el )
    {
        setFieldValue( el );
        addFocusBlur( el, function() { resetFieldValue( this ); }, function() { setFieldValue( this ); } );
    }

    ,'form:submit' : function( el )
    {
        resetFields( el );
    }

    ,'label' : function( el )
    {
        var forId = getLabelId( el );

        if ( !isEmpty( forId ) && exists( $i( forId ) ) )
        {
            addHover( el,
                      function() { addClassName( this, 'mouseover' ); addClassName( $i( getLabelId( this ) ), 'mouseover' ); },
                      function() { removeClassName( this, 'mouseover' ); removeClassName( $i( getLabelId( this ) ), 'mouseover' ); } );
        }
    }

    ,'#additionalDonation:change' : function( el )
    {
        $i( 'totalFees' ).innerHTML = '$' + ( parseInt( $i( 'fees' ).innerHTML.replace( /^\$/, '' ) ) + parseInt( el.value.replace( /^\$/, '' ) ) );
    }

    ,'ol#bylaws h2' : function( el )
    {
        var anchor = createElement( 'a' );

        anchor.setAttribute( 'title', 'Click to expand/collapse the contents of the "' + getInnerText( el ) + '" section.' );
        anchor.setAttribute( 'href', 'javascript:void(0)' );
        MyBehaviour.addRemovableEvent( anchor, 'click', function() { Element.extend( this ).up().next( 'ol' ).toggle(); return false; } );
        anchor.innerHTML = el.innerHTML;

        el.innerHTML = '';
        el.appendChild( anchor );

        Element.extend( el ).next( 'ol' ).hide();
    }

    ,'ol#bylaws' : function( el )
    {
        var anchor = createElement( 'a' );

        MyBehaviour.addRemovableEvent( anchor, 'click', function() { cssQuery( 'ol#bylaws h2 + ol' ).each( function( ol ) { ol.show(); } ); } );

        anchor.appendChild( document.createTextNode( 'Expand all sections' ) );
        anchor.setAttribute( 'href', 'javascript:void(0)' );
        addClassName( anchor, 'wide' );

        var anchor2 = createElement( 'a' );

        MyBehaviour.addRemovableEvent( anchor2, 'click', function() { cssQuery( 'ol#bylaws h2 + ol' ).each( function( ol ) { ol.hide(); } ); } );

        anchor2.appendChild( document.createTextNode( 'Collapse all sections' ) );
        anchor2.setAttribute( 'href', 'javascript:void(0)' );
        addClassName( anchor2, 'wide' );

        el.parentNode.insertBefore( anchor, el );
        el.parentNode.insertBefore( document.createTextNode( ' / ' ), el );
        el.parentNode.insertBefore( anchor2, el );
    }

    ,'a[href=TODO]' : function( el )
    {
        el.onclick = function()
        {
            alert( "Unfortunately, the link pointing to '" + this.innerHTML + "' has not yet been implemented; we apologize for the inconvenience." );
            return false;
        };
    }

    ,'form[action=TODO]' : function( el )
    {
        el.onsubmit = function()
        {
            alert( "ANONYMOUS this.outerHTML:  " + this.outerHTML );
            return false;
        };
    }

    ,'tbody.striped' : function( el )
    {
        stripe( 'tr', el );
    }

    ,'ul.striped' : function( el )
    {
        stripe( 'li', el );
    }
};

MyBehaviour.addRules( newRules );

function setupLightboxImages()
{
    var i = 0;
    var lightbox = $( 'lightbox' + i );

    // Keep going as long as an element with that ID matches.
    while ( exists( lightbox ) )
    {
        new Control.Modal( lightbox, { opacity: 0.75 } );

        lightbox = $( 'lightbox' + ++i );
    }
}

addLoadEvent( setupLightboxImages );
