var openrootid = 0;
var ignoreclose = false;

addOnClickEvent( "setTimeout( closelastroot, 100)" );

function togglerootcat( id, pid ) {

    var menucell = document.getElementById( "menucell_" + id );
    var rootcat  = document.getElementById( id );

    if( toggledisplay( id ) ) {

        // We openned the item

        if( menucell ) {

            if( menucell.style ) {

                if( menucell.offsetWidth > rootcat.offsetWidth ) {

                    rootcat.style.width = (menucell.offsetWidth - 6); // 6 is the padding on rootcat

                }

            }

        }

        closelastroot();

        openrootid = id;

    } else {

        openrootid = 0;

    }

    ignoreclose = true;

}

function closelastroot() {

    if( ignoreclose ) {

        ignoreclose = false;

        return;

    }

    if( openrootid ) {

        // Another one is open

        if( toggledisplay( openrootid ) ) {

            // Oops, it really wasn't open

            toggledisplay( openrootid );

        } else {

            openrootid = 0;

        }

    } else {

    }

}
function setCSS( css_class, id ) {
    var element = document.getElementById( id );
    if( element ) {
        if( element.style ) {
            element.style = css_class;
        }
    }
}
/* A modal window is one which should keep the focus until it
 * is explicitly closed.
 */
var modal_window;
var modal_window_rval;

function modal_popup(url,w,h,props) {

    if( modal_window ) {
        if(!modal_window.closed) {
            modal_window.focus();
        }//if
    }//if

    if( ! props ) {
        var t = (screen.availHeight / 2) - (h/2);
        var l = (screen.availWidth / 2) - (w/2);
        window_props = "top="+t+",left="+l+",width="+w+",height="+h+",directories=no,toolbar=no,resizable=yes,menubar=no,scrollbars=yes,alwaysRaised=yes";
    } else {
        window_props = "width="+w+",height="+h+","+props;
    }//if

    modal_window = window.open(url, 'MODALWIN', window_props);

    addOnFocusEvent( "focusOnModalWindow();" );

}//modal_popup
    

function focusOnModalWindow() {

    if( defined("modal_window") ) {
        if(!modal_window.closed) {
            modal_window.focus();
            // stop();
        } else {
            removeOnFocusEvent();
        }
    }

}
if( document.all ) {

    document.onmousemove = _moveElement;
    document.onmouseup = _removeMoveTarget;

} else {

    window.captureEvents( Event.MOUSEMOVE, Event.MOUSEUP );
    window.onmousemove = _moveElement;
    window.onmouseup = _removeMoveTarget;

}

var movetarget = _clean_target();

function _clean_target() {

    t = new Object();

    t.id       = "";
    t.orig_x   = 0;
    t.orig_y   = 0;
    t.orig_mx  = 0;
    t.orig_my  = 0;
    t.active   = false;

    return t;

}

function setMoveTarget( mozevent, id ) {

    var elem = document.getElementById( id );

    if( elem ) {

        elem.style.position = "absolute";

        movetarget.id       = id;
        movetarget.active   = true;
        movetarget.orig_mx  = _event_x( mozevent );
        movetarget.orig_my  = _event_y( mozevent );
        movetarget.orig_x   = getOffsetLeft( elem );
        movetarget.orig_y   = getOffsetTop( elem );

    }

}

function _moveElement( mozevent ) {

    if( ! movetarget.active ) return;

    var id = movetarget.id;

    var mouse_x = _event_x( mozevent );
    var mouse_y = _event_y( mozevent );
    
    var delta_x = mouse_x - movetarget.orig_mx;
    var delta_y = mouse_y - movetarget.orig_my;

    var elem    = document.getElementById( id );

    var new_x = movetarget.orig_x + delta_x;
    var new_y = movetarget.orig_y + delta_y;

    elem.style.left = new_x;
    elem.style.top  = new_y;

}

function _removeMoveTarget() {

    movetarget = _clean_target();

}


function _event_x( mozevent ) {

    if( document.all ) return event.clientX;

    return mozevent.pageX;

}

function _event_y( mozevent ) {

    if( document.all ) return event.clientY;

    return mozevent.pageY;

}
var popupWindow;

