
var IE = document.all? true : false;
var dom = (document.getElementById) ? true : false;
var IsSafari = (navigator.appName == 'Netscape' && navigator.userAgent.indexOf("Safari")!=-1) ? true : false;
var IsIE = (navigator.appName == 'Microsoft Internet Explorer') ? true : false;
var IsFirefox = (navigator.appName == 'Netscape' && navigator.userAgent.indexOf("Firefox")!=-1) ? true : false;


if (IE) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else { 
    document.onmousemove = captureMousePosition;
}
// Global variables
// Global variables
var xMousePos, yMousePos;
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen

function captureMousePosition(e) 
{
    if (!e) e = window.event;
    
    if (e)
    {
        if (e.pageX || e.pageY)
        {
            // Netscape 6 behaves the same as Netscape 4 in this regard 
            xMousePos = e.pageX;
            yMousePos = e.pageY;
        }
        else if (e.clientX && e.clientY && document.body && document.body.scrollLeft && document.body.scrollTop)
        {
            xMousePos = e.clientX + document.body.scrollLeft;
            yMousePos = e.clientY + document.body.scrollTop;
        } 
        else if (e.clientX && e.clientY)
        {
            xMousePos = e.clientX;
            yMousePos = e.clientY;
        } 
    }
    if (xMousePos < 0){xMousePos = 0}
    if (yMousePos < 0){yMousePos = 0} 
}


function ShowImageByID(ImageID, ImageUrl)
{   
    if ($get(ImageID) != null)
    {
        $get(ImageID).src = ImageUrl;
    }
}

function PopUpBlockArea(areaID, opacity, height, zindex)
{
    ShowArea(areaID);
    if (height > 0)
    {
        ChangeAreaHeight(areaID, height);
    }
    
    if (opacity > 100)
    {
        opacity = 100;
    }    
    if (opacity < 0)
    {
        opacity = 0;
    }
        	        
    var obj = GetObject(areaID);   
    if (obj && obj.style) 
	{
	    if (obj.style.zIndex != null)
	    {
	        obj.style.zIndex = zindex;
	    }
	    
	    if (obj.style.MozOpacity!=null) 
	    {  
		    /* Mozilla's pre-CSS3 proprietary rule */
		    obj.style.MozOpacity = (opacity/100) - .001;
	    } 
	    else if (obj.style.opacity!=null) 
	    {
		    /* CSS3 compatible */
		    obj.style.opacity = (opacity/100) - .001;
	    } 
	    else if (obj.style.filter!=null) 
	    {
		    /* IE's proprietary filter */
		    obj.style.filter = "alpha(opacity="+opacity+")";
	    }
    }   
    else if (obj && obj.zIndex)
    {
	    obj.zIndex = zindex;
	}
}

var RecentViewTimer = null;
var CloseRecentViewTimer = null;

function PopAreaBelow(AreaID, CompareAreaID)
{
    var Top = GetObjectTop(CompareAreaID) + GetObjectHeight(CompareAreaID);
    var Left = GetObjectLeft(CompareAreaID);     

    if (!IsIE)
    {
        Left = Left + 2;
    }
    if (!IsSafari)
    {
        Top = Top - 5;
    }
    
    clearTimeout(CloseRecentViewTimer); 
    RecentViewTimer = window.setTimeout("MoveArea('" + AreaID + "', " + Top +", " + Left +");", 800);  
}

function CloseRecentlyView(AreaID)
{
    clearTimeout(RecentViewTimer); 
    CloseRecentViewTimer = window.setTimeout("CloseArea('" + AreaID + "');", 500); 
}

function ShowArea(AreaID)
{
    if (dom && document.getElementById(AreaID) && document.getElementById(AreaID).style) 
    {
        document.getElementById(AreaID).style.display = 'block';
    }
    else if (document.layers && document.layers[AreaID])
    {       
        document.layers[AreaID].display = 'block';
    }    
    else if (document.all && document.all[AreaID] && document.all[AreaID].style)
    {
        document.all[AreaID].style.display = 'block';
    }
}

function CloseArea(AreaID) 
{
    if (dom && document.getElementById(AreaID) && document.getElementById(AreaID).style)
    {
        document.getElementById(AreaID).style.display = 'none';      
    }
    else if (document.layers && document.layers[AreaID])
    {       
        document.layers[AreaID].display = 'none';
    }    
    else if (document.all && document.all[AreaID] && document.all[AreaID].style)
    {
        document.all[AreaID].style.display = 'none';
    }
}

