var recursionlimit = 20;

// Global instances
var thomsonlogocoordinates;
var mousecoordinates;

function createcoordinateinstances( element )
{
    thomsonlogocoordinates = new elementcoordinatesobject( element );
    mousecoordinates       = new mousecoordinatesobject();
}

// Record an element's X and Y coordinates for the full page and for the viewport
function elementcoordinatesobject( element )
{
    this.actualleftx     = 0;
    this.actualrightx    = 0;
    this.actualtopy      = 0;
    this.actualbottomy   = 0;
    this.viewportleftx   = 0;
    this.viewportrightx  = 0;
    this.viewporttopy    = 0;
    this.viewportbottomy = 0;

    this.element = element;

    this.setelementcoordinates = function( element, level )
    {
        // Initialize
        if ( level == 0 )
        {
            this.actualleftx     = 0;
            this.actualrightx    = 0;
            this.actualtopy      = 0;
            this.actualbottomy   = 0;
            this.viewportleftx   = 0;
            this.viewportrightx  = 0;
            this.viewporttopy    = 0;
            this.viewportbottomy = 0;
        }

        // Limit recursion depth
        if ( level > recursionlimit ) { alert( 'Recursion level reached the limit of ' + recursionlimit ); return }

        // Conditionally recurse
        if ( element.offsetParent ) { this.setelementcoordinates( element.offsetParent, level + 1 ) }

        if ( level == 0 ) // This code is run after any recursion has finished
        {
            this.actualleftx  += element.offsetLeft;
            this.actualrightx  = this.actualleftx + element.offsetWidth;
            this.actualtopy   += element.offsetTop;
            this.actualbottomy = this.actualtopy + element.offsetHeight;

            // What are the viewport coordinates
            var doc_element = document.body;

            if ( window.navigator.userAgent.match( /firefox/i ) )
            {
                // Note: document.doctype.publicId accesses the docType declaration text
                // Note: document.doctype.systemId accesses the docType declaration url
                if  (
                        document.doctype
                        &&
                        document.doctype.systemId
                        &&
                        document.doctype.systemId.match( /(strict|loose)\.dtd/i )
                    )
                {
                    doc_element = document.documentElement;
                }
            }
            else if ( window.navigator.userAgent.match( /msie +(6|7)/i ) )
            {
                // document.childNodes[0] is the docType declaration text
                // document.childNodes[1] is the document.documentElement ( heretofore known as document.body )
                if  (
                        document.childNodes[0].nodeValue
                        &&
                        document.childNodes[0].nodeValue.match( /(strict|loose)\.dt/i )
                    )
                {
                    doc_element = document.documentElement;
                }
            }
            else if ( window.navigator.userAgent.match( /msie +5/i ) )
            {
            }

            // If the logo is position: fixed, then don't adjust for scrolling
            if  ( element.style.position.match( /absolute/i ) )
            {
                this.viewportleftx = this.actualleftx - doc_element.scrollLeft;
                this.viewportrightx = this.actualrightx - doc_element.scrollLeft;
                this.viewporttopy = this.actualtopy - doc_element.scrollTop;
                this.viewportbottomy = this.actualbottomy - doc_element.scrollTop;
            }
            else if ( element.style.position.match( /fixed/i ) )
            {
                this.viewportleftx = this.actualleftx;
                this.viewportrightx = this.actualrightx;
                this.viewporttopy = this.actualtopy;
                this.viewportbottomy = this.actualbottomy;
            }
        }
        else // This code is run when the function has been called recursively
        {
            this.actualleftx += element.offsetLeft;
            this.actualtopy  += element.offsetTop;
        }
    };
}


var totalmousemoveevents = 0;
var mouseeventgovernor = 10;
if ( window.navigator.userAgent.match( /msie/i ) ) { mouseeventgovernor = 5 }

// Calculate mouse coordinates for the full page and the viewport
function mousecoordinatesobject()
{
    this.actualx = 0;
    this.actualy = 0;
    this.viewportx = 0;
    this.viewporty = 0;

    this.setmousecoordinates = function (e)
    {
        // Note: document.body.scrollLeft doesn't work for certain docType definitions - e.g. HTML 4.01 Transitional

        var doc_element = document.body;

        totalmousemoveevents++;

        // Only respond to the mousemove event part of the time
        if ( !( totalmousemoveevents % mouseeventgovernor ) )
        {
            if ( window.navigator.userAgent.match( /firefox/i ) )
            {
                this.actualx   = e.pageX;
                this.actualy   = e.pageY;

                // Note: document.doctype.publicId accesses the docType declaration text
                // Note: document.doctype.systemId accesses the docType declaration url
                if  (
                        document.doctype
                        &&
                        document.doctype.systemId
                        &&
                        document.doctype.systemId.match( /(strict|loose)\.dtd/i )
                    )
                {
                    doc_element = document.documentElement;
                }

                this.viewportx = e.pageX - doc_element.scrollLeft;
                this.viewporty = e.pageY - doc_element.scrollTop;
            }
            else if ( window.navigator.userAgent.match( /msie +(6|7)/i ) )
            {
                this.viewportx = event.clientX;
                this.viewporty = event.clientY;

                // document.childNodes[0] is the docType declaration text
                // document.childNodes[1] is the document.documentElement ( heretofore known as document.body )
                if  (
                        document.childNodes[0].nodeValue
                        &&
                        document.childNodes[0].nodeValue.match( /(strict|loose)\.dt/i )
                    )
                {
                    doc_element = document.documentElement;
                }

                this.actualx = event.clientX + doc_element.scrollLeft;
                this.actualy = event.clientY + doc_element.scrollTop;
            }
            else if ( window.navigator.userAgent.match( /msie +5/i ) )
            {
                this.viewportx = event.clientX;
                this.viewporty = event.clientY;
                this.actualx = event.clientX + document.body.scrollLeft;
                this.actualy = event.clientY + document.body.scrollTop;
            }
        }
    };
}
