// IE Mac compatibility code
if(typeof Array.prototype.push=='undefined')
  Array.prototype.push=function(){
    var i=0;
    b=this.length,a=arguments;
    for(i;i<a.length;i++)this[b+i]=a[i];
    return this.length
  };

// Utility Functions

function registerLoadTask(fn) {
	if(typeof fn == 'function') {
		$(document).ready(fn);
	}
	else {
		$(document).ready(function(){eval(String(fn));});
	}
}

function registerUnloadTask(fn) {
	if(typeof fn == 'function') {
		$(window).unload(fn);
	}
	else {
		$(window).unload(function(){eval(String(fn));});
	}
}

// Pop-ups
//registerLoadTask(function(){addLinkHandlers("A")});
registerLoadTask(function(){addLinkHandlers("AREA")});

function addLinkHandlers(tagname) {
	$(tagname).click(function(){return handleLinkRel(this);});
}

function handleLinkRel(link) {
	var href, rel, relbits;

	if(typeof link.getAttribute == 'undefined') {
		href = link.href;
		rel = link.rel;
	}
	else {
		//* This one is correct in both IE6 and Firefox
		href = link.getAttribute('href', 2);
		rel= link.getAttribute('rel')
	}

	relbits = rel.split('|');

	switch(relbits[0]) {
		case 'popup':
  			switch(relbits.length) {
  				case 1:
  					// No parameters
  					window.open(href);
  					break;
  				case 2:
  					// One parameter. Use it for window properties
  					window.open(href, null, relbits[1]);
  					break;
  				case 3:
  				default:
  					// Two (or more) parameters. Use 'em both
  					window.open(href, relbits[1], relbits[2]);
  					break;
  			}
			return false;
  			break;
  		default:
  			//Unknown link rel -- do nothing
			return true;
	}
}

function showPrintableVersion() {
	var url = 'printer.asp';
	url += window.location.search;
	window.open(url, 'popup', '');
}

// Image swapping
function imgSwap(obj, newsrc) {
	if(typeof obj == 'string') {
		obj = document.getElementById(obj);
	}
	if(!obj) return;
	obj.oldsrc = obj.src;
	obj.src = newsrc;
	return true;
}

function imgRestore(obj) {
	if(typeof obj == 'string') {
		obj = document.getElementById(obj);
	}
	if(!obj) return;
	if(obj.oldsrc) obj.src = obj.oldsrc;
	return true;
}

function createBookmarkLink(text, className) {
	if(window.opera) {
		document.write('<a href="' + location.href + '" rel="sidebar"' + (className ? ' class="' + className + '"' : '') + '>' + text + '<a>');
	}
	else {
		document.write('<a href="javascript:addBookmark()" ' + (className ? ' class="' + className + '"' : '') + '>' + text + '<a>');
	}
}

function addBookmark() {
    var ua=navigator.userAgent.toLowerCase();
    var isKonq=(ua.indexOf('konqueror')!=-1);
    var isSafari=(ua.indexOf('webkit')!=-1);
    var isMac=(ua.indexOf('mac')!=-1);

    if(window.external && (typeof window.external.AddFavorite == 'unknown')) {
        window.external.AddFavorite(location.href, document.title);
	}
	else if(isKonq) {
		alert('Press Ctrl+B to bookmark this page.');
	}
	else if(window.home || isSafari) {
		alert('Press ' + (isMac ? 'Cmd' : 'Ctrl') + ' + D to bookmark this page.');
	}
	else if(!window.print || isMac) {
		alert('Press Cmd+D to bookmark this page.');    
	}
	else {
		alert('Sorry, it looks like you will have to bookmark this page manually using your browser.');
	}
}

// Dynamic menus
var menuShowDelay = 200;
var menuHideDelay = 500;
window.visibleMenus = new Array();

