WHYUI = {};

window.onload = function() {

	window.onkeydown = function(e) {
	
		if (!e) var e = window.event;
		var escapePressed = e.keyCode == 27 || e.keyCode == e.DOM_VK_ESCAPE;
		
		if(escapePressed) {
	
			// Don't let the browser handle the escape
			e.cancelBubble = true; // Microsoft way
			if (e.stopPropagation) e.stopPropagation(); // W3C way
			e.preventDefault();
		
			if(WHYUI.showing) {
	
				hideDialog(true);					
	
			}
			else {
				
				var obj = WHYUI.elementUnderMouse;
				WHYUI.problematicElement = obj;

				// Find the absolute position of the element behing hovered over
				var curleft = curtop = 0;
				if (obj.offsetParent) {
					do {
						curleft += obj.offsetLeft;
						curtop += obj.offsetTop;
					} while (obj = obj.offsetParent);
				}

				var help = document.getElementById('helpDialog');
				help.style.visibility = "visible";					
				help.style.left = '' + (curleft + 20) + 'px';
				help.style.top = '' + (curtop + WHYUI.problematicElement.offsetHeight + 20) + 'px';		
				WHYUI.previousElement = WHYUI.elementUnderMouse;
				WHYUI.previousElementClassName = WHYUI.elementUnderMouse.className;
				document.getElementById('problemDescription').value = "";	
				document.getElementById('problemDescription').focus();
				WHYUI.elementUnderMouse.className += " subject";
				WHYUI.showing = true;
	
			}
			
		}
	
	};
			
	document.body.onmousemove = function(e) {
	
		// If the event wasn't passed, get it from the IE window object.
		if (!e) e = window.event;
		var targ;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
	
		WHYUI.currentMouseX = e.x;
		WHYUI.currentMouseY = e.y;
		WHYUI.elementUnderMouse = targ;
	
	};
			
	var help = document.createElement('div');
	help.id = 'helpDialog';
	help.style.position = 'absolute';
//	help.style.left = '0px';
//	help.style.top = '0px';
	help.style.visibility = "hidden";
	help.style.background = 'rgba(255,255,255,.9)';
	help.style.border = "1px solid black";
	help.style.padding = "1em";
	help.style.color = "black";
	help.style.zIndex = 10000;
	document.body.appendChild(help);
	help.innerHTML = "Comment or question?<br><br><input name='comment' id='problemDescription' class='problem' type='text' onKeyPress='handleFeedbackEnter(event)'><br><br>press enter when you're done...</form>";

}

function handleFeedbackEnter(event) {

	if(event.keyCode != 13) { return; }

	var note = document.getElementById('problemDescription').value;
	
	WHYUI.problematicElement.title = note;
	
	var child = WHYUI.problematicElement;
	var parent = child.parentNode;
	var path = "";
	while(parent != null) {
	
		var number = 0;
		var tagNumber = 0;
		while(number < parent.childNodes.length && parent.childNodes[number] != child) { 
			number++; 
			if(parent.childNodes[number].tagName == child.tagName) { tagNumber++; }	
		}
		if(number >= parent.childNodes.length) {
			console.log("Couldn't find child in parent's child list");
		}
	
		path = "/" + child.tagName.toLowerCase() + "[" + tagNumber + "]" + path;
		
		child = parent;
		parent = parent.parentNode;
	}

	request(
		"page=" + escape(document.location.href) + '&' +
		"anchor=" + escape(path) + '&' +
		"note=" + escape(note), 
		function(response) {

			if(response) {
				alert("" + response);
			}
			hideDialog(false);

		}
	);
		
}

function getCommentDialog() { 

	return document.getElementById('helpDialog');

}

function hideDialog(removeHighlight) {

	getCommentDialog().style.visibility = "hidden";
	WHYUI.showing = false;

	if(removeHighlight && WHYUI.previousElement != null) {
		WHYUI.previousElement.className = WHYUI.previousElementClassName;		
	}

}

/////////////////////////////////////////////////////
//
// AJAX FUNCTIONS (no side effects)
//
/////////////////////////////////////////////////////


// Sends a GET request to the HTTP server with the given parameters, 
// calling the call back with the resulting text from the server.
function request(parameters, callback) {

	var xmlHttp = getXMLHttp();

	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(callback != null) {
				callback.apply(this, [xmlHttp.responseText]);
			}
		}
	};

	var get = "code/feedback.php?" + parameters;
	xmlHttp.open("GET", get, true); 
	xmlHttp.send(null);

}

// Gets an XMLHttp object in a platform-independent manner
function getXMLHttp() {

	var xmlHttp;

	//Firefox, Opera 8.0+, Safari
	try { xmlHttp = new XMLHttpRequest(); }
	//Internet Explorer
	catch(e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(e) { 
				alert("Your browser does not support AJAX!"); 
				return false; 
			}
		}
	}
	return xmlHttp;

}
