/*
	-----------------------------------------------------
	Dynamic iframe-based menu container rendering
	logic.
	-----------------------------------------------------
	Authors:	Marek Lints (lints@axinom.de)
				Maxim Fridental (fridental@axinom.de)	
	Date:		04.02.2005
	-----------------------------------------------------
*/
var MenuContainerComponents = new Array();

MenuContainerFactory = new MenuContainerClass(); 

function MenuContainerClass()
{	
	// Methods
	this.GetDropDownFrame = MenuContainerClass_GetDropDownFrame;
	this.OnAction = MenuContainerClass_OnAction;
	this.OnMouseOver = MenuContainerClass_OnMouseOver;
	this.OnMouseOut = MenuContainerClass_OnMouseOut;
	this.Create = MenuContainerClass_Create;	
	this.CalculateX = MenuContainerClass_CalculateX;
	this.CalculateY = MenuContainerClass_CalculateY;
	this.DestroyChildContainers = MenuContainerClass_DestroyChildContainers;
	this.InsertMenuDiv = MenuContainer_InsertMenuDiv;
	this.GetAlignmentContainer = MenuContainerClass_GetAlignmentContainer;
	this.SetScrollHandler = MenuContainer_SetScrollHandler;
	this.IsMSIE = MenuContainer_IsMSIE;
	this.IsWinMSIE = MenuContainer_IsWinMSIE;
	this.IsMacMSIE = MenuContainer_IsMacMSIE;
	this.Contains = MenuContainer_Contains;
	
	// Variables
	this.ButtonClicked = false;
	this.CurrentDropDown = '';
	this.ScrollHandlerNotSet = true;
	this.MouseOverTheButton = false;
}

function MenuContainerClass_OnAction(theEvent, callerControlId, closeOnSecondCall)
{
	if(this.CurrentDropDown == callerControlId)
	{
		if(closeOnSecondCall)
		{
			this.DestroyChildContainers();	
		}
	}
	else
	{
		this.DestroyChildContainers();	
		this.CurrentDropDown = callerControlId;
		this.Create(theEvent, callerControlId);
	}
}


function MenuContainerClass_OnMouseOver(theEvent, callerControlId, openIfNotOpened)
{
	this.MouseOverTheButton = true;
	if(this.CurrentDropDown == '' && !openIfNotOpened) return;
	this.OnAction(theEvent, callerControlId, false);
}

function MenuContainerClass_OnMouseOut()
{
	this.MouseOverTheButton = false;
}


/*
	The main method for processing dropdown-container-rendering.
*/
function MenuContainerClass_Create(theEvent, callerControlId)
{
	if(this.ScrollHandlerNotSet && window != this.GetDropDownFrame())
	{
		this.SetScrollHandler();
		this.ScrollHandlerNotSet = false;
	}
		
	// It could happen sometimes when page is not loaded completely
	// and a user clicks on a TopMenu item.
	if(!MenuContainerComponents[callerControlId]) return;		
	
	// menuDiv parameters		
	var menuDivParentId = MenuContainerComponents[callerControlId].MenuDivParentId;	
	var menuDivIFrame = MenuContainerComponents[callerControlId].MenuDivIFrame;
	var menuDivIFrameBody = MenuContainerComponents[callerControlId].MenuDivIFrameBody;
	var menuDivContent = MenuContainerComponents[callerControlId].MenuDivContent;
	var menuDivZindex = MenuContainerComponents[callerControlId].Zindex;
	var menuDivWidth = MenuContainerComponents[callerControlId].Width;
	var menuDivHeight = MenuContainerComponents[callerControlId].Height;
	var menuDivBackground = MenuContainerComponents[callerControlId].Background;
	var menuDivRenderDirection = MenuContainerComponents[callerControlId].RenderDirection;
	var menuDivContainerAlignment = MenuContainerComponents[callerControlId].ContainerAlignment;
	
	var theFrame = this.GetDropDownFrame();	// the dropdown frame		
	
	var clientY = (theEvent.toElement)? theEvent.toElement.offsetTop: 0;

    // Hide exception if document isn't loaded yet.
	if(!theFrame.document || !theFrame.document.body) return;
	
	var theMenuDiv = this.InsertMenuDiv(callerControlId + "_MenuDiv");
	
	theMenuDiv.style.width = menuDivWidth;
	theMenuDiv.style.height = menuDivHeight;
	theMenuDiv.style.zIndex = menuDivZindex;
	theMenuDiv.style.left = this.CalculateX(this.GetAlignmentContainer(callerControlId, menuDivParentId), menuDivRenderDirection, menuDivContainerAlignment, menuDivWidth);		
	theMenuDiv.style.top = this.CalculateY(this.GetAlignmentContainer(callerControlId, menuDivParentId), menuDivRenderDirection, clientY);

	if(!this.IsMacMSIE())
	{		
		theMenuDiv.innerHTML = menuDivIFrame;
	
		var iframeObject = theFrame.document.getElementById(callerControlId + "_MenuDivIFrame");	
		if(iframeObject.contentDocument)
		{
			var doct = iframeObject.contentDocument;
		}
		else
		{
			var doct = iframeObject.contentWindow.document;
		}
		if(window.navigator.platform.toLowerCase().indexOf("win") > -1)
		{
			doct.open("text/html", callerControlId)
			doct.write(menuDivIFrameBody);
			doct.close();
		}
		else
		{
			doct.write(menuDivIFrameBody);
		}
	}
	else
	{
		//No need to hack with IFRAME
		theMenuDiv.style.border = '1px solid black';
		theMenuDiv.innerHTML = menuDivContent;
	}	
		
	var oldClickHandler = theFrame.document.body.onclick;
	theFrame.document.body.onclick = function () 
	{			
		if(!MenuContainerFactory.MouseOverTheButton)
		{
			MenuContainerFactory.DestroyChildContainers();
		}
		if(oldClickHandler)
			oldClickHandler();
	}
	if(theEvent.type == 'click')
		theEvent.cancelBubble = true;
}