// float div
function FloatMenu()
{    
    if (!GetObject('MainProductClassMenuDiv'))
    {
        return true;
    }

    var curTop = GetObjectTop('MainBody');
    var bottomTop = GetObjectTop('bottomTable');
    var originalTop = GetObjectTop('MainProductClassMenuDiv');
    var scrollY = GetPageTop();
    
    var moveToTop, moveToLeft;
    
    // if main body is not in page
    if (curTop <= scrollY)
    {                      
        if (originalTop + 600 < bottomTop)
        {
            moveToTop = scrollY + 1;
        }
        else
        {
            if (originalTop > scrollY + 1)
            {
                moveToTop = scrollY + 1;
            }
            else
            {
                moveToTop = bottomTop - 600; 
            }
        }
        ShowArea('BackToTopDiv');
    }
    else // set position under search bar
    {
        CloseArea('BackToTopDiv');
        
        if (IsSafari)
        {
            moveToTop = curTop + 2;
        }
        else
        {
            moveToTop = curTop;
        }
    }
    
    ChangeAreaWidth('ContentBodyTable', 1000);
    
    // move to left    
    var curLeft = GetObjectLeft('MainBody');
    var contentLeft = GetObjectLeft('MainContentBody');  
    
    if (contentLeft != null && contentLeft - curLeft < 150)
    {    
        if (IsSafari)
        {
            moveToLeft = contentLeft - 148;
        }
        else
        {
            moveToLeft = contentLeft - 149;
        }
        MoveArea('MainProductClassMenuDiv', moveToTop, moveToLeft)
    }
    else
    {
        MoveAreaVertical('MainProductClassMenuDiv', moveToTop);
    }
    
    //FloatSimilarItem();MainContentBody
}

function ScrollToTop()
{
    window.scroll(0,0); 
}

function MoveAreaZIndex(areaID, zindex) {
    var obj = GetObject(areaID);
    if (obj && obj.style) {
        if (obj.style.zIndex != null) {
            obj.style.zIndex = zindex;
        }
    }
    else if (obj && obj.zIndex) {
        obj.zIndex = zindex;
    }

}

function MoveArea(areaID, top, left)
{
    if (dom && document.getElementById(areaID) && document.getElementById(areaID).style)
    {
        document.getElementById(areaID).style.display = 'block';
        document.getElementById(areaID).style.position = 'absolute';
        document.getElementById(areaID).style.left = left+ 'px';
        document.getElementById(areaID).style.top = top + 'px';
    }
    else if (document.layers && document.layers[areaID])
    {
        document.layers[areaID].display = 'block'; 
        document.layers[areaID].position = 'absolute';
        document.layers[areaID].left = left;
        document.layers[areaID].top = top;
    }
    else if (document.all && document.all[areaID] && document.all[areaID].style)
    {
        document.all[areaID].style.display = 'block';
        document.all[areaID].style.position = 'absolute';
        document.all[areaID].style.left = left;
        document.all[areaID].style.top = top;
    }
}

function MoveAreaVertical(areaID, top)
{
    if (dom && document.getElementById(areaID) && document.getElementById(areaID).style)
    {
        document.getElementById(areaID).style.display = 'block';
        document.getElementById(areaID).style.position = 'absolute';
        document.getElementById(areaID).style.top = top + 'px';
    }
    else if (document.layers && document.layers[areaID])
    {
        document.layers[areaID].display = 'block'; 
        document.layers[areaID].position = 'absolute';
        document.layers[areaID].top = top;
    }
    else if (document.all && document.all[areaID] && document.all[areaID].style)
    {
        document.all[areaID].style.display = 'block';
        document.all[areaID].style.position = 'absolute';
        document.all[areaID].style.top = top;
    }
}

// do nothing 
//function FloatSimilarItem() {return true;}

////////////////////////////////////////////////
// menu part
////////////////////////////////////////////////
    var CloseTimer2 = null, CloseTimer3 = null;
    var OpenTimer2 = null, OpenTimer3 = null;
    var LiveMenu2ID = null, LiveMenu3ID = null;
    
    function ShowMenu2(Menu2ID)
    {   
        var thisMenu = GetObject(Menu2ID);
        
        if (thisMenu != null)
        {
            if (LiveMenu2ID == Menu2ID)
            {  
                clearTimeout(CloseTimer2);
            }
            else
            {
                if (LiveMenu2ID != null)
                {
                    clearTimeout(CloseTimer2);
                    CloseArea(LiveMenu2ID);
                }
                LiveMenu2ID = Menu2ID;
                
                ClearOpenTimer();
                OpenTimer2 = window.setTimeout("ShowAreaX('" + Menu2ID + "', 'MenuTable', 'tier1_');", 500);
            }
        }
    }
    
    function CloseMenu2(Menu2ID)
    {      
        var thisMenu = GetObject(Menu2ID);  
        if (thisMenu != null)
        {
            if (LiveMenu2ID == Menu2ID && $get(Menu2ID).style.display == 'none')
            {
                clearTimeout(OpenTimer2); 
                CloseArea(Menu2ID); 
                LiveMenu2ID = null;
            }
            else
            {
                LiveMenu2ID = Menu2ID;
                CloseTimer2 = window.setTimeout("CloseArea('" + Menu2ID + "'); LiveMenu2ID = null;", 500);
            }
        }
    }
    
    function ShowMenu3(Menu3ID)
    {   
        var thisMenu = GetObject(Menu3ID);
        
        if (thisMenu != null)
        {
            if (LiveMenu3ID == Menu3ID)
            {  
                clearTimeout(CloseTimer3);
            }
            else
            {
                if (LiveMenu3ID != null)
                {
                    clearTimeout(CloseTimer3);
                    CloseArea(LiveMenu3ID);
                }
                LiveMenu3ID = Menu3ID;
                
                ClearOpenTimer();
                OpenTimer3 = window.setTimeout("ShowAreaX('" + Menu3ID + "', '" + LiveMenu2ID +"', 'tier2_');", 500);
            }
        }
    }
    
    function CloseMenu3(Menu3ID)
    {      
        var thisMenu = GetObject(Menu3ID);  
        if (thisMenu != null)
        {
            LiveMenu3ID = Menu3ID;
            CloseTimer3 = window.setTimeout("CloseArea('" + Menu3ID + "'); LiveMenu3ID=null; ", 500);
        }
    }
    
    function ClearOpenTimer()
    {
        if (OpenTimer2 != null)
        {
            clearTimeout(OpenTimer2);
        }
        if (OpenTimer3 != null)
        {
            clearTimeout(OpenTimer3);
        }
    }
    
    function HoverStaticMenu(event)
    {
        event.className='StaticMenuHover';  
    }
    
    function LeaveStaticMenu(event)
    {
        event.className='StaticMenu';  
    }
    
    function HoverPopMenu(event)
    {
        event.className='PopMenuHover';  
    }
    
    function LeavePopMenu(event)
    {
        event.className='PopMenuItem';  
    }
