var url = String(window.location);
baseUrl = url.baseUrl();

if (!nu) {
	var nu = new Object();
}

nu.ToolTip = Class.create();
nu.ToolTip.prototype = {

	xOffset: 10,
	yOffset: -32,
	clip: null,
	showTimeout: null,
	showDelay: 300,
	maxWords: 20,
	visible: false,
	
	initialize: function(){
		new Insertion.Bottom(document.body, "<div id=\"tooltip\">&nbsp;</div>")
		this.clip = $('tooltip');
		new Effect.Opacity(this.clip, {to: 0, duration:0});		
	},
	
	show: function(){
		this.visible = true;
		this.clip.style.visibility = "visible";
		new Effect.Opacity(this.clip, {to: 0.99, from:0, duration:0.25});
	},
	
	hide: function(){
		this.visible = false;	
		new Effect.Opacity(this.clip, {to: 0, from:0.99, duration:0.35, afterFinish: function(){
			if (!this.visible) {			
				this.clip.style.visibility = "hidden";
			}
		}.bind(this)});
	},
	
	setContents: function(ajaxResponse){
		
		var name = ajaxResponse.getElementsByTagName('Name')[0].firstChild.nodeValue;
		var description = ajaxResponse.getElementsByTagName('Description')[0].firstChild.nodeValue;
		this.clip.innerHTML = '<div><h3>' + name + '</h3>' + '<p>' + description + '</p></div>';

	},
	
	followmouse: function(e) {		
		var elementHeight = this.clip.getHeight();
		var elementY = Event.pointerY(e) + this.yOffset;
		var docHeight = document.body.clientHeight + document.body.parentNode.scrollTop;		
		if (elementY + elementHeight > docHeight){
			elementY -= (elementY + elementHeight) - docHeight;
		}
		this.clip.style.left =  Event.pointerX(e) + this.xOffset + "px";
		this.clip.style.top = elementY + "px";
	}
}

function initToolTip() {
	// DON'T CREATE TOOLTIP ON LANDING PAGES
	// CRASHES IE6
	if (!$('flashcontent')) {	
		var toolTip = new nu.ToolTip();	
		var tips = $('content').getElementsByTagName("a");
	
		for (var i=0; i < tips.length; i++){
		
			if (tips[i].getAttribute('href').replace(baseUrl, "").substring(0, 26).toLowerCase() == "/repository/jargon-buster/") {
				var updateRequest;				
	

				tips[i].onmouseover = function(){				

					document.onmousemove = toolTip.followmouse.bindAsEventListener(toolTip);
					var href = '/xml' + this.getAttribute('href').replace(baseUrl, "");			
				
					updateRequest = new Ajax.Request(
					href, {onComplete: function(req) {						
							toolTip.show();
							toolTip.setContents(req.responseXML);
						}
					});
				}
				tips[i].onmouseout = function(){
					document.onmousemove = null;
					Try.these(
						function() { updateRequest.transport.abort() }
					);
					toolTip.hide();			
				}
				tips[i].onclick = function(){
					this.blur();
					return false;
				}
			}
		}
	}
}

Event.observe(window, 'load', initToolTip, false);