/**
 * Routines for interacting with users via error/warning notifications.
 *
 * error_notify() checks for the existence of a stylesheet for #error_notify (or
 * whatever you set error_notify_id to), and if it does not exist, will provide
 * a basic color set for your error notification box.
 *
 * @url         $URL: svn+ssh://xris@svn.schedulesdirect.org/data/svn/web/schedulesdirect.org/htdocs/js/user_errors.js $
 * @date        $Date: 2008-12-07 00:34:02 +0000 (Sun, 07 Dec 2008) $
 * @version     $Revision: 874 $
 * @author      $Author: xris $
 * @copyright   Silicon Mechanics
 * @license     LGPL
 *
 * @package     SiMech
 * @subpackage  Javascript
 *
 * @uses        browser.js
 * @uses        utils.js
 *
/**/

/**
 * Provide fancy javascript display of an already-rendered user_errors element
/**/
    function display_user_errors() {
    // Make sure browser is defined
        if (!browser) {
            alert('Developer Error:  browser.js is needed for notify.js, but was not loaded.');
            return;
        }
    // Get some info about the window so we can position the notification appropriately
        var window_width = 0, window_height = 0;
        if (window.innerHeight) {                                                     // all except Explorer
            window_width  = window.innerWidth;
            window_height = window.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            window_width  = document.documentElement.clientWidth;
            window_height = document.documentElement.clientHeight;
        }
        else if (document.body) {                                                     // other Explorers
            window_width  = document.body.clientWidth;
            window_height = document.body.clientHeight;
        }
    // Do we need to create a buffer to go beneath the error?
        var buffdiv = $('user_errors_buffer');
        if (buffdiv) {
            buffdiv.style.visibility = 'hidden';
            buffdiv.style.display    = '';
        }
        else {
        // Create a div to prevent clicks into the main page content
            buffdiv = document.createElement('div');
            buffdiv.id                    = 'user_errors_buffer';
            buffdiv.style.visibility      = 'hidden';
            buffdiv.style.zIndex          = 9998;
            buffdiv.style.backgroundColor = '#ffffff';
            buffdiv.style.height          = '100%';
            buffdiv.style.width           = '100%';
            buffdiv.style.top             = '0px';
            buffdiv.style.left            = '0px';
            buffdiv.style.bottom          = '0px';
            buffdiv.style.right           = '0px';
            buffdiv.style.opacity         = .70;
            buffdiv.style.MozOpacity      = .70;
            buffdiv.style.filter          = 'alpha(opacity=70)';
            buffdiv.style.scroll          = 'none';
        // IE < 7 gets position:absolute with magic onscroll movement
            if (browser.is_ie && browser.v_ie < 7)
                buffdiv.style.position = 'absolute';
            else
                buffdiv.style.position = 'fixed';
        // And don't forget to add it to the document
            document.body.appendChild(buffdiv);
        }
    // Show the commands div
        $('user_errors_commands').style.display = '';
    // Load the error div
        var div = $('user_errors');
        div.style.zIndex     = 9999;
        div.style.visibility = 'hidden';
        div.style.display    = '';
        div.addClassName('js');
    // IE < 7 gets position:absolute with magic onscroll movement
        if (browser.is_ie && browser.v_ie < 7)
            div.style.position = 'absolute';
        else
            div.style.position = 'fixed';
    // Add the special IE event handlers to reposition the error handlers
    // if the user tries to scroll the page, etc.
        if (browser.is_ie && browser.v_ie < 7) {
            Event.observe(window,   'scroll', display_user_errors);
            Event.observe(document, 'click',  display_user_errors);
        }
    // Center the div on the page
        var top  = parseInt((window_height - div.offsetHeight) / 2);
        var left = parseInt((window_width  - div.offsetWidth)  / 2);
    // IE gets special position:absolute treatment to account for scrolling
        if (browser.is_ie && browser.v_ie < 7) {
            div.style.top      = (top  + document.documentElement.scrollTop)  + 'px';
            div.style.left     = (left + document.documentElement.scrollLeft) + 'px';
            buffdiv.style.top  = document.documentElement.scrollTop  + 'px';
            buffdiv.style.left = document.documentElement.scrollLeft + 'px';
            buffdiv.style.height = window_height + 'px';
            buffdiv.style.width  = window_width  + 'px';
        }
        else {
            div.style.top      = top  + 'px';
            div.style.left     = left + 'px';
            buffdiv.style.top  = '0px';
            buffdiv.style.left = '0px';
        }
    // Show the divs
        buffdiv.style.visibility = 'visible';
        div.style.visibility     = 'visible';
    }

/**
 * Return the the error notification box created by display_user_errors() to
 * its non-javascript state.
/**/
    function reset_user_errors() {
    // Hide the things that we don't need anymore
        $('user_errors_buffer').style.display   = 'none';
        $('user_errors_commands').style.display = 'none';
    // Undo as much as we can
        var div = $('user_errors');
        div.style.height = '';
        div.style.width  = '';
        div.style.top    = '';
        div.style.right  = '';
        div.style.bottom = '';
        div.style.left   = '';
        div.removeClassName('js');
        div.style.position       = 'relative';
    // Remove the special IE events
        if (browser.is_ie && browser.v_ie < 7) {
            Event.stopObserving(window,   'scroll', display_user_errors);
            Event.stopObserving(document, 'click',  display_user_errors);
        }
    }

/**
 * Dismiss the error notification box created by display_user_errors()
/**/
    function hide_user_errors() {
        $('user_errors').style.display        = 'none';
        $('user_errors_buffer').style.display = 'none';
    // Remove the special IE events
        if (browser.is_ie && browser.v_ie < 7) {
            Event.stopObserving(window,   'scroll', display_user_errors);
            Event.stopObserving(document, 'click',  display_user_errors);
        }
    }

