﻿
// This function binds a qtip tooltip function to the mouseover
// and mouseout events for each element belonging to class
// "jargonSmartLink". The mouseover event shows or dynamically
// creates the tooltip, populates it with content from the callback
// page using the termid attribute, and inserts it into the DOM.
// The mouseout function hides the tooltip.
function BindQtips() {
    $(".jargonSmartLink").each(function() {
        var tid = $(this).attr("termid");
        $(this).qtip({
            content: {
                url: '/BooksOfJargonCallback.aspx',
                data: { TermID: tid },
                method: 'get'
            },
            show: 'mouseover',
            hide: 'mouseout',

            position: {
                corner: {
                    target: 'topRight',
                    tooltip: 'bottomLeft'
                }
            },

            style: {
                border: {
                    width: 1,
                    radius: 3,
                    color: '#6699CC'
                },
                width: 300,
                tip: 'bottomLeft'
            }

        });
    });
}


// Executes the binding function on first load
$(document).ready(BindQtips);

// Executes on body onload; asks the pageRequestManager
// to attach BindQtips as an event handler for endRequest
function load() {
    //Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(UnbindQtips);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(BindQtips);
}


// HACK BY JWF
// Explanation: there is a bug in either the qtip library or IE itself.
// When an ajax update occurs, dynamically created qtips are removed
// from the DOM as the new content is inserted. However, events which
// have been bound to the dynamically created tooltips are not correctly
// unbound. When jquery attempts to get an offset on these elements,
// getBoundingClientRect() throws an exception. I have overwritten
// this part of the jquery library to wrap the call in a try/catch block.

// The safe wrapped call
function IESafeGetBoundingClientRect(elem) {
    try {
        return elem.getBoundingClientRect();
    }
    catch (e) {
        var box = {
            top: 0,
            left: 0
        }

        return box;
    }
}

// the jquery function definition that originally used the call. It
// now uses the safe wrapped call. See jquery-1.3.2 line 4176 for original.
if (document.documentElement["getBoundingClientRect"])
    jQuery.fn.offset = function() {
        if (!this[0]) return { top: 0, left: 0 };
        if (this[0] === this[0].ownerDocument.body) return jQuery.offset.bodyOffset(this[0]);
        var box = IESafeGetBoundingClientRect(this[0]), doc = this[0].ownerDocument, body = doc.body, docElem = doc.documentElement,
			clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
			top = box.top + (self.pageYOffset || jQuery.boxModel && docElem.scrollTop || body.scrollTop) - clientTop,
			left = box.left + (self.pageXOffset || jQuery.boxModel && docElem.scrollLeft || body.scrollLeft) - clientLeft;
        return { top: top, left: left };
    };