function popup(url,w,h)
{

    if(popupWindow) {
        if(!popupWindow.closed) {
            popupWindow.focus();
        }
    }

    popupWindow=window.open(url, 'popupWindow', "width="+w+",height="+h+",directories=no,toolbar=no,resizable=yes,menubar=no,scrollbars=yes");

}
/* A quick window is one which closes when the opener regains
 * the focus.
 */
var quick_window;

function quick_popup(url,w,h) {

    if( quick_window ) {
        if(!quick_window.closed) {
            quick_window.focus();
        }//if
    }//if

    quick_window=window.open(url, 'MODALWIN', "width="+w+",height="+h+",directories=no,toolbar=no,resizable=yes,menubar=no,scrollbars=yes,alwaysRaised=yes");
    quick_window.onUnload = 'popup_onunload()';

    addOnFocusEvent( "focusOnQuickWindow();" );

}//quick_popup

function focusOnQuickWindow() {

    if( defined("quick_window") ) {
        if(!quick_window.closed) {
            quick_window.close();
            // stop();
        } else {
            removeOnFocusEvent();
        }
    }

}
var window_onResizeEvents = new Array();

window.onresize = window_onresize;

function window_onresize() {

    for( i in window_onResizeEvents ) {

        eval( window_onResizeEvents[i] );

    }//for

}//window_onresize

function addOnResizeEvent( event ) {

    push( window_onResizeEvents, event );

}
/*------------------------------------------------------------------------------
 * defined STRING
 *
 * example:
 *
 *  if( !defined( "document.body" ) ) alert( "Something is wrong" );
 *
 * true if STRING isn't 'undefined'.
 *
 * (written before getElementById was known about)
 *----------------------------------------------------------------------------*/

function defined(str) {

    if( eval("typeof(" + str + ")") != 'undefined' ) return true;

    return false;

}//defined

/*------------------------------------------------------------------------------
 * valid STRING
 *
 * example:
 *
 *  if( !valid( "document.frames" ) ) alert( "There are no frames" );
 *
 * Determine if an object is defined and not null in the current scope.  This
 * method safely navigates the STRING so you won't get javascript errors if the
 * parents of the object you're really interested in don't exist.
 *
 * (written before getElementById was known about)
 *----------------------------------------------------------------------------*/

function valid( object_address ) {

    var objects = object_address.split( "." );

    var address_path = new String();

    for( var i = 0; i < objects.length; i++ ) {

        if( address_path.length > 0 ) {

            address_path += ".";

        }//if

        address_path += objects[i];

        var address_result = eval("typeof(" + address_path + ")");

        if( address_result == 'undefined') {

            return false;

        } else if( eval( address_path + "== null" ) ) {

            return false;

        }//if

    }//for

    return true;

}//valid

/*------------------------------------------------------------------------------
 * printWindow
 *
 * Print the window.  (I think this is here b/c code needs to be added to get
 * the window to print on all browsers).
 *----------------------------------------------------------------------------*/

function printWindow() {

    window.print();

}//printWindow

/*------------------------------------------------------------------------------
 * showProperties ELEMENT
 *
 * For times when you don't have access to a javascript debugger and you want
 * to see the properties of some element.
 *----------------------------------------------------------------------------*/

function showProperties( el ) {

    var props = new String();

    for( i in el ) {

        if( (i.indexOf( "outer" ) > -1) || (i.indexOf( "inner" ) > -1) ) {

            // skip

        } else {

            props = props + i + ": " + el[i] + "\t";

            if( (i % 6) == 0 ) {

                props = props + "\n";

            }

        }

    }

    alert( props );

}//showProperties

/*------------------------------------------------------------------------------
 * getOffsetTop ELEMENT
 *
 * Get the offset top of an element relative to the window.
 *----------------------------------------------------------------------------*/

function getOffsetTop( element ) {

    if( !element ) return;

    var offset  = element.offsetTop;

    // element.offsetParent.style.position != "absolute"

    if( element.offsetParent ) {

        var pos = getCSSAttribute( element.offsetParent, "position" );

        if( pos != "absolute" ) offset += getOffsetTop( element.offsetParent );

    }

    return offset;

}//getOffsetTop

