// Browser Type Detect Code and Netscape 4 CSS Margin Fix
is_w3c = 1; is_ns = 0; is_ie = 0; is_op = 0;
if( (navigator.userAgent.indexOf('Mozilla')!=-1) && (parseInt(navigator.appVersion) < 5) ) { is_ns = 1; }
if( (navigator.userAgent.indexOf('Opera')!=-1) ) { is_op = 1; }
if(navigator.userAgent.indexOf('MSIE')!=-1) { is_ns = 0; if(navigator.userAgent.indexOf('MSIE 4')!=-1) { is_ie = 1; } }
if( is_ns || is_ie ) { is_w3c = 0; }

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

var preloadFlag = false;
function preloadimages() {

		// Set active class on links to current page
		var links = document.getElementsByTagName('a');
		for (var x = 0; x < links.length; x++)
			{
			if (links[x].href == window.location.toString() && links[x].href.indexOf('#') == -1 )
				{
				var text = links[x].innerHTML.toString();
				if (links[x].className.indexOf('active') == -1 && text.indexOf('back to top') == -1)
					{
					links[x].className += ' active';
					}
				}
			}

	if (document.images) {
//		sub_7_over = newImage("images/sub_7-over.gif");
		order = newImage("images/global-button-processing.gif");
		preloadFlag = true;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

menu_visible = 0;		// 1 = one of the global sub menus is visible
menu_timeout = 0;		// var for delayed hide timer
menu_canhide = 1;		// 1 = sub menu can be hidden
menu_pendinghide = 0;	// 1 = hide was requested during timeout period
menu_names = new Array('videos');

function submenu_timeout() {
	menu_canhide = 1;
	if(menu_pendinghide) {
		submenu_hide();
		}
	}

function submenu_over() {
	menu_pendinghide = 0;
	if(! menu_canhide) {
		clearTimeout(menu_timeout);
		}
	menu_timeout = setTimeout('submenu_timeout()', 1000);
	menu_canhide = 0;
	}

function submenu_display(divname) {
	menu_visible = 1;
	if(! menu_canhide) {
		clearTimeout(menu_timeout);
		}
	menu_timeout = setTimeout('submenu_timeout()', 3000);
	menu_canhide = 0;
	if(is_w3c) {
		for(x=0;x<1;x++) {
			document.getElementById(menu_names[x]).style.visibility = 'hidden';
			}
		document.getElementById(divname).style.visibility = 'visible';
		}
	}

function submenu_hide() {
	if(menu_visible) {
		if(menu_canhide) {
			menu_visible = 0;
			if(is_w3c) {
				for(x=0;x<1;x++) {
					document.getElementById(menu_names[x]).style.visibility = 'hidden';
					}
				}
			}
		else {
			menu_pendinghide = 1;
			}
		}
	}

function submenu_hidenow() {
	if(menu_visible) {
		menu_visible = 0;
		if(is_w3c) {
			for(x=0;x<6;x++) {
				document.getElementById(menu_names[x]).style.visibility = 'hidden';
				}
			}
		}
	}



/**
* Scrolls the page to the specified element
*
* @param			elementId			The ID of the element to scroll to
* @param			elementY			The Y position of element (optional, will be calculated if not specified)
*/
function smoothScroll(elementId, elementY)
	{
	// If the elements vertical location hasn't been specificed, calculate it
	if (arguments.length != 2)
		{
		// Get it's offset
		obj = document.getElementById(elementId);
		obj.style.display = 'block';	// Make sure it's visible, otherwise we can't get it's location
		elementY = obj.offsetTop;

		// If its parent is relative or absolutely positioned, find it's offset and add it to the total
		while (obj.offsetParent)
			{
			obj = obj.offsetParent;
			elementY += obj.offsetTop;
			}

		// Make the scroll stop just above the target (looks nicer)
		elementY -= 15;
		if (elementY < 0)
			{
			elementY = 0;
			}

		// Check to see we're not trying to scroll off the end of the page
		var contentHeight = document.getElementById('page').offsetHeight;
		var windowHeight = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight));
		if ( (contentHeight - windowHeight) < elementY)
			{
			elementY = (contentHeight - windowHeight);
			}
		}

	// Get the current window scroll position
	var yPos = window.scrollY ? window.scrollY : (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

	// Calculate the pixels remaining to scroll and the scroll step size
	var distanceLeft = Math.abs(yPos - elementY);
	var stepSize = 100;
	if (distanceLeft < 400)
		{
		stepSize = 60;
		}
	if (distanceLeft < 200)
		{
		stepSize = 20;
		}
	if (distanceLeft < 50)
		{
		stepSize = 10;
		}

	// Calculate the scroll
	if (yPos < elementY)
		{
		// Scroll down
		yPos += stepSize;

		// Check if we're scrolled past the target
		if (elementY < yPos)
			{
			yPos = elementY;
			}
		}
	else if (elementY < yPos)
		{
		// Scroll Up
		yPos -= stepSize;

		// Check if we're scrolled past the target
		if (yPos < elementY)
			{
			yPos = elementY;
			}
		}

	// Check for less than zero
	if (yPos < 0)
		{
		yPos = 0;
		}
	if (elementY < 0)
		{
		elementY = 0;
		}

	// Scroll window
	window.scrollTo(0, yPos);

	// If we haven't reached the target, run the another scroll step
	if (yPos != elementY)
		{
		setTimeout("smoothScroll('"+elementId+"',"+elementY+");", 10);
		}
	}


/**
* Finds the parent of the element with a node type of parentTagName
*
* @param		element					The element object to find the parent of
* @param		parentTagName		The type of parent element to find
*
* @return		The element's parent object of the specified type, or null if no parent of that type could be found
*/
function findParent(element, parentTagName)
	{
	if (element == null)
		{
		return null;
		}
	else
		{
		if ( element.nodeType == 1 && element.tagName.toLowerCase() == parentTagName.toLowerCase() )
			{
			return element;
			}
		else
			{
			return findParent(element.parentNode, parentTagName);
			}
		}
	}
