$.include("js/splendid-lib/splendid-utils.js");
$(document).ready(function() {
	$("#mainNav").addClass("horizontal-nav");
   initNavigation();
});
timeout = null;
timeoutTop = null;

function initNavigation() {
	// alert(jQuery.browser.mozilla);
	
	if (navigator.appVersion.indexOf("Mac")!=-1 && jQuery.browser.mozilla) {
		$("body").append("<iframe src='javascript:false' style='position: absolute; top: 0px; left: 0px; display: none; width: 1px; height: 1px; z-index: 0;' id='nav-iframe' frameborder='0' scrolling='no'>&nbsp;</iframe>");
	}
	
	$("ul.horizontal-nav ul.not").css({
        "position": "absolute",
        "display": "none",
		"z-index" : "5"
    });
	
	$('ul.horizontal-nav, ul.horizontal-nav a').css({"z-index" : "5", "cursor" : "pointer"});

	$('ul.horizontal-nav li.hasLink').mouseover(function() {
		$("ul.current").hide();
		var pos = $(this).position();
		var left = pos.left - 4;
		var top = pos.top + $(this).height();
		var offset = $(this).offset();
		var offsetLeft = offset.left;
		var offsetTop = offset.top + $(this).height();

		// alert("left is" + offsetLeft);

		// var $browser = jQuery.browser;
		
		var $thisWidth = $(this).children("ul.contain").width();
		var $thisHeight = $(this).children("ul.contain").height();

		// 'background-color' : '#FFFFFF',
		if (navigator.appVersion.indexOf("Mac")!=-1 && jQuery.browser.mozilla) {
			$("#nav-iframe").css({'width' : $thisWidth + 'px', 'height' : $thisHeight + 'px', 'left' :  offsetLeft + 'px', 'top' :  offsetTop + 'px', 'z-index' : 1}).show();
		}
		
		$(this).children("ul.contain").css({'left' :  left + 'px', 'top' :  top + 'px', 'z-index' : 5}).addClass("current").show();
		
		
	});

	$('ul.horizontal-nav').mouseout(function() {
		$("ul.current").hide().remove("current");
		$("#nav-iframe").hide();
	});
	

	
	$("#topNav ul.not").css({
        "position": "absolute",
        "display": "none"
    });
	$('#topNav ul.current').show();

	if ($('#topNav ul.current').hasClass('centerAround')) {
		var tempObject = $($('#topNav ul.current').parent());
		var tempPosition = $(tempObject).position();
		var tempWidth1 = $(tempObject).width();
		var tempWidth2 = 0;
		$('li',tempObject).each(function() {
			tempWidth2 = tempWidth2 + $(this).width();
		});
		var tempLeft = (tempPosition.left + (tempWidth1 / 2)) - (tempWidth2 / 2);
		if (isSetAsIE6) {
			$('#topNav ul.current').css('width',(725-tempLeft)+'px');
		}
		$('#topNav ul.current').css('left',tempLeft+'px');
		$('#topNav ul.current').addClass('done');
	} else if ($('#topNav ul.current').hasClass('rightAround')) {
		var tempObject = $($('#topNav ul.current').parent());
		var tempPosition = $(tempObject).position();
		var tempWidth1 = $(tempObject).width();
		var tempWidth2 = 0;
		$('li',tempObject).each(function() {
			tempWidth2 = tempWidth2 + $(this).width();
		});
		var tempLeft = tempPosition.left + tempWidth1 - tempWidth2 - 1;
		if (isSetAsIE6) {
			$('#topNav ul.current').css('width',(725-tempLeft)+'px');
		}
		$('#topNav ul.current').css('left',tempLeft+'px');
		$('#topNav ul.current').addClass('done');
	}


    $('#topNav li.hasLink').hover(
		function() {
		    topNavigation_onHoverOver(this);
		},
		function() {
		   timeoutTop = setTimeout('closeTopNav("#' + this.id + '")', 400);
		}
	);
}

function topNavigation_onHoverOver(which) {
	genNavigation_onHoverOver(which);
}

function closeTopNav(e) {
	clearTimeout(timeoutTop);
	timeoutTop = null;
	closeGenNav(e);
}
function closeGenNav(e) {
	$("ul", e).hide();
	$('#topNav ul.current').show();
	$('#topNav a.selected2').addClass('selected');
	$('#topNav a.selected3').removeClass('selected3');
	checkForDodgyIE6Elements("visible");
}
function genNavigation_onHoverOver(which) {
	if (timeoutTop != null) {
		clearTimeout(timeoutTop);
		timeoutTop = null;
	}
	$('#topNav a.selected2').removeClass('selected');
	$('#topNav a.selected3').removeClass('selected3');
	$('#topNav ul').hide();

	if (timeout != null) {
		clearTimeout(timeout);
		timeout = null;
	}


	$('#'+which.id+' a.tlev').addClass('selected3');
	if ($('ul', which).hasClass('done')) {
		$('ul', which).show();
	} else {
		if ($('ul', which).hasClass('centerAround')) {
			$('ul', which).show();
			var tempPosition = $(which).position();
			var tempWidth1 = $(which).width();
			var tempWidth2 = 0;
			$('ul li', which).each(function() {
				tempWidth2 = tempWidth2 + $(this).width();
			});
			var tempLeft = (tempPosition.left + (tempWidth1 / 2)) - (tempWidth2 / 2);
			if (isSetAsIE6) {
				$('ul', which).css('width',(725-tempLeft)+'px');
			}
			$('ul', which).css('left',tempLeft+'px');
		} else if ($('ul', which).hasClass('rightAround')) {
			$('ul', which).show();
			var tempPosition = $(which).position();
			var tempWidth1 = $(which).width();
			var tempWidth2 = 0;
			$('ul li', which).each(function() {
				tempWidth2 = tempWidth2 + $(this).width();
			});
			var tempLeft = tempPosition.left + tempWidth1 - tempWidth2 - 1;
			if (isSetAsIE6) {
				$('ul', which).css('width',(725-tempLeft)+'px');
			}
			$('ul', which).css('left',tempLeft+'px');
		} else {
			$('ul', which).css('width','725px');
			$('ul', which).show();
		}
		$('ul', which).addClass('done');
	}
	checkForDodgyIE6Elements("hidden");

}