/*------------------------------------------------------------------------------
 * getOffsetLeft ELEMENT
 *
 * Get the offset left of an element relative to the window.
 *----------------------------------------------------------------------------*/

function getOffsetLeft( element ) {

    if( !element ) return;

    var offset  = element.offsetLeft;

    if( element.offsetParent  ) {

        var pos = getCSSAttribute( element.offsetParent, "position" );

       if( pos != "absolute" ) offset += getOffsetLeft( element.offsetParent );

    }

    return offset;

}//getOffsetLeft

/*------------------------------------------------------------------------------
 * getOffsetRight ELEMENT
 *
 * Get the offset left of an element relative to the window.
 *----------------------------------------------------------------------------*/

function getOffsetRight( elem ) {

    if( !elem ) return;

    var offset = getOffsetLeft( elem );

    var w = getElemWidth( elem );

    if( ieCompatable() ) w = elem.scrollWidth;

    offset += w;

    return offset;

}//getOffsetRight

function getElemWidth( elem ) {

    var width = 0;
    
    if( elem.offsetWidth ) width = elem.offsetWidth;

    if( elem.childNodes ) {

        for( var i in elem.childNodes ) {

            var child = elem.childNodes[i];

            var childw = getElemWidth( child );

            if( childw > width ) {

                width = childw;

            }

        }

    }

    return width;        

}

/*------------------------------------------------------------------------------
 * vscrollWindowToElement
 *
 * Vertically scroll the window so the element is viewable.  If the element is
 * near the top or bottom edge of the window, center the window around the 
 * element.
 *----------------------------------------------------------------------------*/

function vscrollWindowToElement( element ) {

    if( !element ) return;

    var h = window.innerHeight;
    var p = window.scrollY;
    var y = getOffsetTop( element );
    var z = element.offsetHeight;

    if( ieCompatable() ) {

        h = document.body.offsetHeight;
        p = document.body.scrollTop;
        z = element.scrollHeight;

    }//if

    var m       = p + h;
    var margin  = z;            // margin is the height of one element

    if( y < (p - z) || y > m ) {

        // Outside of viewable area, center

        var ypos = y - parseInt((h/2));

        window.scroll( 0, ypos );

    } else if( y < (p + margin) ) {

        // Upper limit

        window.scroll( 0, (y - margin) );

    } else if( y > (m - (margin + z)) ) {

        // Lower limit

        window.scroll( 0, (y - (h - (margin + z))) );

    }//if

}//vscrollWindowToElement

/*------------------------------------------------------------------------------
 * hscrollWindowToElement
 *
 * Horizontally scroll the window so the element is viewable.  If the element is
 * near the left or right edge of the window, center the window around the 
 * element.
 *----------------------------------------------------------------------------*/

function hscrollWindowToElement( element ) {

    if( !element ) return;

    var w = window.innerWidth;
    var p = window.scrollX;
    var x = getOffsetLeft( element );
    var z = element.offsetWidth;

    if( ieCompatable() ) {

        w = document.body.offsetWidth;
        p = document.body.scrollLeft;
        z = element.scrollWidth;

    }//if

    var m       = p + w;
    var margin  = z;            // margin is the width of one element

    if( x < (p - z) || x > m ) {

        // Outside of viewable area, center

        var ypos = x - parseInt((w/2));

        window.scroll( ypos, 0 );

    } else if( x < (p + margin) ) {

        // Upper limit

        window.scroll( (x - margin), 0 );

    } else if( x > (m - (margin + z)) ) {

        // Lower limit

        window.scroll( (x - (w - (margin + z))), 0 );

    }//if

}//hscrollWindowToElement

function changeSrc( id, new_src ) {

    var target = document.getElementById( id );

    if( target && new_src ) {

        target.src = new_src;

    }//if

}//changeSrc

/*------------------------------------------------------------------------------
 * getContentWindow IFRAME_ID, [ELEM_ID]
 *
 * Returns the inner contentWindow of an IFRAME element.  If ELEM_ID is set,
 * only return the contentWindow if it has an element by that ID.
 *----------------------------------------------------------------------------*/

function getContentWindow( id, verify ) {

    var content = document.getElementById( id );

    if( ! content ) {
    
        content = window.frames[id];

    }//if

    if( content ) {

        var cwin = content.contentWindow;

        if( verify ) {

            if( cwin.document.getElementById( verify ) ) {

                return content.contentWindow;

            }//if

        } else {

            return content.contentWindow;

        }//if

    }//if

    return false;

}//getContentWindow

