﻿// JScript File

var cX = 0; var cY = 0;
function UpdateCursorPosition(e){ 
    cX = e.pageX; cY = e.pageY;
}

function UpdateCursorPositionDocAll(e){ 
    cX = event.clientX; cY = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}

function initMenu()
{
    if(document.all) { 
        document.onmousemove = UpdateCursorPositionDocAll; 
    }
    else { 
        document.onmousemove = UpdateCursorPosition; 
    }
}

function troggleSubMenu(obj, myParent)
{
    //clearTimeout(menuTimerId);
    if (!document.getElementById(obj)) return false;
    targetSubMenu = document.getElementById(obj);
    
    if(document.all)
	{
		document.onmousemove = UpdateCursorPositionDocAll;
	}
	else
	{
		document.onmousemove = UpdateCursorPosition;
	}
    
    aPosition = findPos(myParent);
    
    targetSubMenu.style.left = aPosition[0] + "px";
    targetSubMenu.style.top = aPosition[1] + 30 + "px";
    
    displayMenu = true;
    
    popupTimerId = setTimeout(function() { showSubMenu(targetSubMenu); }, 200);
    
    targetSubMenu.onmouseout = function()
    {
        if(checkIsStillOnMenu(this) == false)
        {
            clearTimerId = setTimeout(function() { stopIntervalSequence(targetSubMenu); }, 500);
        }
    }
    targetSubMenu.onmouseover = function()
    {
        clearTimeout(popupTimerId);
        popupTimerId = setTimeout(function() { showSubMenu(targetSubMenu); }, 300);
    }
    document.getElementById('HeaderBottom').onmouseout = function()
    {
        if(checkIsStillOnMenu(this) == false)
        {
            clearTimerId = setTimeout(function() { stopIntervalSequence(targetSubMenu); }, 500);
        }
    }
    myParent.onmouseout = function()
    {
        if(checkIsStillOnMenu(this) == false)
        {
            clearTimerId = setTimeout(function() { stopIntervalSequence(targetSubMenu); }, 500);
        }
    }
}
function stopIntervalSequence(obj)
{
    clearTimeout(popupTimerId);
    if(checkIsStillOnMenu(obj) == false)
    {
        clearTimeout(clearTimerId);
        hideSubMenu(obj);
    }
}
function showSubMenu(obj)
{
    obj.style.display = "block";
}
function hideSubMenu(obj)
{
    obj.style.display = "none";
}

function getViewportWidth()
{
	return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
}
function checkIsStillOnMenu(tobj)
{
    objPosition = findPos(tobj);
    if(cX < objPosition[0] + 5 || cX > (objPosition[0] + tobj.offsetWidth - 5))
    {
        return false;
    }
    else if( cY < objPosition[1] + 1 || cY > (objPosition[1] + tobj.offsetHeight - 5))
    {
        //alert(cY + ' ' + ' ' + ' ');
        return false;
    }
    return true;
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
	
function AssignPosition(obj, d) 
{
    iViewportWidth = getViewportWidth();

    aPosition = findPos(obj);
    iPosX = aPosition[0];
    
    if((iPosX + document.getElementById(d).offsetWidth) > iViewportWidth)
    {
        document.getElementById(d).style.left = (cX-10 - document.getElementById(d).offsetWidth) + "px";
    }
    else
    {
        document.getElementById(d).style.left = (cX+10) + "px";
    }
    
    document.getElementById(d).style.top = (cY - document.getElementById(d).offsetHeight - 10) + "px";
}

function DisplayPopup(obj, d)
{
    document.getElementById(d).style.display = "block";
	
	AssignPosition(obj, d);
}

function ShowContent(obj, d, Message)
{		
    if(d.length < 1) { return; }
    		
	if(document.all)
	{
		document.onmousemove = UpdateCursorPositionDocAll;
	}
	else
	{
		document.onmousemove = UpdateCursorPosition;
		//document.getElementById(firstObj).innerHTML = firstObjContain;
	}
	
	if(Message)
	{
	    document.getElementById(d).innerHTML = Message;
	}
	
	popupTimerId = setTimeout(function() { DisplayPopup(obj, d); }, 800);	
	
}


