﻿/***************************************************************************************
$Workfile: ItemPopu.js $
$Author: Muhammad Noor Hassan $
$Revision: 1 $
$Created Date: 07/28/09 $
$Description: Utility function to show the item pop-up div on roll-over
***************************************************************************************/

// Get Position of the given object
function getPos(srcObject) {
    var x=0, y=0;
	while (srcObject && srcObject.style.position != 'absolute') 
	{
	    if (srcObject && (srcObject.offsetLeft || srcObject.offsetTop))
	    {		
	        x += srcObject.offsetLeft;		
	        y += srcObject.offsetTop;			
	    }
	    srcObject= srcObject.offsetParent;
	}    
    return (new Array(x, y));
}

// Set the position of the given object
function setPos(TargetObject, x, y) {
    TargetObject.style.left = x + "px"; 
    TargetObject.style.top = y + "px";
}

// Show the content divs on Item view page for sellit, contact owner & but it now options
function ShowContentDiv(srcControl, contentDivId)
{
    if(contentDivId == 'divContactOwner')
        contectDiv = document.getElementById('divContactOwner');
    else if(contentDivId == 'divButItNow')
        contectDiv = document.getElementById('divButItNow');
    else if(contentDivId == 'divSellIt')
        contectDiv = document.getElementById('divSellIt');

    // If already visible, ignore the refresh
    if (contectDiv.style.display != "none")
        return;
    
    // Get the Offset for display the hover item detail window
    srcPosition = getPos(srcControl)

    // Now show the preview pane
    contectDiv.style.display = "";
    // We are subtracting the 245 from width, becuase the main content DIV is absolute and is at 
    // a differenc3e of 245 from the top. so any child absolute windows should be relative to that.
    setPos(contectDiv, srcPosition[0]-350-(contectDiv.clientWidth/2), srcPosition[1] + 60);
}

// This function hides the given Popup window after a given delay
function HidePreviewPopupWithDelay(evt, divPopupId, timeOutSec)
{
    evt = evt || window.event;
    targetElement = evt.relatedTarget || evt.toElement;
    
    // Hide the window after given interval
    window.setTimeout(function() { HideContentDiv(evt, document.getElementById(divPopupId), targetElement)}, timeOutSec);
}

function HideContentDiv(evt, contentDiv, opt_targetElement)
{   
    // Handle for the nested DIV mouse out i.e. don't hide when mouse is still over the nested DIV
    if (opt_targetElement)
        targetElement = opt_targetElement
    else
    {
        evt = evt || window.event;    
        targetElement = evt.relatedTarget || evt.toElement;
    }
    
    while (targetElement && targetElement.nodeName !== 'HTML') 
    {                        
        targetElement = targetElement.parentNode;    
        
        // Hide if it's a child window of our main container window
        if (targetElement.id == contentDiv.id)
            return;
    }
    
    contentDiv.style.display = "none";
}