function menuMouseOver(node) {
	$(node).addClass('over');
	var menu = findMenu(node);
	if(!menu) return;
	if(menu.showPending) {window.clearTimeout(menu.showPending); menu.showPending = false;}
	if(menu.hidePending) {window.clearTimeout(menu.hidePending); menu.hidePending = false;}
	if(menu.style.display == 'block') return;
	menu.showPending = window.setTimeout(function(){showMenu(menu);}, menuShowDelay);
}

function menuMouseOut(node) {
	$(node).removeClass('over');
	var menu = findMenu(node);
	if(!menu) return;
	if(menu.showPending) {window.clearTimeout(menu.showPending); menu.hidePending = false;}
	if(menu.hidePending) {window.clearTimeout(menu.hidePending); menu.showPending = false;}
	if(menu.style.display == 'none') return;
	menu.hidePending = window.setTimeout(function(){hideMenu(menu);}, menuHideDelay);
}

function findMenu(node) {
	if(typeof node.getElementsByTagName != 'undefined') {
		var children = node.getElementsByTagName('DIV');
		if(children)
			return children[0];
		else
			return null;
	}
	else {
		var children = node.childNodes;
		for(var i=0; i < children.length; i++)
			if (children[i].nodeName == 'DIV')
				return children[i];
		return null;
	}
}

function hideMenusNow() {
	var vis = window.visibleMenus;
	var newvis = new Array();
	var i = 0;
	for(i=0; i < vis.length; i++) {
		var menu = vis[i];
		if(menu.hidePending) {
			window.clearTimeout(menu.hidePending);
			menu.hidePending = false;
			menu.style.display = 'none';

			var menuUL = menu.getElementsByTagName('UL')[0];
			if (menuUL && typeof menuUL.oldLeft != 'undefined') menuUL.style.left = menuUL.oldLeft;
		}
		else {
			newvis.push(vis[i]);
		}
	}
	window.visibleMenus = newvis;
	if (newvis.length == 0) {showSelectControls(document.body);}
}

function hideMenu(menu) {
	if(!menu) return;
	var vis = window.visibleMenus;
	var newvis = new Array();
	menu.style.display = 'none';
	for(var i = 0; i < vis.length; i++) {
		if(vis[i] != menu) {
			newvis.push(vis[i]);
		}
	}
	window.visibleMenus = newvis;
	if (newvis.length == 0) {showSelectControls(document.body);}

	var menuUL = menu.getElementsByTagName('UL')[0];
	if (menuUL && typeof menuUL.oldLeft != 'undefined') menuUL.style.left = menuUL.oldLeft;
}

function showMenu(menu) {
	if(!menu) return;
	hideMenusNow();
	var embeds = document.getElementsByTagName('EMBED');
	var objects = document.getElementsByTagName('OBJECT');
	if(embeds.length == 0 && objects.length == 0) {
		// Doing this causes Flash to re-start in Firefox
		// Not doing it can cause a flash of horizontal scroll bar.
		// The scroll bar is less annoying.
		document.body.style.overflowX = 'hidden';
	}
	menu.style.visibility = 'hidden';
	menu.style.display = 'block';
	if(window.visibleMenus.length == 0) {hideSelectControls(document.body);}
	window.visibleMenus.push(menu);

	var viewportWidth = document.getElementsByTagName('BODY')[0].offsetWidth;
	var menuUL = menu.getElementsByTagName('UL')[0];
	if(menuUL) {
		var menuWidth = menuUL.offsetWidth;
		var menuLeft = menuUL.offsetLeft;
		var tmp = menuUL;
		while(tmp = tmp.offsetParent) {
			menuLeft += tmp.offsetLeft;
		}
		//Menu moving temporarily disabled for this site.
		if(false && menuLeft + menuWidth > viewportWidth) {
			// This menu would go off the right side of the viewport
			menuUL.oldLeft = menuUL.style.left;
			tmp = menu.parentNode;
			if(/\btopLevelNavItem\b/.test(tmp.className)) {
				// It's the first menu, align it to the right of the viewport
				//menuUL.style.left = (viewportWidth - menuLeft - menuWidth - 1) + 'px';

				// It's the first menu, align it at the right of parent
				menuUL.style.left = (tmp.offsetWidth - menuWidth + 1) + 'px';
			}
			else {
				// It's a sub-sub-menu, make it come out on the left
				menuUL.style.left = '-' + (menuWidth-5) + 'px';
			}
		}
	}
	menu.style.visibility = '';
	if(embeds.length == 0 && objects.length == 0) {
		document.body.style.overflowX = '';
	}
}