/*------------------------------------------------------------------------------
 * getContentDocument FRAMEID
 *
 * Return the content document for the specified iframe.
 *----------------------------------------------------------------------------*/

function getContentDocument( frameid ) {

    var doc;

    if (document.all) {

        doc = frames[frameid].document;

    } else {

        try {

            var iframe = document.getElementById( frameid );

            var doc = iframe.contentDocument;

        } catch( err ) {

            // Quiet

        }

    }

    return doc;

}//getContentDocument


/*------------------------------------------------------------------------------
 * createActiveXObject OBJECT_ID
 *
 * Wrapper to create an object for IE or Netscape 7.1 and above.
 *
 * OBJECT_ID    The classid (text form) of the object to create.
 *----------------------------------------------------------------------------*/

function createActiveXObject( obj_id ) {

  var object;

  try {

    if(window.ActiveXObject) {

      obj = new ActiveXObject( obj_id );

    } else if (window.GeckoActiveXObject) {

        obj = new GeckoActiveXObject( obj_id );

    }//if

  } catch( ex ) {

    // Be quiet

    return;

  }//catch

  return obj;

}//createActiveXObject

/*------------------------------------------------------------------------------
 * getCenterX SUBTRACT
 *
 * Get the middle of the window (minus SUBTRACT if specified).
 *----------------------------------------------------------------------------*/

function getCenterX( objX ) {
    var w = window.innerWidth;
    if( ieCompatable() ) {
        w = document.body.offsetWidth;
    }
    if( objX ) {
        w -= objX;
    }
    return parseInt((w/2));
}//getCenterX

/*------------------------------------------------------------------------------
 * getCenterY SUBTRACT
 *
 * Get the middle of the window (minus SUBTRACT if specified).
 *----------------------------------------------------------------------------*/

function getCenterY( objY ) {
    var h = window.innerHeight;
    if( ieCompatable() ) {
        h = document.body.offsetHeight;
    }
    if( objY ) {
        h -= objY;
    }
    return parseInt((h/2));
}//getCenterY

/*------------------------------------------------------------------------------
 * resizeElementToParent ELEMID, [XPAD, YPAD]
 *
 * Resize an element to fit its parent's width and height (minus XPAD and YPAD
 * if specified).
 *----------------------------------------------------------------------------*/

function resizeElementToParent( id, xpad, ypad ) {

    var elem = document.getElementById( id );

    if( elem ) {

        var padre = elem.parentNode;

        if( padre ) {

            var h = padre.offsetHeight;
            var w = padre.offsetWidth;

            if( w ) {
                elem.style.width  = w - xpad;
            }

            if( h ) {
                elem.style.height = h - ypad;
            }

        }

    }

}//resizeElementToParent

/*------------------------------------------------------------------------------
 * resizeElementToParentWidth ELEMID, [XPAD]
 *
 * Resize an element's width to that of the parent (minus XPAD if provided).
 *----------------------------------------------------------------------------*/

function resizeElementToParentWidth( id, xpad ) {

    if( ! xpad ) xpad = 0;

    var elem = document.getElementById( id );

    if( elem ) {

        var padre = elem.parentNode;

        if( padre ) {

            var w = padre.offsetWidth;

            if( w ) {
                elem.style.width = w - xpad;
            }

        }

    }

}//resizeElementToParentWidth

/*------------------------------------------------------------------------------
 * toggledisplay ELEMID
 *
 * Returns true if we displayed the item, false if we hid it.
 *----------------------------------------------------------------------------*/

function toggledisplay( id ) {

    var elem = document.getElementById( id );

    if( elem ) {

        if( elem.style ) {

            if( elem.style.display == "block" ) {

                elem.style.display = "none";

                return false;

            } else {

                elem.style.display = "block";

                return true;

            }

        }

    }

}//toggledisplay

/*------------------------------------------------------------------------------
 * moveElement ID, WIDTH, HEIGHT, XPOS, YPOS, ZPOS
 *
 * Helper for resizing or moving an element.
 *----------------------------------------------------------------------------*/

