/* ----------------------------------------------------------------------------
 General DHTML Javascript Library
 
 This library collects those function that are used time-and-again for simple
 DHTML operations (accessing CSS properties, event handling).
 
 This is not an API, framework, application, or whatever else...it's just a
 collection of useful function.
 
 
 license: Use freely but include credit to the author.
 author:  Alex Mikitik
 since:   March 2, 2005
---------------------------------------------------------------------------- */


/* 
 * Returns a uniquely name page element, regardless of browser DOM.
 * 
 * NOTE: This will not fetch nested layers in Netscape 4.
 * 
 * The returned object is DOM Element (as found in the DOM core specification
 * at http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/).  In addition to the
 * standard contents, the Element has an extra property and two new methods:
 *      css             - The element's style information.  This is a shortcut
 *                        to the element's style (CSS) properties.  You can
 *                        access any of these using:
 *                                object.css.property-name
 *      addEvent()      - Adds an event handler
 *      removeEvent()   - Removes an event handler
 * 
 * param:   name (string) - The unique name of a page element.
 * return:  An object containing the Element and style information of the
 *          element.  If the object wasn't found, null is returned.
 */
function getElement(name) {
    
    // Get the object
    if (document.getElementById)    var e = document.getElementById(name);
    else if (document.all)          var e = document.all[name];
    else if (document.layers)       var e = document.layers[name];
    else                            var e = null;
    
    // Assign custom properties
    if (e != null) {
        e.css = (document.layers) ? e : e.style;
        e.addEvent = addEvent;
        e.removeEvent = removeEvent;
        
        e.show = function () { this.css.display = 'block'; };
        e.hide = function () { this.css.display = 'none'; };
    }
    
    return e;
}


/* 
 * Simple wrapper function that determines whether an element is undefined.
 * 
 * return:  TRUE if the element is undefined, otherwise FALSE.
 */
function isUndefined(a) {
    return typeof a == 'undefined';
}


/* 
 * Registers an event on a given element.
 * 
 * The function you register should be written to access an element's
 * properties using the 'this' keyword.  For example:
 * 
 *  function changeBackground() {
 *      this.style.background = #aaa;
 *  }
 * 
 * Currently, only one function may be registered per event.  Any additional
 * functions registered will only replace the previous one.
 * 
 * param:   event (string)  - Name of the event to register.
 * param:   method (string) - Name of the function (with NO parentheses)
 */
function addEvent(event, method){
    this[event] = method;
    if(this.captureEvents) this.captureEvents(Event[event.toUpperCase()]);
}


/* 
 * Removes an event handler from a given element.
 * 
 * param:   event (string)   - Name of the event to remove event from.
 */
function removeEvent(event){
    this[event] = null;
    if(this.releaseEvents) this.releaseEvents(Event[event.toUpperCase()]);
}


/* 
 * Convenience function for showing block page elements.
 * 
 * param: element (string) - Unique name of block level element to show.
 */
function show(element) {
    var e = getElement(element);
    e.css.display = 'block';
}


/* 
 * Convenience function for hiding block page elements.
 * 
 * param: element (string) - Unique name of block level element to hide.
 */
function hide(element) {
    var e = getElement(element);
    e.css.display = 'none';
}


/* 
 * Initializes the event handler for the help menu.
 * 
 * This function will initialize an event handler for the help menu only if
 * there is a 'help-button' element in the page.
 */
function initHelpMenu() {
    var help  = getElement('help-button');
    var menu  = getElement('help-menu');
    var close = getElement('help-close');
    
    if (help != null) {
        help.addEvent('onclick',
            function() {
                if (menu.css.display == 'none' ||
                    menu.css.display == '') {
                    menu.show();
                    help.hide();
                }
            });
    }
    
    if (close != null) {
        close.addEvent('onclick',
            function() {
                menu.hide();
                help.show();
            });
    }
}


// These defaults should be changed the way it best fits your site
var _FIXED_FEATURES = 'location=0,status=0,resizable=1,scrollbars=0,menubar=0,width=400,height=350';
var _OPEN_FEATURES = 'location=0,status=0,resizable=1,scrollbars=0,menubar=0';

function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _FIXED_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, width, height) {
    
    // Determine popup size.  Will only display specified size if both
    // width and height were set.
    if (isUndefined(width) && isUndefined(height)) {
        features = _FIXED_FEATURES;
    } else {
        features = _OPEN_FEATURES + ",width=" + width + ",height=" + height;
    }
    
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}
