/* *******************************************************************
incPositioningAPI.js
General Element Positioning Functions
*** REQUIRES: incCommonAPI.js ***
-scott 5/12/06

Adapted From:
    Example 4-3 (DHTMLapi.js)
    "Dynamic HTML:The Definitive Reference"
    2nd Edition
    by Danny Goodman
    Published by O'Reilly & Associates  ISBN 1-56592-494-0
    http://www.oreilly.com
    Copyright 2002 Danny Goodman.  All Rights Reserved.
    // DHTMLapi.js custom API for cross-platform
    // object positioning by Danny Goodman (http://www.dannyg.com).
    // Release 2.0. Supports NN4, IE, and W3C DOMs.
******************************************************************** */

// -------------------------------------------------------------------
// gfunShiftTo(obj, x, y)
// Position an object at a specific pixel coordinate
// -------------------------------------------------------------------
function gfunShiftTo(obj, x, y) {    var theObj = g_getObject(obj);    if (theObj) {
        if (gIsCSS) {
            // equalize incorrect numeric value type
            var units = (typeof theObj.left == "string") ? "px" : 0;
            if (x != null) theObj.left = x + units;            if (y != null) theObj.top = y + units;        }        else if (gIsNN4) {			if (x == null) x = gfunGetObjectLeft(obj);			if (y == null) y = gfunGetObjectTop(obj);
            theObj.moveTo(x,y);
        }
    }
}

// -------------------------------------------------------------------
// gfunShiftBy(obj, deltaX, deltaY)
// Move an object by x and/or y pixels
// -------------------------------------------------------------------
function gfunShiftBy(obj, deltaX, deltaY) {
    var theObj = g_getObject(obj);
    if (theObj) {
        if (gIsCSS) {
            // equalize incorrect numeric value type
            var units = (typeof theObj.left == "string") ? "px" : 0 
            theObj.left = gfunGetObjectLeft(obj) + deltaX + units;
            theObj.top = gfunGetObjectTop(obj) + deltaY + units;
        }        else if (gIsNN4) {
            theObj.moveBy(deltaX, deltaY);
        }
    }
}

// -------------------------------------------------------------------
// gfunSetZIndex(obj, zOrder)
// Set the z-order of an object
// -------------------------------------------------------------------
function gfunSetZIndex(obj, zOrder) {
    var theObj = g_getObject(obj);
    if (theObj) {
        theObj.zIndex = zOrder;
    }
}

// -------------------------------------------------------------------
// gfunSetBGColor(obj, color)
// Set the background color of an object
// -------------------------------------------------------------------
function gfunSetBGColor(obj, color) {
    var theObj = g_getObject(obj);
    if (theObj) {
        if (gIsNN4) {
            theObj.bgColor = color;
        } else if (gIsCSS) {
            theObj.backgroundColor = color;
        }
    }
}

// -------------------------------------------------------------------
// gfunIsVisible(obj)
// Returns a boolean if the item is visible or not
// -------------------------------------------------------------------
function gfunIsVisible(obj) {
    var theObj = g_getObject(obj);
    if (theObj) {
		if (theObj.visibility != "hidden" && theObj.display != "none") {			return true;		}		else {			return false;		}
    }
}


// -------------------------------------------------------------------
// gfunShow(obj)
// Set the visibility of an object to visible
// -------------------------------------------------------------------
function gfunShow(obj) {
    var theObj = g_getObject(obj);
    if (theObj) {
        theObj.visibility = "visible";
    }
}

// -------------------------------------------------------------------
// gfunHide(obj)
// Set the visibility of an object to hidden
// -------------------------------------------------------------------
function gfunHide(obj) {
    var theObj = g_getObject(obj);
    if (theObj) {
        theObj.visibility = "hidden";
    }
}

// -------------------------------------------------------------------
// gfunDisplay(obj)
// Set the display of an object to inherit
// -------------------------------------------------------------------
function gfunDisplay(obj) {
    var theObj = g_getObject(obj);
    if (theObj) {
        theObj.display = "block";
    }
}

// -------------------------------------------------------------------
// gfunCollapse(obj)
// Set the display of an object to none
// -------------------------------------------------------------------
function gfunCollapse(obj) {
    var theObj = g_getObject(obj);
    if (theObj) {
        theObj.display = "none";
    }
}


// -------------------------------------------------------------------
// gfunToggleDisplay(obj)
// Toggles the display of an object from none to block or vice-versa// -------------------------------------------------------------------
function gfunToggleDisplay(obj) {
    var theObj = g_getObject(obj);
    if (theObj) {
        if (theObj.display == "none") theObj.display = "block";
        else theObj.display = "none";
    }
}