/*
	Returns the parent object of the current control.
*/
function MenuContainerClass_GetAlignmentContainer(callerControlId, menuDivParentId)
{
	var theFrame = this.GetDropDownFrame();
	if(document.getElementById(callerControlId + "_ButtonDiv"))
		return document.getElementById(callerControlId + "_ButtonDiv");
	else if(theFrame.document.getElementById(menuDivParentId + "_MenuDiv"))
		return theFrame.document.getElementById(menuDivParentId + "_MenuDiv");
	else
		return 0;
}

/*
	Calculates the X-coordinate of the dropdown to be rendered.
*/
function MenuContainerClass_CalculateX(parentObj, renderDirection, containerAlignment, width)
{
	var myBody = this.GetDropDownFrame().document.body;
	if(this.Contains(myBody, parentObj))
	{
		if(renderDirection == 'bottom' || renderDirection == 'top')
		{
			if(containerAlignment == 'right')
			{
				return parentObj.offsetLeft + parentObj.offsetWidth - width;
			}
			else // 'left'
			{
				if(parentObj.style.left)
					return parseInt(parentObj.style.left.replace("px", "")) + parseInt(parentObj.offsetLeft);
				else
					return parentObj.offsetLeft;
			}
		}
		else
		{
			if(renderDirection == 'right')
				return parentObj.offsetLeft + parentObj.offsetWidth;
			else
				alert('left doesn\'t work yet');
	}
	}
	else
	{
		if(renderDirection == 'right')
			return GetScrollLeft(this.GetDropDownFrame()) + 0;
		else
			if(containerAlignment == 'right')
				return GetScrollLeft(this.GetDropDownFrame()) + parentObj.offsetLeft + parentObj.offsetWidth - width;
			else
				return GetScrollLeft(this.GetDropDownFrame()) + parentObj.offsetLeft;
	}
}

/*
	Calculates the Y-coordinate of the dropdown to be rendered.
*/
function MenuContainerClass_CalculateY(parentObj, renderDirection, clientY)
{
	var myBody = this.GetDropDownFrame().document.body;
		
	if(this.Contains(myBody, parentObj))
	{
		if(renderDirection == 'bottom')
		{	
			if(parentObj.style.top)
				return parseInt(parentObj.style.top.replace("px", "")) + parseInt(parentObj.offsetHeight);
		else
				return 0;
		}
		else // TODO: this doesn't work yet as expected
			if(renderDirection == 'left' || renderDirection == 'right')
				return parentObj.offsetTop + clientY;
			else
				alert('Top doesn\'t work yet');
	}
	else
	{	
		if(renderDirection == 'bottom')
		{
			return GetScrollTop(this.GetDropDownFrame()) + 0;
		}
		else
		{
			return GetScrollTop(this.GetDropDownFrame()) + clientY;
		}
	}
}

/*
	Returns the reference to the dropdown frame.
*/
function MenuContainerClass_GetDropDownFrame()
{	
    try
    {	
        if(window.parent.frames.length == 2)
        {
            if(window.parent.frames[0].name == "ControlCenterFrame")
            {
	            var topWindow = AxGetTopWin(window);
	            if(topWindow.frames.length == 2 && topWindow.frames[0].MenuContainerFactory)
	            {
        		    return topWindow.frames[1];
	            }	
	            return topWindow;
	        }
	    }
    }
    catch(e) { /* This exception was thrown because we accessed unacessible iframe. Let's ignore it. */}
    return window;
}

