// Uses jQuery

this.GetWindowDimensions = function() {
	var myWidth = 0, myHeight = 0;
	if (typeof(window.innerWidth) == 'number') {
		// Not Internet Explorer
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in "Standards" Mode
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		// IE 4 & 5+ in "Quirks" Mode
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}

this.GetScrollOffset = function() {
	var xOffset = 0, yOffset = 0;
	if (typeof(window.pageYOffset) == "number") {
		// Netscape
		xOffset = window.pageXOffset;
		yOffset = window.pageYOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    	//DOM compliant
    	xOffset = document.body.scrollLeft;
   	 	yOffset = document.body.scrollTop;
  	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    	//IE6 standards compliant mode
    	xOffset = document.documentElement.scrollLeft;
    	yOffset = document.documentElement.scrollTop;
  	}

	return [ xOffset, yOffset ];
}

this.CurrentPage = function() {
	// rewrite to use baseURL as array
	var currentURL = $(location).attr("href"); // gets the current url
	//var baseURL = $("base").attr("href"); // gets the base url requires the <base> tag to be defined
	var baseURL = "http://www.indiana-eta.org/"; // ugly, not portable, but it works...
	var secondaryBase = "http://bradt.no-ip.org/~ineta/";
	
	$("#menu > ul > li > a").each(function() { // loops through every "a" element that is a child of "#menu > ul > li"
		$(this).filter(function() {  // filters based on the if statements below
			if (currentURL.match($(this).attr("href"))) { // checks to see if the href attribute of the current link shows up in the current URL
				return true;
			} else if (((currentURL == baseURL) || (currentURL == secondaryBase)) && ($("#menu > ul > li > a").index(this) == 0)) { // if the currentURL and baseURL are the same, and we're on the first element in the loop (the home page) then return true
				return true;
			} else { // oh damn, not this element
				return false;
			}
		}).addClass("current").append(" &raquo;"); // if true, give it the "current" class
	});	
}

this.ToolTip = function() {
	xOffset = 10; // Up/Down Offset
	yOffset = 20; // Right/Left Offset
	
	$("a[title][title!='']").hover(function(e) {
		this.t = this.title;
		this.title = "";
		
		// create tooltip
		$("body").append("<div id='tooltip'>" + this.t + "</div>");
		
		// flip variables
		xFlip = false;
		yFlip = false;
		
		// tooltip dimensions
		tooltipSize = [ $("#tooltip").outerWidth(), $("#tooltip").outerHeight() ];
		
		// window size and scroll offset
		windowSize = GetWindowDimensions();
		scrollOffset = GetScrollOffset();
		
		edgeDistance = [ (windowSize[0] + scrollOffset[0] + yOffset - e.pageX),
						 (windowSize[1] + scrollOffset[1] + xOffset - e.pageY) ];
		
		// get x axis orientation
		if((e.pageX + tooltipSize[0] + yOffset + 15) >= (windowSize[0] + scrollOffset[0]))
			xFlip = true;
		// get y axis orientation
		if((e.pageY + tooltipSize[1] + xOffset) >= (windowSize[1] + scrollOffset[1]))
			yFlip = true;
			
		if(xFlip)
			$("#tooltip").css("left",(e.pageX - yOffset*1.25 - tooltipSize[0]) + "px");
		else
			$("#tooltip").css("left",(e.pageX + yOffset) + "px");
			
		if(yFlip)
			$("#tooltip").css("top",(e.pageY + xOffset - tooltipSize[1]) + "px");
		else
			$("#tooltip").css("top",(e.pageY - xOffset) + "px");
			
		$("#tooltip").fadeIn("fast");
		
		$("#debug").html("Width: " + tooltipSize[0] + " Height: " + tooltipSize[1] + " Outer Width: " + $("#tooltip").outerWidth() + " Outer Height: " + $("#tooltip").outerHeight());
		
	},
	function() {
		this.title = this.t;
		// destroy tooltip
		$("#tooltip").remove();
	});
	
	$("a[title][title!='']").mousemove(function(e) {
		// flip variables
		xFlip = false;
		yFlip = false;
		
		// tooltip dimensions
		tooltipSize = [ $("#tooltip").width(), $("#tooltip").height() ];
		
		// window size and scroll offset
		windowSize = GetWindowDimensions();
		scrollOffset = GetScrollOffset();
		
		edgeDistance = [ (windowSize[0] + scrollOffset[0] + yOffset - e.pageX),
						 (windowSize[1] + scrollOffset[1] + xOffset - e.pageY) ];
		
		// get x axis orientation
		if((e.pageX + tooltipSize[0] + yOffset + 15) >= (windowSize[0] + scrollOffset[0]))
			xFlip = true;
		// get y axis orientation
		if((e.pageY + tooltipSize[1] + xOffset) >= (windowSize[1] + scrollOffset[1]))
			yFlip = true;
			
		if(xFlip)
			$("#tooltip").css("left",(e.pageX - yOffset*1.25 - tooltipSize[0]) + "px");
		else
			$("#tooltip").css("left",(e.pageX + yOffset) + "px");
			
		if(yFlip)
			$("#tooltip").css("top",(e.pageY - xOffset - tooltipSize[1]) + "px");
		else
			$("#tooltip").css("top",(e.pageY - xOffset) + "px");									
	});
}

$(document).ready(function() { // Make sure the page is loaded before executing the script
	CurrentPage();
	ToolTip();
});