//
//  general.js
//  Written by Stefan Habel
//  Version 1.1
//  Last modified: 04.04.2009
//


//
// Returns whether the given variable points to an array.
//
// \param array   the variable to check
//
window.IsArray = function ( array )
{
    return typeof array == "object" && array.length != undefined;
}


//
// Returns the number of elements in the given array.
//
// \param array   the array to count elements for
//
window.Count = function ( array )
{
    if (!IsArray(array))
        return 0;

    var n = 0;
    for (var object in array)
        ++n;
    return n;
}


//
// Returns the index of the given element in the given array, or -1 if the
// element is not contained in the array.
//
// \param element   the element to search in the array
// \param array     the array to search the element in
//
window.IndexOf = function ( element, array )
{
    if (!IsArray(array))
        return -1;

    for (var i = 0; i < array.length; ++i)
        if (array[i] == element)
            return i;
    return -1;
}


//
// Convenience function for checking whether the given element is contained in
// the given array.
//
// \param element   the element to search in the array
// \param array     the array to search the element in
//
window.IsIn = function ( element, array )
{
    return IndexOf(element, array) > -1;
}


//
// Returns the inner width of the browser window.
//
// \return The browser window's inner width.
//
window.getInnerWidth = function ()
{
    if (self.innerWidth)
        return self.innerWidth;
    else if (document.documentElement && document.documentElement.clientWidth)
        return document.documentElement.clientWidth;
    else if (document.body)
        return document.body.clientWidth;
    return 0;
}


//
// Returns the inner height of the browser window.
//
// \return The browser window's inner height.
//
window.getInnerHeight = function ()
{
    if (self.innerHeight)
        return self.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight)
        return document.documentElement.clientHeight;
    else if (document.body)
        return document.body.clientHeight;
    return 0;
}


//
// Returns the offset by which the page has been horizontally scrolled.
//
// \return The offset by which the page has been horizontally scrolled.
//
window.getScrollXOffset = function ()
{
    if (window.pageXOffset)
        return window.pageXOffset;
    else if (document.documentElement && document.documentElement.scrollLeft)
        return document.documentElement.scrollLeft;
    else if (document.body && document.body.scrollLeft)
        return document.body.scrollLeft;
    return 0;
}


//
// Returns the offset by which the page has been vertically scrolled.
//
// \return The offset by which the page has been vertically scrolled.
//
window.getScrollYOffset = function ()
{
    if (window.pageYOffset)
        return window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
    else if (document.body && document.body.scrollTop)
        return document.body.scrollTop;
    return 0;
}


//
// Returns the absolute coordinate of the left edge of the given element.
//
// \param element The element for which to calculate the absolute left position.
// \return The absolute X coordinate of the given element.
//
window.getAbsoluteOffsetLeft = function ( element )
{
    var result = 0;
    if (element.offsetParent)
        do {
            result += element.offsetLeft;
        } while (element = element.offsetParent);
    return result;
}


//
// Returns the absolute coordinate of the top edge of the given element.
//
// \param element The element for which to calculate the absolute top position.
// \return The absolute Y coordinate of the given element.
//
window.getAbsoluteOffsetTop = function ( element )
{
    var result = 0;
    if (element.offsetParent)
        do {
            result += element.offsetTop;
        } while (element = element.offsetParent);
    return result;
}