// -------------------------------------------------------------------
// gfunGetScrollValueY() [and gfunGetScrollValue() <- OLD]
// Returns the height in pixels that a page is scrolled down
// -------------------------------------------------------------------
function gfunGetScrollValueY() {
	if (document.documentElement) if (document.documentElement.scrollTop) return document.documentElement.scrollTop;
	if (document.body) return document.body.scrollTop;
	else return null;
}
function gfunGetScrollValue() {
	return gfunGetScrollValueY();
}

// -------------------------------------------------------------------
// gfunGetScrollValueX()
// Returns the height in pixels that a page is scrolled to the right
// -------------------------------------------------------------------
function gfunGetScrollValueX() {
	if (document.documentElement) if (document.documentElement.scrollLeft) return document.documentElement.scrollLeft;
	if (document.body) return document.body.scrollLeft;
	else return null;
}
// -------------------------------------------------------------------
// gfunGetObjectLeft(obj)
// Retrieve the x coordinate of a positionable object
// -------------------------------------------------------------------
function gfunGetObjectLeft(obj)  {
    var elem = g_getRawObject(obj);    var result = 0;
    if (document.defaultView) {
        var style = document.defaultView;
        var cssDecl = style.getComputedStyle(elem, "");
        result = cssDecl.getPropertyValue("left");
    }    else if (elem.currentStyle) {
        result = elem.currentStyle.left;
    }    else if (elem.style) {
        result = elem.style.left;
    }    else if (gIsNN4) {
        result = elem.left;
    }
    return parseInt(result);
}

// -------------------------------------------------------------------
// gfunGetObjectTop(obj)
// Retrieve the y coordinate of a positionable object
// -------------------------------------------------------------------
function gfunGetObjectTop(obj)  {
    var elem = g_getRawObject(obj);
    var result = 0;
    if (document.defaultView) {
        var style = document.defaultView;
        var cssDecl = style.getComputedStyle(elem, "");
        result = cssDecl.getPropertyValue("top");
    }    else if (elem.currentStyle) {
        result = elem.currentStyle.top;
    }    else if (elem.style) {
        result = elem.style.top;
    }    else if (gIsNN4) {
        result = elem.top;
    }
    return parseInt(result);
}