// end of menu part

    // Jan 30, 2009 - p.Gargantiel
    //   position an IFRAME under DIV to address IE6 bug (drop-down list box always displays on top)
    function positionFrame(frameid, divid)
    {	    
	    var div = GetObject(divid);
	    var frm = GetObject(frameid);
	    	    
	    if (frm && div)
	    {
	        frm.style.left = div.style.left;
	        frm.style.top = div.style.top;
	        frm.style.height = div.offsetHeight;
	        frm.style.width = div.offsetWidth;
	        frm.style.display = 'block';
	    }
    }


    // Mar 26, 2009 - p.Gargantiel
    //  position secondary/tertiary level menu to viewable area
    function ShowAreaX(AreaID, parentTable, prefix) {
        var menuItemYLoc;
        var menuHeight;
        var windowHeight;
        var menuItemHeight;
        var submenuOffset;
        var pageOffset;
        var menuYLoc;
        var shims;

        menuItemHeight = 28;    // per PopMenuItem in StyleSheet
        shims = 7;              // space filler

        // set display attribute to show content
        if (dom && document.getElementById(AreaID) && document.getElementById(AreaID).style) {
            document.getElementById(AreaID).style.display = 'block';
        }
        else if (document.layers && document.layers[AreaID]) {
            document.layers[AreaID].display = 'block';
        }
        else if (document.all && document.all[AreaID] && document.all[AreaID].style) {
            document.all[AreaID].style.display = 'block';
        }

        // retrieve top y-position of static (tier 1) menu
        menuYLoc = GetObjectTop(parentTable);

        // retrieve top y-position of static (tier 1) menu item
        menuItemYLoc = GetObjectTop(prefix + AreaID);

        // retrieve height of submenu (tier 2 menu)
        menuHeight = GetObjectHeight(AreaID);

        // retrieve window screen height
        windowHeight = GetPageInnerSize()[0];

        // retrieve page offset
        pageOffset = GetPageTop();

        // if the submenu (tier 2 menu) is larger than the viewable area, render as-is
        if ((menuHeight + menuItemHeight) > windowHeight) {
            // compute standard, vertical offset of the submenu
            submenuOffset = menuItemYLoc - menuYLoc + shims;

            // move submenu to its standard y-location with the given offset
            MoveAreaVertical(AreaID, submenuOffset);
        }
        // if the submenu (tier 2 menu) will be rendered below the viewable area, move submenu up sufficiently
        // such that it will be rendered within the viewable area
        else if ((menuItemYLoc + menuHeight) > (windowHeight + pageOffset)) {
            var overflow;
            var menuItemMultiple;

            // determine how much it will exceed
            overflow = (menuItemYLoc + menuHeight) - (windowHeight + pageOffset);

            // compute the multiple of menu-item-height to shift up the submenu
            menuItemMultiple = Math.floor(overflow / menuItemHeight) + 1;

            // compute the vertical offset of the submenu
            submenuOffset = menuItemYLoc - menuYLoc + shims - (menuItemMultiple * menuItemHeight);

            // move submenu to its new y-location with the given offset
            MoveAreaVertical(AreaID, submenuOffset);
        }
        // otherwise, render the submenu as-is (without vertical shift)
        else {
            // compute standard, vertical offset of the submenu
            submenuOffset = menuItemYLoc - menuYLoc + shims;

            // move submenu to its standard y-location with the given offset
            MoveAreaVertical(AreaID, submenuOffset);
        }
    }    