var IE = document.all ? true : false;
if (!IE) { document.captureEvents(Event.MOUSEMOVE); }

document.onmousemove = function(e) {
	if (IE) {
		if (document.documentElement) {
			mouseXY.setX(event.clientX + document.documentElement.scrollLeft);
			mouseXY.setY(event.clientY + document.documentElement.scrollTop);			
		}
		else {
			mouseXY.setX(event.clientX + document.body.scrollLeft);
			mouseXY.setY(event.clientY + document.body.scrollTop);
		}
	}
	else {
		mouseXY.setX(e.pageX);
		mouseXY.setY(e.pageY);
	}
}

function Point() {
	var x = 0;
	var y = 0;	
	this.setX = function(x) { this.x = (x > 0) ? x : 0; }	
	this.setY = function(y) { this.y = (y > 0) ? y : 0; }
}

function getElementXY(obj) {
	var point = new Point();

	if (obj.offsetParent) {
		point.x = obj.offsetLeft;
		point.y = obj.offsetTop;

		while (obj = obj.offsetParent) {
			point.x += obj.offsetLeft;
			point.y += obj.offsetTop;
		}
	}

	return point;
}

var mouseXY = new Point();
var selectedSource = null;
var menuXY1 = null;
var menuXY2 = null;
var menuTimeout = null;

function menuOff() {
	if ((menuXY1 == null) || (menuXY2 == null)) {
		var globalNav = document.getElementById("globalNav");
		menuXY1 = getElementXY(globalNav);
		menuXY2 = new Point();
		menuXY2.x = menuXY1.x + parseInt(globalNav.offsetWidth);
		menuXY2.y = menuXY1.y + parseInt(globalNav.offsetHeight);
	}

	if (((mouseXY.x >= menuXY1.x) && (mouseXY.x <= menuXY2.x)) && ((mouseXY.y >= menuXY1.y) && (mouseXY.y <= menuXY2.y))) {
		menuTimeout = setTimeout("menuOff()", 1000);
	}
	else {
		clearTimeout(menuTimeout);
		setMenu(selectedSource, "menuLink", "hidden");
		selectedSource = null;
	}
}

function menuOn(source,event) {
	clearTimeout(menuTimeout);
	setMenu(selectedSource, "menuLink", "hidden");
	setMenu(source, "menuLinkHover", "visible");
	selectedSource = source;
	menuOff();
}

function setMenu(source, className, visibility) {
	if (source) {
		source.className = className;
		if (source.rel) {
			var navmenu = document.getElementById('globalNav');
			var submenu = document.getElementById(source.rel);
			if (navmenu && submenu) {
				if (parseInt(submenu.offsetWidth+source.offsetLeft) < parseInt(navmenu.offsetWidth)) {
					submenu.style.left = source.offsetLeft + "px";
				} else {
					var margin = parseInt(navmenu.offsetWidth) - parseInt(submenu.offsetWidth);
					submenu.style.left = parseInt(margin/2) + "px";
				}
				submenu.style.visibility = visibility;
			}
		}		
	}
}

function searchOnFocus(source) {
	if (source.form.searchReset.value == "false") {
		source.form.searchReset.value = "true";
		source.value = "";
	}
}

function submitSearch(form) {
	var searchString = form.qt.value.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");

	if ((searchString != "") && (form.searchReset.value == "true")) {
		document.location = getPublicUSeekHost()+"/query.html?qt="+escape(searchString);
	}

	return false;
}

function navigateToHome() {
	var userid = readCookie("nykuid");
	isAuthenticated = !((userid == null) || (userid == "") || (userid == "OblixAnonymous"));
	if(isAuthenticated) {
		window.location=getPrivatePegasusHost();
	}
	else {
		window.location=getPublicPegasusHost();
	}
}

function readCookie(name) {
	if (document.cookie) {
		var attrs = document.cookie.split(';');
		var attr;
		var index;
		name+="=";

		for (var i = 0; i < attrs.length; i++) {
			attr = attrs[i];
			index = attr.indexOf(name);
			if (index != -1) { return attr.substring(index+name.length,attr.length); }
		}
	}

	return null;
}

function createSocket() {
var socket = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
var ajax = (window.XMLHttpRequest)?new XMLHttpRequest():null;
	if (!ajax) {
		for (var c=0; c<socket.length; c++) {
			try {
				ajax = new ActiveXObject(socket[c]);
				break;
			} catch(e) {
				ajax = null;
			}
		}
	}
return ajax;
}