// -------------------------------------------------------------------
// gfunGetRealLeft(obj)
// Gets the number of pixels an object is from the left of the window
// -------------------------------------------------------------------
function gfunGetRealLeft(obj) {
    var elem = g_getRawObject(obj);
	var xPos = elem.offsetLeft;
	var tempEl = elem.offsetParent;
  	while (tempEl != null) {
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
	return xPos;
}

// -------------------------------------------------------------------
// gfunGetRealTop(obj)
// Gets the number of pixels an object is from the top of the window
// -------------------------------------------------------------------
function gfunGetRealTop(obj) {

    var elem = g_getRawObject(obj);
	var yPos = elem.offsetTop;
	var tempEl = elem.offsetParent;
  	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
  	
	return yPos;
}

// -------------------------------------------------------------------
// gfunGetObjectWidth(obj)
// Retrieve the rendered width of an element
// -------------------------------------------------------------------
function gfunGetObjectWidth(obj)  {
    var elem = g_getRawObject(obj);
    var result = 0;
    
    if (elem != null) {
        if (elem.offsetWidth) {
            result = elem.offsetWidth;
        }        else if (elem.clip && elem.clip.width) {
            result = elem.clip.width;
        }        else if (elem.style && elem.style.pixelWidth) {
            result = elem.style.pixelWidth;
        }
    }
    return parseInt(result);
}


// -------------------------------------------------------------------
// gfunGetObjectParentContainerWidth(obj)
// Retrieve the inside width of an element's Parent Object
// -------------------------------------------------------------------
function gfunGetObjectParentContainerWidth(obj)  {
    var elem = g_getRawObject(obj);
    if (elem.parentNode) elem = elem.parentNode;
    else if (elem.parentElement) elem = elem.parentElement;
    var nWidth = gfunGetObjectWidth(elem);
    var result = nWidth;
    
    // Subtract Horizontal Padding from the Width
    if (result > 0 && elem.style && elem.style.paddingLeft != null) {
        // Factor Left Padding
        var sTempPad = elem.style.paddingLeft;
        var nTempSize = parseInt(sTempPad);
        if (!isNaN(nTempSize)) {
            if (sTempPad.indexOf("px") > 0) {
                result -= parseInt(sTempPad);
            }
            else if (sTempPad.indexOf("%") > 0) {
                result -= nWidth * (parseInt(sTempPad) / 100);
            }
        }
        
        // Factor Right Padding
        sTempPad = elem.style.paddingRight;
        nTempSize = parseInt(sTempPad);
        if (!isNaN(nTempSize)) {
            if (sTempPad.indexOf("px") > 0) {
                result -= parseInt(sTempPad);
            }
            else if (sTempPad.indexOf("%") > 0) {
                result -= nWidth * (parseInt(sTempPad) / 100);
            }
        }
    }

    return parseInt(result);
}

// -------------------------------------------------------------------
// gfunGetObjectHeight(obj)
// Retrieve the rendered height of an element
// -------------------------------------------------------------------
function gfunGetObjectHeight(obj)  {
    var elem = g_getRawObject(obj);
    var result = 0;
    
    if (elem != null) {
        if (elem.offsetHeight) {
            result = elem.offsetHeight;
        }        else if (elem.clip && elem.clip.height) {
            result = elem.clip.height;
        }        else if (elem.style && elem.style.pixelHeight) {
            result = elem.style.pixelHeight;
        }
    }
    return parseInt(result);
}


// -------------------------------------------------------------------
// gfunGetObjectParentContainerHeight(obj)
// Retrieve the inside height of an element's Parent Object
// -------------------------------------------------------------------
function gfunGetObjectParentContainerHeight(obj)  {
    var elem = g_getRawObject(obj);
    if (elem.parentNode) elem = elem.parentNode;
    else if (elem.parentElement) elem = elem.parentElement;
    var nHeight = gfunGetObjectHeight(elem);
    var result = nHeight;
    
    // Subtract Horizontal Padding from the Height
    if (result > 0 && elem.style && elem.style.paddingLeft != null) {
        // Factor Top Padding
        var sTempPad = elem.style.paddingTop;
        var nTempSize = parseInt(sTempPad);
        if (!isNaN(nTempSize)) {
            if (sTempPad.indexOf("px") > 0) {
                result -= parseInt(sTempPad);
            }
            else if (sTempPad.indexOf("%") > 0) {
                result -= nHeight * (parseInt(sTempPad) / 100);
            }
        }
        
        // Factor Bottom Padding
        sTempPad = elem.style.paddingBottom;
        nTempSize = parseInt(sTempPad);
        if (!isNaN(nTempSize)) {
            if (sTempPad.indexOf("px") > 0) {
                result -= parseInt(sTempPad);
            }
            else if (sTempPad.indexOf("%") > 0) {
                result -= nHeight * (parseInt(sTempPad) / 100);
            }
        }
    }

    return parseInt(result);
}

// -------------------------------------------------------------------
// gfunGetInsideWindowWidth()
// Return the available content width space in browser window
// -------------------------------------------------------------------
function gfunGetInsideWindowWidth() {
    if (window.innerWidth) {
        // Factor in Scrollbars?
        var nScollbars = 0;
        if (gfunGetInsideWindowHeight() < gfunGetObjectHeight(window.document)) nScollbars = 32;
        return window.innerWidth - nScollbars;
    }    else if (isIE6CSS) {
        // measure the html element's clientWidth
        return document.body.parentElement.clientWidth
    }    else if (document.body && document.body.clientWidth) {
        return document.body.clientWidth;
    }
    return 0;
}

// -------------------------------------------------------------------
// gfunGetInsideWindowHeight()
// Return the available content height space in browser window
// -------------------------------------------------------------------
function gfunGetInsideWindowHeight() {
    if (window.innerHeight) {
        return window.innerHeight;
    }    else if (isIE6CSS) {
        // measure the html element's clientHeight
        return document.body.parentElement.clientHeight
    }    else if (document.body && document.body.clientHeight) {
        return document.body.clientHeight;
    }
    return 0;
}

// -------------------------------------------------------------------
// gfunGetCellWidth(objTable, objCell)// Function to get the Cell Width of a Table
// -------------------------------------------------------------------
function gfunGetCellWidth(objTable, objCell) {
	objTable = g_getRawObject(objTable);
	objCell = g_getRawObject(objCell);

	if (objTable && objCell) {
		var objRow = (objCell.parentNode) ? objCell.parentNode : objCell.parentElement;
		nCellIndex = objCell.cellIndex;

		// Safari Bug Fix to get CellIndex
		var sAgent = navigator.userAgent;
		if (sAgent.indexOf("Safari") > 1) { //Get the Cell Index another way
			for (var i=0; i<objRow.cells.length; i++) {
				if (objRow.cells[i] == objCell) {
					nCellIndex = i;
					break;
				}
			}
		}

		var nCellWidth, nTableLeft, nTableWidth, nCellLeft, nCellIndex, objNextCell;
		nCellLeft = parseInt(objCell);
		if (nCellIndex < objRow.cells.length-1) { // Not the Last Cell
			objNextCell = objRow.cells[nCellIndex+1];
			nCellWidth = parseInt(objNextCell) - nCellLeft;
		}
		else { //Last Cell so use the table width
			nTableLeft = parseInt(objTable);
			nCellWidth = nTableLeft + parseInt(objTable.width) - nCellLeft;
		}
		return nCellWidth;
	}
	return 0;
}
