// $Id: cartcrosssell.js,v 1.5 2009/08/20 22:39:40 jchung Exp $

function SelectCartCrossSellLine()
{
    xSellForm = document.cartCrosssellForm;
    
    if (!xSellForm)
        return false;
    
    lineSelect = xSellForm.cartCrossSellLineNumber;
    if (!lineSelect)
        return false;
        
    lineSelectOptionLength = lineSelect.length;
    for (i=0; i<lineSelectOptionLength; i++)
    {
        line = i+1;
        lineDiv = document.getElementById("cartCrossSellLine_" + line);
        if (!lineDiv)
            return false;
            
        if (lineSelect.options[i].selected)
        {
            lineDiv.style.display = "block";
        }
        else
        {
            lineDiv.style.display = "none";
        }
    }
    
    return true;
}

function DisplayElement(overlayElementId, anchorDivElementId, evt, yFudgeFactor)
{
    div = document.getElementById(overlayElementId);
    if (div)
    {
        if (div.innerHTML)
        {
            // Only for IE
        	if (window.attachEvent && navigator.appVersion.substr(22,3)!="5.0") {
        		var ieMat=document.createElement('iframe');
        		if(document.location.protocol == "https:")
        			ieMat.src="//0";
        		else if(window.opera != "undefined")
        			ieMat.src="";
        		else
        			ieMat.src="javascript:false";
        		ieMat.scrolling="no";
        		ieMat.frameBorder="0";
        		ieMat.style.position = "absolute";
        		ieMat.id = "iframeoverlay";
        		
        		// Making the overlay smaller than the div's width and height because for some reason it will
        		// peek out from behind the overlay without these adjustments.
        		ieMat.style.width=(div.offsetWidth-2)+"px";
        		if (div.offsetHeight)
            		ieMat.style.height=(div.offsetHeight-2)+"px";
                else
                    ieMat.style.height=0+"px";
    
        		ieMat.style.zIndex="-1";

                // Put IFRAME in the same div as the overlay. This way the IFRAME will move with overlay
        		div.insertBefore(ieMat, div.childNodes[0]);
                div.style.zIndex="100";
            }
        }

        MoveElement(overlayElementId, anchorDivElementId, evt, yFudgeFactor);
        
        if (div.innerHTML)
            div.style.visibility = 'visible';
    }
}

function HideElement(elementId)
{
    div = document.getElementById(elementId);
    if (div)
    {
        div.style.visibility = 'hidden';
        // Move the overlay WAY off the page
        div.style.top = '-1000px';
        div.style.left = '-1000px';

        if (window.attachEvent && navigator.appVersion.substr(22,3)!="5.0") {
            if (div.childNodes.length)
            {
                for (i=0; i<div.childNodes.length; i++)
                {
                    if (div.childNodes[i].id == "iframeoverlay")
                    {
                        div.removeChild(div.childNodes[i]);
                    }
                }
            }
        }
    }
}

function MoveElement(overlayElementId, anchorDivElementId, evt, yFudgeFactor)
{
    // The overlay's position is relative to the sidebar   
    overlaydiv = document.getElementById(overlayElementId);
    
    if (overlaydiv)
    {
    
        // The mouse coordinates are relative to the window
        mouseX=evt.pageX?evt.pageX:evt.clientX;
        mouseY=evt.pageY?evt.pageY : (evt.clientY + document.body.scrollTop + document.documentElement.scrollTop);

        if (anchorDivElementId)
        {
            anchorDiv = document.getElementById(anchorDivElementId);
            leftBoundary = anchorDiv.offsetParent.offsetLeft;    // left boundary of the right side bar.
            rightBoundary = leftBoundary + anchorDiv.offsetParent.clientWidth;   // right boundary of the right side bar

            totalOffsetLeft = TotalOffsetLeft(anchorDiv);    // The left offset of the div that was rolled moused over
        }
        else
        {
            leftBoundary = 0;
            rightBoundary = document.body.clientWidth;
            totalOffsetLeft = 0;
        }        

        overlayLeftEdge = mouseX - (overlaydiv.clientWidth/2);  // The overlay's left edge
        overlayRightEdge = overlayLeftEdge + overlaydiv.clientWidth;    // The overlay's right edge

        
        if (leftBoundary < overlayLeftEdge && overlayRightEdge < rightBoundary) // within side bar's boundaries
        {
            // Transform the mouse's x position to be relative to the sidebar's left offset.
            overlaydiv.style.left = (mouseX - totalOffsetLeft - (overlaydiv.clientWidth/2)) + "px";
        }
        else if (leftBoundary > overlayLeftEdge)    // the left edge of the overlay is past the sidebar's left boundary
        {
            // Set the overlay's left position to be 1px to the right of the sidebar's left boundary
            overlaydiv.style.left = 1 + "px";   
        }
        else if (rightBoundary < overlayRightEdge)  // the right edge is past the sidebar's right boundary
        {
            // Set the overlay's left position so that the right side of the overlay is 1px from the right boundary
            overlaydiv.style.left = (anchorDiv.offsetParent.clientWidth - overlaydiv.clientWidth - 2) + "px"; 
        }
        
        
        // Set the top position so that the bottom of the overlay is where the top of the anchorDiv is.
        // Need to add in position of sidebar relative to top of the page
        overlaydiv.style.top= (mouseY - (overlaydiv.clientHeight + yFudgeFactor)) + "px";                
    }
}

function TotalOffsetLeft(el, skipElementId)
{
    var o;
    var totalOffsetLeft;
    if (el.id == skipElementId)
        o = 0;
    else
        o = el.offsetLeft;
    
    if (el.offsetParent)
        totalOffsetLeft = (TotalOffsetLeft(el.offsetParent, skipElementId) + o);
    else
        totalOffsetLeft = o;

    return totalOffsetLeft;
}