function moveElement( id, w, h, x, y, z ) {

    var elem = document.getElementById( id );

    if( elem ) {

        if( elem.style ) {

            if( w ) elem.style.width = w;
            if( h ) elem.style.height = h;
            if( x ) elem.style.left = x;
            if( y ) elem.style.top = y;
            if( z ) elem.style.zIndex = z;

        }
    }

}//moveElement

/*------------------------------------------------------------------------------
 * setStyleById ELEMID, ATTRIBUTE, VALUE
 *
 * Set some style attribute on the element.
 *----------------------------------------------------------------------------*/

function setStyleById( id, attr, val ) {

    var elem = document.getElementById( id );

    if( elem ) {

        if( elem.style ) {

            eval( "elem.style." + attr + " = '" + val + "'" )

        }

    }

}//setStyleById

/*------------------------------------------------------------------------------
 * moveOnScreen ELEMID
 *
 * If part of the element is out of the visible area, move it back in.
 * Currently working for right-margin.
 *----------------------------------------------------------------------------*/

function moveOnScreen( id ) {

    var elem = document.getElementById( id );

    if( elem ) {

        if( elem.style ) {

            var win_w = window.innerWidth;

            if( ieCompatable() ) win_w = document.body.offsetWidth;

            var elem_r = getOffsetRight( elem );

            if( elem_r > win_w ) {

                var adj = win_w - elem_r;

                elem.style.left = elem.style.left - adj;

            }

        }

    }

}//moveOnScreen

/*------------------------------------------------------------------------------
 * join SEPARATOR, [THING, [THING,..]]
 *
 * Concatenation routing with homage to perl.
 *----------------------------------------------------------------------------*/

function join( separator ) {

    var result  = "";

    for( var i=1; i<arguments.length; i++) {

       result += arguments[i] + separator;

    }

    return result;

}//join

/*------------------------------------------------------------------------------
 * sendmail WHO, WHERE
 *
 * Mimic the anchor 'mailto' function
 *----------------------------------------------------------------------------*/

function sendmail( who, where ) {

    var href = "mailto:" + who + "@" + where;

    document.location = href;

}//sendmail

/*------------------------------------------------------------------------------
 * getCSSAttribute ELEMENT, CSSKEY
 *
 * Get the CSS attribute value.  First check the style, and then look at the
 * element's classes.
 *----------------------------------------------------------------------------*/

function getCSSAttribute( elem, key ) {

    var attr = false;

    if( elem ) {

        if( elem.style ) attr = eval( "elem.style." + key );

        return attr;

        // Stopped coding because a) the class which is found doesn't hold all
        // of the definitions, and b) this is rediculously expensive.  The 
        // intention is to get a style attribute for the current element.

        if( !attr ) {

            if( elem.className ) {

                var classes = elem.className.split( / / );

                for( var i = 0; !attr && (i < classes.length); i++ ) {

                    var klass = getCSSClass( classes[i] );

                    if( klass ) {

                        for( var j = 0; j < klass.style.length; j++ ) {

                            if( klass.style[j] == key ) {

                                var re    = new RegExp( key + "\s*:([^;]*);" );
                                var match = klass.cssText.search( re );

                                attr = match[0];

                            }

                        }

                        klass = false;

                    }

                }

            }

        }

    }

    return attr;

}//getCSSAttribute

/*------------------------------------------------------------------------------
 * getCSSClass CLASSNAME
 *
 * Return the stylesheet class.
 *
 * Only returns the first found class, TODO merge them all?
 *----------------------------------------------------------------------------*/

function getCSSClass( target ) {

    target = "." + target;

    var stylesheets = document.styleSheets;
    var rulekey     = "cssRules";
    var klass       = false;

    if( ieCompatable() ) rulekey = "rules";

    if( stylesheets ) {

        for( var i = 0; !klass && (i < stylesheets.length); i++ ) {

            for( var j = 0; !klass && (j < stylesheets[i][rulekey].length); j++ ) {

                var cssclass    = stylesheets[i][rulekey][j];
                var classname   = cssclass.selectorText;

                if( classname == target ) {

                    klass = cssclass;
                
                }

            }

        }

    }

    return klass;

}//getCSSClass

