// JavaScript Document
function mwinit()
{
	dropdownMenu('navigation', 'menuplacement');
}



// protected mailto link
function protectmail(name, domainaddress, linktext1, linktext2)
{
	document.write("<a href='mailto:" + name + "@" + domainaddress + "'>" + linktext1 + linktext2 + "</a>");
}




function loadwindow(url, x, y){
	newwindow=window.open(url, "", "width=" + x + ", height=" + y + ",menubar=0");
	newwindow.focus();
}


// utility attachEventListener
// target is the DOM node
// eventType is the event, like "click"
// functionRef is the function to handle the event
// capture specifies whether the listener is applied during the capture (true) or bubble (false) event propagation phase. Bubble is generally better.
function attachEventListener(target, eventType, functionRef, capture)
{
		// most browsers other than IE allow addEventListener
		if (typeof target.addEventListener != "undefined")
		{
				target.addEventListener(eventType, functionRef, capture);
		}
		// but IE uses attachEvent
		else if (typeof target.attachEvent != "undefined")
		{
				target.attachEvent("on" + eventType, functionRef);
		}
		else
		{
				// fallback for browsers that don't support event listeners, particularly IE5 for Mac
				// if there is already a handler for this event, chain the new one on after the old one.
				eventType = "on" + eventType;
				if (typeof target[eventTyle] == "function")
				{
					var oldListener = target[eventTyle];
					target[eventTyle] = function()
					{
						oldListener();
						return functionRef();
					};
				}
				else
				{
						target[eventType] = functionRef;
				}
		}
}


// utility AddLoadListener
function addLoadListener(fn)
{
		// addEventListener is W3C Standard but not everybody supports it
		if (typeof window.addEventListener != 'undefined')
		{
				window.addEventListener('load', fn, false);
		}
		// in Opera the load event comes from the document, not the window
		else if (typeof document.addEventListener != 'undefined')
		{
				document.addEventListener('load', fn, false);
		}
		// IE uses attachEvent instead
		else if (typeof window.attachEvent != 'undefined')
		{
				window.attachEvent('onload', fn);
		}
		else 
		{
				// last resort - for Mac IE 5 and other older browsers
				// chain on to the previous onload handler 
				var oldfn = window.onload;
				if (typeof window.onload != 'function')
				{
						window.onload = fn;
				}
				else
				{
						window.onload = function()
						{
								oldfn();
								fn();
						};
				}
		}
}



// getElementsByAttribute
function getElementsByAttribute(attribute, attributeValue)
{
	var elementArray = new Array();
	var matchedArray = new Array();
	
	if (document.all)
	{
			elementArray = document.all;
	}
	else
	{
			elementArray = document.getElementsByTagName("*");
	}


	for (var i = 0; i < elementArray.length; i++)
	{
			if (attribute == "class")
			{
					// an element might have multiple classes
					// look for attributeValue at beginning or end of string, or surrounded by blanks
					var pattern = new RegExp("(^| )" + attributeValue + "( |$)");
					if (pattern.test(elementArray[i].className))
					{
							matchedArray[matchedArray.length] = elementArray[i];
					}
					
			}
			else if (attribute == "for")
			{
					// the "for" attribute is called htmlFor in IE
					if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for"))
					{
							if (elementArray[i].htmlFor == attributeValue)
							{
								matchedArray[matchedArray.length] = elementArray[i];
							}
					}
			}
			else if (elementArray[i].getAttribute(attribute) == attributeValue)
			{	
				matchedArray[matchedArray.length] = elementArray[i];
			}
	}
	
	return matchedArray;
}

// getEventTarget(event)
function getEventTarget(event)
{
		var targetElement = null;
		
		if (typeof event.target != "undefined")
		{
				targetElement = event.target;
		}
		else
		{
				targetElement = event.srcElement;
		}
		
		// if the target is a text node, find the first non-text-node parent
		while (targetElement.nodeType == 3 && targetElement.parentNode != null)
		{
				targetElement = targetElement.parentNode;
		}
		return targetElement;
}

// getScrollingPosition
function getScrollingPosition()
{
		var position = [0,0];
		
		// window.pageYOffset used by Firefox / Mozilla, Safari, Konqueror, and Opera
		if (typeof window.pageYOffset != 'undefined')
		{
				position = [ window.pageXOffset, window.pageYOffset ];
		}
		// documentElement.scrollTop used in IE6 in "standards-compliant" mode
		// also recognized in IE5 but value is always zero so it's not useful there
		else if (typeof document.documentElement.scrollTop != 'undefined' &&
				 document.documentElement.scrollTop > 0)
		{
				position = [ document.documentElement.scrollLeft, document.documentElement.scrollTop ];
		}
		// IE5 and IE6 in "quirks" mode
		else if (typeof document.body.scrollTop != 'undefined')
		{
				position = [ document.body.scrollLeft, document.body.scrollTop ];
		}
		
		return position;
}

// get size of current viewport
function getViewportSize()
{
		var size = [0,0];
		
		if (typeof window.innerWidth != 'undefined')
		{
				size = [ window.innerWidth, window.innerHeight ];
		}
		else if (typeof document.documentElement != 'undefined'
				 	&& typeof document.documentElement.clientWidth != 'undefined'
						&& document.documentElement.clientWidth != 0)
		{
				size = [ document.documentELement.clientWidth, document.documentElement.clientHeight ];
		}
		else
		{
				size =  [
						 	document.getElementsByTagName('body')[0].clientWidth,
							document.getElementsByTagName('body')[0].clientHeight
						];
		}
		return size;
}
