//
// Scripts for various ajax popup controls.
//

// Global variable for details popup timeout instance.
var popupDetailsTimeoutID = 0;

function ShowPopupDetails(pce)
{
    if (pce != null)
    {
        pce.showPopup();
    }
}
  
// Check the popup details client cursor position and render up/downward popup style
function CheckPopupDetails(e, popupID, behaviorID)
{
    var windowY = f_clientHeight();    // windows y size
    var availSpace = 230;     // available bottom gap
    //var yScroll = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;

    if (e != null)
    {
        var popupObj = $get(popupID);
        var pce = $find(behaviorID);
        if ((popupObj == null) || (pce == null)) return;
        
        if (windowY - e.clientY > availSpace)
        {
            popupObj.className = 'ProdMiniPopupOuterLineDown';            // set the downward styles
            pce._offsetY = 0;     
        }
        else
        {
            popupObj.className = 'ProdMiniPopupOuterLineUp';
            pce._offsetY = -290;     
        }
        $find(behaviorID + 'PopupBehavior').set_positioningMode(0);   // aboslute positioning
    }
}

// Set the popup position for product grid page.
//  e             - firing event.
//  sourceID      - Source element's client id (thumbnail) (handy to explicitly pass it; same as pce's TargetControlID).
//  behaviorID    - BehaviorID property set on the PopupControlExtender.
//
// Set x the same for all, so popup is always in the center content area.
// Set y above or below the source, so popup is on the screen.
//   Avoid setting it over the source element, else mouseover/mouseout happens immediately.
// Omit x, y, position declarations, to handle them here.
// The pce only has offset properties referenced from the source element (thumbnail; its target).
//   It has no absolute position properties, so position has to be calc'ed as an offset.
// Use source element (thumbnail) as a stable reference, since that's the pce's reference;
//  instead of the mouse position (original code).
//
// TODO: Later versions of the toolkit handle screen positioning.
//
function CheckPopupGrid(e, sourceID, behaviorID)
{
    var popupLeft = 175;        // Desired left edge position.
    var popupHeight = 340;      // height = css height + padding + borders.
    var offsetBelow = 70;       // Position below source. Space a little away from thumbnail, else it looks odd.
    var offsetAbove = -10;      // Position above source (negative is upward).

    if (e != null)
    {
        var pce = $find(behaviorID);
        if (pce == null) return;

        var sourceElement = document.getElementById(sourceID);
        if (sourceElement == null) return;
        
        // Position on page, independent of scrolling.
        var srcPos = findPos(sourceElement);
        var srcX = srcPos[0];
        var srcY = srcPos[1];

        // Set x so popup is always in the center content area.
        pce._offsetX = popupLeft - srcX;
        
        // If room below, display below, else display above.
        var popupBottom = srcY - f_scrollTop() + offsetBelow + popupHeight;
        if (popupBottom < f_clientHeight())
        {
            pce._offsetY = offsetBelow;
        }
        else
        {
            pce._offsetY = offsetAbove - popupHeight;
        }
        
        // Set absolute position; seems to reference from top left.
        $find(behaviorID + 'PopupBehavior').set_positioningMode(0);
    }
}

// Check popup screen position for oem machine fitment.
function CheckPopupOemFit(e, behaviorID, popupHeight)
{
    if (e != null) 
    {
        var pce = $find(behaviorID);  
        if (pce == null)
        {
            return;
        }

        // windowY is the window client-area height.
        // e.clientY is the position of the event from the top of windowY.
        var windowY = f_clientHeight();
        if (windowY - e.clientY > popupHeight)    
        {
            pce._offsetY = 0;     
        } 
        else
        {
            // This pops it upward from the click point, even if there's some room below, just to keep it simple.
            pce._offsetY = -popupHeight;  
        }

        $find(behaviorID + 'PopupBehavior').set_positioningMode(0);   // absolute positioning
    }
}

