// JavaScript Document

subHover = function() {
	var subEls = document.getElementById("menu").getElementsByTagName("li");
	for (var i=0; i<subEls.length; i++) {
		subEls[i].onmouseover=function() {
			this.className+=" subhover";
		}
		subEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" subhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", subHover);

/* keep track of browser height */
var height = 0;

$(window).scroll(function() {
	windowResize();
});

function HandleLoad()
{
	if ($.cookie("nav") == null)
		$.cookie("nav", "open", { path: '/', expires: 1 });
	if ($.cookie("nav") == "open" || location.href.indexOf("/services/") != -1)
	{
		$("#tabs").css({left: "334px"});
		$("#subNav").css({left: "211px"});
		$("#mainContent").css({ marginLeft: "172px" });
		$("#mainContent").css({ marginRight: "28px" });
	}
	$("#subNav").css({ display: "block" });
	$("#tabs").css({ display: "block" });
	HandleSize();
}

var handleTimer = 0;
function HandleSize()
{
	handleTimer = setTimeout('windowResize()', 50);
}

function windowResize()
{
	if (handleTimer)
		clearInterval(handleTimer);

	var mainContent = document.getElementById("mainContent");
	var cHeight = (document.documentElement)? document.documentElement.clientHeight : document.body.clientHeight;
	var iHeight = (window.innerHeight)? window.innerHeight : 0;
	var pageHeight = $("#mainContent").height();

	height = (cHeight > iHeight)? cHeight : iHeight;

	if (height > (pageHeight + 338))
		mainContent.style.height = ((height - 338 >= 550) ? height - 338 : 550) + "px";
	HandleOffset();
}

function HandleOffset()
{
	var offsetTop = calculateBottom();
	$("#tabs").css({ top: offsetTop + "px" });
}

function calculateBottom()
{
	var visibleHeight = 0;
    var pHeight1 = (window.innerHeight && window.innerHeight > 0)? window.innerHeight : 9999999;
    var pHeight2 = (document.documentElement && document.documentElement.clientHeight > 0)? document.documentElement.clientHeight : 9999999;
    var pHeight3 = (document.body && document.body.clientHeight > 0)? document.body.clientHeight : 9999999;

    visibleHeight = (pHeight1 > pHeight2)? pHeight2 : pHeight1;
    visibleHeight = (pHeight3 > visibleHeight)? visibleHeight : pHeight3;
    
    var top = 0;
    var pTop1 = (document.documentElement)? document.documentElement.scrollTop : 0;
    var pTop2 = (document.body)? document.body.scrollTop : 0;
    
    top = (pTop1 > pTop2)? pTop1 : pTop2;

	var offset = (visibleHeight + top - 126 >= 338) ? visibleHeight + top - 126 : 338;
	offset = (height >= visibleHeight + top + 126) ? height - 126 : offset;
	return offset;
}