function hideSelectControls(root) {
	if(! navigator.appVersion.match(/MSIE [1-6]/)){
		return;
	}
	var selectControls = root.getElementsByTagName('SELECT');
	for(var i = 0 ; i < selectControls.length; i++) {
		selectControls[i].style.visibility = 'hidden';
		selectControls[i]._hiddenByRefract = true;
	}
}

function showSelectControls(root) {
	if(! navigator.appVersion.match(/MSIE [1-6]/)) {
		return;
	}
	var selectControls = root.getElementsByTagName('SELECT');
	for(var i = 0 ; i < selectControls.length; i++) {
		if(selectControls[i]._hiddenByRefract) {
			selectControls[i].style.visibility = 'visible';
			//delete(selectControls[i]._hiddenByRefract);
		}
	}
}

function isValidDate(d, m, y, acceptShortYear) {
	var day = parseInt(d, 10);
	var month = parseInt(m, 10);
	var year = parseInt(y, 10);
	if(isNaN(day) || isNaN(month) || isNaN(year)) return false;
	if(!acceptShortYear && (String(y).length < 4)) return false;
	if(month < 1 || month > 12) return false;
	switch(month) {
	case 2:
		if (day > 29) return false;
		if (day == 29 && (year%4!=0 || (year%100==0 && year%400!=0))) return false;
	case 4:
	case 6:
	case 9:
	case 11:
		if (day > 30) return false;
	default:
		if (day > 31) return false;
	}
	return true;
}

function getLeft(element) {
	var left = 0, absoluteAncestor = false, computedStyle = (document.defaultView && document.defaultView.getComputedStyle), currentStyle = (element.currentStyle);
//	so long as the element has an positioning context...
	while (element.offsetParent) {
//		add the left offset of the element
		left += element.offsetLeft
//		set the element to the element which provided the positioning context
		element = element.offsetParent;
//		if the element hasLayout (IE-only property)
		if (currentStyle && element.currentStyle.hasLayout && element.nodeName.toLowerCase() != 'html') {
//			add the width of the left border
			left += element.clientLeft;
//			and if it's absolutely positioned, flag that we've got an absolutely positioned ancestor
			if (element.currentStyle['position'] == 'absolute') absoluteAncestor = true;
//		otherwise, if it's a browser that supports computedStyle & the element is absolutely positioned
		} else if (computedStyle && document.defaultView.getComputedStyle(element, "").getPropertyValue('position') == 'absolute') {
//			add the left border of the element
			left += parseInt(document.defaultView.getComputedStyle(element, "").getPropertyValue('border-left-width'));
//			and flag that we've got an absolutely positioned ancestor
			absoluteAncestor = true;
		}
	}
//	if the browser supports currentStyle (i.e. is IE) and we found an absolutely positioned ancestor, add any left margin on the BODY
	if (!absoluteAncestor && currentStyle) return left += document.getElementsByTagName('BODY')[0].offsetLeft;
//	otherwise, if we found an absolutely positioned ancestor and the browser supports computed style add any left border from the BODY
	else if (!absoluteAncestor && computedStyle) return left += parseInt(document.defaultView.getComputedStyle(document.getElementsByTagName('BODY')[0], "").getPropertyValue('border-left-width'));
}
