﻿//------------------------------------------------------------------------- 
// class to provide information on when user would like help and where      
// they would like it                                                       
// Usage is:                                                                
//      MZ_HelpByHierarchy.init(showInfo);                                  
//                                                                          
//      function showInfo(sNode, sId, sPath,nMouseX,nMouseY)                
//      {                                                                   
//          alert(sNode + ":" + sId + ":" + sPath);                         
//      }                                                                   
//                                                                          
// V. Singleton  ModeZero.net 2009                                          
//-------------------------------------------------------------------------
var MZ_HelpByHierarchy =
{
    //-------------------------------------------------------------------------
    m_nMouseDownTimerId: null,
    m_fnCallbackOnHelp: null,
    m_nHoldForHelpTimeMs: 1500,
    m_sIdOfHelpContainerDiv: null,

    //-------------------------------------------------------------------------
    mouseDown:
    function(mEvent, nOverrideWaitTimeMs, nOffsetX, nOffsetY)
    {
        var srcElement, x, y

        if (!mEvent)
        {
            mEvent = window.event;
            srcElement = mEvent.srcElement;
        }
        else
        {
            srcElement = mEvent.target;
        }

        x = mEvent.clientX + (nOffsetX ? nOffsetX : 0);
        y = mEvent.clientY + (nOffsetY ? nOffsetY : 0);

        var nWaitTimeBeforeDisplay = (nOverrideWaitTimeMs != null) ? nOverrideWaitTimeMs : MZ_HelpByHierarchy.m_nHoldForHelpTimeMs;

        MZ_HelpByHierarchy.m_nMouseDownTimerId = setTimeout(function() { MZ_HelpByHierarchy.helpRequested(x, y, srcElement) }, nWaitTimeBeforeDisplay);
    },

    //-------------------------------------------------------------------------
    mouseUp:
    function(mEvent)
    {
        if (MZ_HelpByHierarchy.m_nMouseDownTimerId != null)
        {
            clearTimeout(MZ_HelpByHierarchy.m_nMouseDownTimerId)
            MZ_HelpByHierarchy.m_nMouseDownTimerId = null;
        }
    },

    //-------------------------------------------------------------------------
    showHelpNow:
    function(mEvent, nOffsetX, nOffsetY)
    {
        MZ_HelpByHierarchy.mouseDown(mEvent, 0, nOffsetX, nOffsetY) // show now, wait = 0
    },

    //-------------------------------------------------------------------------
    helpRequested:
    function(clientX, clientY, myElement)
    {
        MZ_HelpByHierarchy.m_nMouseDownTimerId = null;

        // where clicked    
        var x = clientX;
        var y = clientY;
        x = parseInt(clientX + document.body.scrollLeft);
        y = parseInt(clientY + document.body.scrollTop);

        // call the function that handles the request
        m_fnCallbackOnHelp(myElement.nodeName,
                            myElement.id,
                            MZ_HelpByHierarchy.getElementHierarchy(myElement),
                            x, y);
    },

    //-------------------------------------------------------------------------
    getElementHierarchy:
    function(emtRoot)
    {
        var sRc = "";
        var emtCurrent = emtRoot;
        while (emtCurrent.parentNode)
        {
            sRc += "|" + (emtCurrent.className ? emtCurrent.className : "$");
            emtCurrent = emtCurrent.parentNode
        }

        return (sRc);
    },

    //------------------------------------------------------------------------- 
    showInfo:   // default callback used when request has been made to give help
    function(sNode, sId, sPath, x, y)
    {
        // position popup control (control) at mouse then show it
        var divHelp = $get(m_sIdOfHelpContainerDiv);

        // offset so mouse up is sent to popup window 
        divHelp.style.left = x - 10 + "px";
        divHelp.style.top = y - 10 + "px";
        $find("bidHelpPopup").showPopup();
        $find("bidDynamicHelpText").populate("_page_:" + window.location.pathname + "_element_:" + sNode + "_id_:" + sId + "_hier_:" + sPath);
    },


    //-------------------------------------------------------------------------
    _internal_init:
    function(fnCallbackForHelp, sIdOfHelpConatinerDiv)
    {
        if (fnCallbackForHelp != null)
        {
            m_fnCallbackOnHelp = fnCallbackForHelp;
        }
        else
        {
            m_fnCallbackOnHelp = MZ_HelpByHierarchy.showInfo;
            m_sIdOfHelpContainerDiv = sIdOfHelpConatinerDiv;
        }

        // hook-up our mouse trackers
        if (document.addEventListener)
        {
            document.addEventListener('mousedown', function(evt) { MZ_HelpByHierarchy.mouseDown(evt) }, false);
            document.addEventListener('mouseup', function(evt) { MZ_HelpByHierarchy.mouseUp(evt) }, false);
        }
        else
        {
            document.onmousedown = function() { MZ_HelpByHierarchy.mouseDown() }
            document.onmouseup = function() { MZ_HelpByHierarchy.mouseUp() }
        }
    },

    //-------------------------------------------------------------------------
    init_overrideCallback:
    function(fnCallbackForHelp)
    {
        MZ_HelpByHierarchy._internal_init(fnCallbackForHelp, null);
    },

    //-------------------------------------------------------------------------
    init:
    function(sHelpContainerId)
    {
        MZ_HelpByHierarchy._internal_init(null, sHelpContainerId);
    }



}
//-------------------------------------------------------------------------
