/*
 * DisplayUitls.js
 *
 * Utility functions for altering the display of HTML componenets from javascript
 *
 * Company: Hyfinity Ltd
 * Copyright (c) 2003
 */


/**
 * Generic function for toggling the display of a component.
 * @param componentID the ID of the HTML component to show/hide
 * @param method An optional string specifying the required visibility of the
 *               component ('show' or 'hide')
 */
function toggleComponent(componentID, method)
{
    var component = document.getElementById(componentID);
    if ((component != null) && (typeof(component) != 'undefined'))
    {
        if (typeof(method) == 'undefined')
        {
            if (component.style.display == 'none')
            {
                showComponent(component);
            }
            else
            {
                hideComponent(component);
            }
        }
        else
        {
            if (method == 'hide')
            {
                hideComponent(component);
            }
            else
            {
                showComponent(component);
            }
        }
    }
}

/*generic utility function for hiding a specific component*/
function hideComponent(component)
{
    if (component == null)
        return;

    if (component._oldCSSDisplay == undefined)
        component._oldCSSDisplay = component.style.display;

    component.style.visibility = 'hidden';
    component.style.display = 'none';
}
/*generic utility function for showing a particular component*/
function showComponent(component)
{
    if (component == null)
        return;

    component.style.visibility = 'visible';
    if (component._oldCSSDisplay != undefined)
        component.style.display= component._oldCSSDisplay;
    else
        component.style.display = 'block';
}

/* utility function for disabling a component
 * may want to extend this so children components are disabled as well, ie if a div component is passed in*/
function disableComponent(component)
{
    if (component == null)
        return;

    component.disabled=true;
}

/* utility function for enabling a component*/
function enableComponent(component)
{
    if (component == null)
        return;

    component.disabled=false;
}

/* Functions for adjustable dividers. */
var dividerMode = 'stop'

var widthAdjustTarget;
var fixedPoint;
var directionMode;

function dividerPressed(target, mode)
{
    if (typeof(mode) == 'undefined')
    {
        mode = 'left';
    }

    dividerMode = 'move';
    widthAdjustTarget = target;
    directionMode = mode;
    if (directionMode == 'left')
    {
        fixedPoint = getLeftPosition(widthAdjustTarget);
    }
    else
    {
        fixedPoint = getRightPosition(widthAdjustTarget);
    }

    //alert(windowWidth);
}

function dividerMove(e)
{
    //alert(dividerMode);
    if (dividerMode == 'move')
    {
        var posx = 0;
        var posy = 0;
        var evt;
        var target;
        if (!e)
        {
            evt = window.event;
            target = evt.srcElement;
        }
        else //netscape
        {
            evt = e;
            target = evt.currentTarget;
        }

        //get the mouse coords
        if (evt.pageX || evt.pageY)
        {
            posx = evt.pageX;
            posy = evt.pageY;
        }
        else if (evt.clientX || evt.clientY)
        {
            posx = evt.clientX + document.body.scrollLeft;
            posy = evt.clientY + document.body.scrollTop;
        }

        var newWidth

        if (directionMode == 'left')
        {
            newWidth = posx - fixedPoint;
        }
        else
        {
            newWidth = fixedPoint - posx;
        }
        if (newWidth > 0)
        {
            widthAdjustTarget.style.width = newWidth;
        }
    }

}

function dividerReleased()
{
    dividerMode = 'stop';
}

function getLeftPosition(obj)
{
    var ol=obj.offsetLeft;
    while ((obj=obj.offsetParent) != null)
    {
        ol += obj.offsetLeft;
    }
    return ol;
}

function getRightPosition(obj)
{
    return getLeftPosition(obj) + obj.offsetWidth;
}