/*
	Destroys all child containers of the given one.
	If id == '0', all containers will be destroyed.
*/
function MenuContainerClass_DestroyChildContainers()
{	
	if(this.CurrentDropDown != '')
	{
		var theFrame = this.GetDropDownFrame();		
		var divElem = theFrame.document.getElementById(this.CurrentDropDown + "_MenuDiv");	
		if(divElem)
		{		
			divElem.parentNode.removeChild(divElem);
		}
		this.CurrentDropDown = '';
		this.ScrollHandlerNotSet = true;
	}
}

/*
	Writes into dropdown frame's document.
*/
function MenuContainer_InsertMenuDiv(id)
{
	var theFrame = this.GetDropDownFrame();
	
	var newDiv = theFrame.document.createElement("div");
	newDiv.id = id;
	newDiv.style.position = 'absolute';
	newDiv.style.cursor = 'pointer';
	newDiv.style.backgroundColor = 'white';

	var helpDiv = theFrame.document.getElementById("TopMenuHelpDiv");
	if(helpDiv)
	{
		helpDiv.appendChild(newDiv);
	}
	else
	{	
		theFrame.document.body.appendChild(newDiv);	
	}
	return theFrame.document.getElementById(id);
}

function MenuContainer_SetScrollHandler()
{
	var theFrame = this.GetDropDownFrame();		
	var theFrameObj;
	if(theFrame.id)
	{
		theFrameObj = theFrame.document.getElementById(theFrame.id);
	}
	else if(theFrame.name)
	{
		theFrameObj = parent.document.getElementsByName(theFrame.name);
		if(theFrameObj.length > 0)
			theFrameObj = theFrameObj[0];
	}
	
	if(theFrameObj)
	{
		var theScrollObject;
		if(window.navigator.appName.toLowerCase().indexOf("microsoft") > -1)
			theScrollObject = theFrameObj.contentWindow.document.body;		
		else
			theScrollObject = theFrameObj.contentWindow;

		if(theScrollObject)
		{			
			SetOnScrollEvent(theScrollObject, theFrame);

			if(window.navigator.appName.toLowerCase().indexOf("microsoft") == -1) return;

			var nodes =  theFrameObj.contentWindow.document.childNodes;
			for(var n = 0; n < nodes.length; n++)
			{
				if(nodes[n].onscroll)
				{
					SetOnScrollEvent(nodes[n], theFrame);
				}
			}


		}
	}
}


function SetOnScrollEvent(theScrollObject, theFrame)
{
	var oldScrollHandler = theScrollObject.onscroll;	
	var scrollFunction = function()
	{		
		if(MenuContainerFactory.CurrentDropDown != '')
		{	
			var elementObj = theFrame.document.getElementById(MenuContainerFactory.CurrentDropDown + "_MenuDiv");
		
			if(elementObj)
			{
				var posTop;
				if(window.navigator.appName.toLowerCase().indexOf("microsoft") > -1)
				{
					//a little bit hacking: sometimes it is in html object, sometimes in body...		
					posTop = Math.max(theFrame.document.documentElement.scrollTop, theFrame.document.body.scrollTop) + "px";
				}
				else
				{
					posTop = theFrame.scrollY + "px";
				}
				elementObj.style.top = posTop;
			}
		}
		if(oldScrollHandler)
			oldScrollHandler();
	}
	theScrollObject.onscroll = scrollFunction;
	theScrollObject.onmousewheel = scrollFunction;
}

function MenuContainer_IsMSIE()
{
	return window.navigator.appName.toLowerCase().indexOf("microsoft") > -1;
}

function MenuContainer_IsMacMSIE()
{
	return this.IsMSIE() && (window.navigator.platform.toLowerCase().indexOf("mac") > -1);
}

function MenuContainer_IsWinMSIE()
{
	return this.IsMSIE() && (window.navigator.platform.toLowerCase().indexOf("win") > -1);
}


function MenuContainer_Contains(container, child)
{
	var parentContainer = child;
	do
	{
		if(parentContainer == container)
		{
			return true;
		}
		parentContainer = parentContainer.parentNode;
	}
	while(parentContainer && parentContainer != parentContainer.parentNode);
	
	return false;
}

function AxGetTopWin(startWindow)
{
	var topWin = startWindow;
	if(topWin)
	{
		while(topWin.parent && topWin.parent != topWin) 
		{
			topWin = topWin.parent;
		}
	}
	return topWin;
}


