/**
 * Adds an event handler to a DOM element, returning whether it was successful.
 * 
 * @param object element - The DOM element.
 * @param string type - The type of event, without the "on" prefix.
 * @param object handler - The event handler function.
 * @return boolean
 */
function _add_event_handler(element, type, handler)
{
    if (element.addEventListener)
    {
        element.addEventListener(type, handler, false);
        return true;
    }
    else if (element.attachEvent)
    {
        return element.attachEvent('on' + type, handler);
    }
    else
    {
        return false;
    }
}

/**
 * Adds a window "onload" event handler without removing any existing onload handlers,
 * returning whether it was successful.
 * 
 * @param object handler - The event handler function.
 * @return boolean
 */
function _add_onload(handler)
{
    return _add_event_handler(window, 'load', handler);
}


_add_onload(function ()
{
    if (document.getElementById)
    {
        var masthead = document.getElementById('page-masthead');
        
        if (masthead)
        {
            // Pick a random masthead background image from this list.
            var bgimages = [];
            
            // http://www.pps.k12.or.us/schools/madison/2365.htm
            bgimages.push('http://www.pps.k12.or.us/schools/madison/template/Madison_Poetry_Reading_Fade.gif');
            
            // http://www.pps.k12.or.us/schools/madison/2336.htm
            bgimages.push('http://www.pps.k12.or.us/schools/madison/template/Madison_Leaders.gif');
            
            // http://www.pps.k12.or.us/schools/madison/1521.htm
            bgimages.push('http://www.pps.k12.or.us/schools/madison/template/Madison_FB.gif');
            
            // http://www.pps.k12.or.us/schools/madison/755.htm
            bgimages.push('http://www.pps.k12.or.us/schools/madison/template/Madison_Garden.jpg');
            
            // http://www.pps.k12.or.us/schools/madison/183.htm
            bgimages.push('http://www.pps.k12.or.us/schools/madison/template/hs_madison_02.jpg');
            
            // http://www.pps.k12.or.us/schools/madison/188.htm
            bgimages.push('http://www.pps.k12.or.us/schools/madison/template/Madison_SR_Class.jpg');
            
            // http://www.pps.k12.or.us/schools/madison/187.htm
            bgimages.push('http://www.pps.k12.or.us/schools/madison/template/Madison_Band_Parade.gif');
            

            if (bgimages.length > 0)
            {
                var i = Math.min(Math.floor(Math.random() * bgimages.length), bgimages.length - 1);
                
                masthead.style.backgroundImage = 'url(' + bgimages[i] + ')';
            }
        }
        
        // Enable the navbar's drop-down menus.
        var MenuBar1 = new Spry.Widget.MenuBar('nav-bar-main-menu', {imgRight:'http://www.pps.k12.or.us/schools/madison/template/SpryMenuBarRight.gif'});
        
        if (document.getElementById('nav-bar-subpages-menu'))
        {
            var MenuBar2 = new Spry.Widget.MenuBar('nav-bar-subpages-menu');
        }
        
        // Style the Google search box.
        var q = document.getElementById('search-box');
        
        if (q)
        {
            if (navigator.platform == 'Win32')
            {
                q.style.cssText = 'border: 1px solid #7e9db9; padding: 2px;';
            }
            
            q.onfocus = function ()
            {
                this.style.background = '#ffffff';
            };
            
            q.onblur = function ()
            {
                if (this.value == '')
                {
                    this.style.background = '#ffffff url(http://www.pps.k12.or.us/schools/madison/template/google_custom_search_watermark.gif) left no-repeat';
                }
            };
            
            if (!/[&?]q=[^&]/.test(location.search))
            {
                q.onblur();
            }
            
            
            
            // Avoid Internet Explorer memory leaks.
            q = null;
        }
    }
    
    
});
