﻿//******************************************
// Requires the following JavaScript files:
//  - wz_tooltips.js
//  - Common.js
//******************************************

// <summary>
//  Function used to associate control events to custom functions within the page.  It is encapsulated in order to allow attaching events in all browsers
// </summary>
// <param name="ctrlTargetToAttachTo">Reference to the control to attach an event to</param>
// <param name="strEventName">Name of the event on control 'ctrlTargetToAttachTo' to associate with custom method fnHandlerName</param>
// <param name="fnHandlerName">Method to execute when event 'strEventName' fires on control 'ctrlTargetToAttachTo'</param>
// <param name="boolUseCapture">Whether or not to use capture - Events which are bubbling upward through the tree will not trigger a listener designated to use capture</param>
// <example>
//   addEvent(document.getElementByID('idname'), 'click', idControlClicked, false);
// </example>
function addEvent(ctrlTargetToAttachTo, strEventName, fnHandlerName, boolUseCapture) {
    if (ctrlTargetToAttachTo.addEventListener) {   //Attach an event to a Mozilla based browser
        ctrlTargetToAttachTo.addEventListener(strEventName, fnHandlerName, boolUseCapture);
        return true;
    }
    else if (ctrlTargetToAttachTo.attachEvent)
    //Attach an event in newer Internet Explorer Browsers
        return ctrlTargetToAttachTo.attachEvent('on' + strEventName, fnHandlerName);
    else
    //Attach an event to an older style browser that does not understand the previou options
        ctrlTargetToAttachTo['on' + strEventName] = fnHandlerName;
}

// <summary>
// Retrieve a reference to the element responsible for the event raised
// </summary>
// <param name="e">Argument passed to an event handler</param>
function getEventRaiserElement(e) {
    var e = e ? e : window.event;
    var element = e.target ? e.target : e.srcElement;

    return element;
}


// <summary>
// Format the Tooltip depending on whether or not there is a title associated with the tooltip
// </summary>
function formatTipText(title, body) 
{
    var tooltip = body;
    if (title != null && title.length > 0)
        tooltip = "<b>" + title + "</b><br />" + body;

    return tooltip;
}

var g_tiptext;
// <summary>
// Display the tooltip dynamically attached to element items in a page
// </summary>
// <param name="e">Argument passed to an event handler</param>
function showDynamicTip(e) {
    // Retrieve a reference to the element that raised this event
    var element = getEventRaiserElement(e);

    // Retrieve the value of the tooltip from this element
    g_tiptext = element.title;

    // Remove the title attribute value so that the built in 'alt text' tooltip will not be displayed by the browser
    element.title = "";

    //Display the properly formatted tooltip to the user based on the title text of element.
    Tip(formatTipText(getInnerText(element), g_tiptext), WIDTH, -250, SHADOW, true);
}

// <summary>
// Hide the tooltip dynamically attached to element items in a page
// </summary>
// <param name="e">Argument passed to an event handler</param>
function hideDynamicTip(e) {
    // Retrieve a reference to the element that raised this event
    var element = getEventRaiserElement(e);

    //Hide currently visible Tooltip
    UnTip();

    //Reset the value of the title on this element
    element.title = g_tiptext;
    g_tiptext = "";
}
