﻿// Layout helper functions for NetCampus

if (typeof(_fileVersions) == 'undefined')
{
	eval('var _fileVersions = new Array()');
}
_fileVersions.push('Layout 6.6.0.0');

function BitEnabled(bitmask, testbit)
{
	return (bitmask & testbit) == testbit;
}

// Dock an element inside a RadWindow relative to the RadWindow
function DockToWindow(wnd, element, dock)
{	
	var leftMargin = 2;
	var topMargin = 4;
	var bottomMargin = 40;
	var elementTop = 0;
	var elementLeft = 0;
	
	element.style.position = 'absolute';

	if (BitEnabled(dock, Dock.Center))
	{
	    elementTop = (wnd.get_height() / 2) - (element.offsetHeight / 2) - bottomMargin;
	    elementLeft = (wnd.get_width() / 2) - (element.offsetWidth / 2);
	}

	if (BitEnabled(dock, Dock.Bottom))
	{
	    elementTop = wnd.get_height() - element.offsetHeight - bottomMargin;
	}

	if (BitEnabled(dock, Dock.Right))
	{
	    elementLeft = wnd.get_width() - element.offsetWidth;	
	}

	if (BitEnabled(dock, Dock.Left))
	{
		elementLeft = leftMargin;
	}

	if (BitEnabled(dock, Dock.Top))
	{
		elementTop = topMargin;
	}
	
	element.style.top = elementTop > topMargin ? elementTop : topMargin;
	element.style.left = elementLeft > leftMargin ? elementLeft : leftMargin;
}

// Doc a RadWindow relative to the application (browser) borders
function DockWindow(wnd, dock)
{
	// When centering only, we can use the builtin method in the RadWindow.
	if (dock == Dock.Center)
	{
		wnd.center();
		return;
	}

	var leftMargin = 2;
	var topMargin = 4;
	var bottomMargin = 34;
	var undefinedPosition = -9999;

	var appHeight = GetApplicationHeight();
	var appWidth = GetApplicationWidth();
	var wndWidth = wnd.get_width();
	var wndHeight = wnd.get_height();
	var top = undefinedPosition;
	var left = undefinedPosition;
	
	if (BitEnabled(dock, Dock.Center))
	{
		top = (appHeight / 2) - (wndHeight / 2) - bottomMargin;		
		left = (appWidth / 2) - (wndWidth / 2);
	}

	if (BitEnabled(dock, Dock.Bottom))
	{
		top = appHeight - wndHeight - bottomMargin;
	}

	if (BitEnabled(dock, Dock.Right))
	{
		left = appWidth - wndWidth;	
	}

	if (BitEnabled(dock, Dock.Left))
	{
		left = leftMargin;
	}

	if (BitEnabled(dock, Dock.Top))
	{
		top = topMargin;
	}

	if (left != undefinedPosition)
	{
		if (left < leftMargin)
		{
			left = leftMargin;
		}
	}
	else
	{
		left = wnd.getWindowBounds().x;
	}
	
	if (top != undefinedPosition)
	{
		if (top < topMargin)
		{
			top = topMargin;
		}		
	}	
	else
	{
		top = wnd.getWindowBounds().y;
	}

	wnd.moveTo(left, top);
}

// Move a window to the cursor position, if possible, and make sure that
// the new position is inside the application boundaries.
function MoveToCursor(wnd, sourceEvent)
{
	var applicationHeight = GetApplicationHeight();
	var applicationWidth = GetApplicationWidth();
	var height = wnd.get_height();
	var width = wnd.get_width();
	var bottomMargin = 5;
	var left;
	var top;
	var adjustPosition = false;
	
	if (sourceEvent == undefined)
	{
		sourceEvent = event;
	}
	
	if(sourceEvent != null)
	{
		left = sourceEvent.clientX - width / 2;
		top = sourceEvent.clientY - height / 2;
		
		if (left < 0)
		{
			left = 0;
		}
		
		if (top < 0)
		{
			top = 0;
		}
	
		adjustPosition = true;
	}
	else
	{
		top = wnd.getWindowBounds().y;
		left = wnd.getWindowBounds().x;
	}		

	if (top + height + bottomMargin > applicationHeight)
	{
		top = applicationHeight - height - bottomMargin;
		adjustPosition = true;
	}

	if (left + width > applicationWidth)
	{
		left = applicationWidth - width;
		adjustPosition = true;
	}

	if (adjustPosition)
	{
		wnd.MoveTo(left, top);
	}	
}

// Make sure a window is inside the application boundaries.
function KeepInsideBoundaries(wnd)
{
	var applicationHeight = GetApplicationHeight();
	var applicationWidth = GetApplicationWidth();
	var height = wnd.get_height();
	var width = wnd.get_width();
	var top = wnd.getWindowBounds().y;
	var left = wnd.getWindowBounds().x;
	var bottomMargin = 5;

	if (top + height + bottomMargin > applicationHeight)
	{
		top = applicationHeight - height - bottomMargin;
		adjustPosition = true;
	}

	if (left + width > applicationWidth)
	{
		left = applicationWidth - width;
		adjustPosition = true;
	}

	if (adjustPosition)
	{
		wnd.MoveTo(left, top);
	}	
}

function GetApplicationHeight()
{
    return top.innerHeight ||
        top.document.documentElement.clientHeight || 
        top.document.body.clientHeight;
}

function GetApplicationWidth()
{
	var wnd = window;

	while(wnd.parent.frameElement != null)
	{
		wnd = wnd.parent;
	}

    if(wnd.frameElement != null)
    {
    	return wnd.frameElement.offsetWidth;
    }
    else
    {
        return document.body.offsetWidth;
    }
}

function GetFrameTop(wdw)
{
	var parentHeight = 0;

	if (!wdw)
	{
		wdw = window;
	}

	do
	{
        parentHeight += GetTop(wdw.frameElement);
		wdw = wdw.parent;
	} while(wdw != wdw.parent);	
	
	return parentHeight;
}

// Get element's top position relative to beginning of element's body
function GetTop(element)
{
	var top = 0;

    // IE offsetParent of FRAME is null. In Firefox it's Frameset.
	while(element != null && element.tagName != 'FRAMESET')
	{
		top += element.offsetTop;
		element = element.offsetParent;
	}
	
	return top;
}

// Get element's left position relative to beginning of element's body
function GetLeft(element)
{
	var left = 0;

	while (element != null && element.tagName != 'FRAMESET')
	{
	    left += element.offsetLeft;
	    element = element.offsetParent;
	}
	
	return left;
}

function AdjustIframeLayout(elementName)
{
	if (elementName == null)
	{
		elementName = 'IframeSection';
	}

	var element = $get(elementName);

	if (element == null && frameElement.tagName.search(/^iframe$/i))
	{
		element = frameElement;
	}
	
	if (element != null)
	{
		var maxHeight = GetApplicationHeight() - GetFrameTop() - GetTop(element);

		if (element.offsetHeight != maxHeight)
		{
			element.style.height = maxHeight + 'px';	
		}
	}
}



