function set_timestamp( givenElement )
{
    var myDate = new Date( document.lastModified );
    var myYear = myDate.getFullYear();
    var myMonth = monthName( myDate.getMonth() );
    var myDay = dayName( myDate.getDay() );
    var myDate = myDate.getDate();
    var myString = "This page was last updated on " + myDay + ", " + 
            myMonth + " " + myDate + ", " + myYear + " ";
    givenElement.innerHTML = myString + givenElement.innerHTML;
}
function monthName( monthNumber )
{
    var months = new Array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" );
    return months[ monthNumber ];
}
function dayName( dayNumber )
{
    var days = new Array( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" );
    return days[ dayNumber ];
}
function open_all_dropdowns()
{
    var myDropdowns = array_elements_by_name_with_className( "div", "dropdown" );
    apply_to_array( myDropdowns, open_dropdown );
}
function open_dropdown( givenDropdown )
{
    var closedChild = first_child_with_className( givenDropdown, "dropdown_closed" );
    if( closedChild == null ) closedChild = first_child_with_className( givenDropdown, "dropdown_body" );
    if( closedChild != null )
    {
        closedChild.className = "dropdown_open";
        make_caption( closedChild, "click the header to close" );
    }
}
function close_all_dropdowns()
{
    var myDropdowns = array_elements_by_name_with_className( "div", "dropdown" );
    apply_to_array( myDropdowns, close_dropdown );
}
function close_dropdown( givenDropdown )
{
    var openChild = first_child_with_className( givenDropdown, "dropdown_open" );
    if( openChild == null ) openChild = first_child_with_className( givenDropdown, "dropdown_body" );
    if( openChild != null )
    {
        openChild.className = "dropdown_closed";
    }
}
function initialize_body()
{
    var myDropdowns = array_elements_by_name_with_className( "div", "dropdown" );
    apply_to_array( myDropdowns, initialize_dropdown );
    
    var myOpenButtons = array_elements_by_name_with_className( "button", "open_dropdowns" );  
    apply_to_array( myOpenButtons, initialize_openButton );  

    var myCloseButtons = array_elements_by_name_with_className( "button", "close_dropdowns" );  
    apply_to_array( myCloseButtons, initialize_closeButton ); 
    
    var myTimestamps = array_elements_by_name_with_className( "p", "timestamp" );
    apply_to_array( myTimestamps, set_timestamp );  
}
function initialize_dropdown( givenDropdown )
{
    var myHead = first_child_with_className( givenDropdown, "dropdown_header" );
    myHead.onclick = function() { toggle_dropdown( givenDropdown ) };
    myHead.onmouseover = function() { highlight_dropdown( givenDropdown ) };
    myHead.onmouseout = function() { un_highlight_dropdown( givenDropdown ) };
    
    var myBody = first_child_with_className( givenDropdown, "dropdown_body" );
    if( myBody != null ) close_dropdown( givenDropdown );
}
function highlight_dropdown( givenDropdown )
{
    var headerChild = first_child_with_className( givenDropdown, "dropdown_header" );
    if( headerChild != null ) headerChild.style.backgroundColor = "gainsboro";
}
function un_highlight_dropdown( givenDropdown )
{
    var headerChild = first_child_with_className( givenDropdown, "dropdown_header" );
    if( headerChild != null ) headerChild.style.backgroundColor = "white";

}
function initialize_openButton( givenButton )
{
    givenButton.value = "OPEN ALL";
    givenButton.onclick = function () { open_all_dropdowns() };
}
function initialize_closeButton( givenButton )
{
    givenButton.value = "CLOSE ALL";
    givenButton.onclick = function () { close_all_dropdowns() };
}
function toggle_dropdown( givenDropdown )
{
    var closedChild = first_child_with_className( givenDropdown, "dropdown_closed" );
    var openChild = first_child_with_className( givenDropdown, "dropdown_open" );
    if( closedChild != null )
    {
        closedChild.className = "dropdown_open";
        make_caption( closedChild, "click the header again to close" );
    }
    else if( openChild != null )
    {
        openChild.className = "dropdown_closed";
    }
    else
    {
        alert( "can not toggle the element " + givenDropdown.outerHTML );
    }
}
function make_caption( parentNode, captionText )
{
    var oldCaption = first_child_with_className( parentNode, "dropdown_caption" );
    if( oldCaption != null ) parentNode.removeChild( oldCaption );
    
    var captionElement = document.createElement( "div" );
    parentNode.appendChild( captionElement );
    captionElement.className = "dropdown_caption";
    captionElement.appendChild( document.createTextNode( captionText ) );
}
function remove_children( givenParent )
{
    while( givenParent.hasChildNodes() )
    {
        givenParent.removeChild( givenParent.firstChild );
    }
}
function first_child_with_className( givenParent, givenClassName )
{
    for ( var i=0; i < givenParent.childNodes.length; i++ )
    {
        var currentChild = givenParent.childNodes[ i ];
        if( currentChild.className == givenClassName ) return currentChild;
    }
    return null;
}
function array_elements_by_name_with_className( givenElementName, givenClassName )
{
    // can I use XPath here?
    var newArray = new Array();
    var elementsWithGivenClassName = document.getElementsByTagName( givenElementName );
    for( var i = 0; i < elementsWithGivenClassName.length; i++ )
    {
        var currentElement = elementsWithGivenClassName( i );
        if( currentElement.className == givenClassName )
        {
            newArray[ newArray.length ] = currentElement;
        }
    }
    return newArray;
}
function apply_to_array( givenArray, givenFunction )
{
    for( var i = 0; i < givenArray.length; i++ )
    {
        givenFunction( givenArray[ i ] );
    }
}
