/**
 * Initialize Tooltips.
 * @author Christian GEORGESCU
 * $Author$ $Date$ $Revision$
 */
/**
 * Modified Verion to properly work in an iframe
 * @author Alain Grandjean
 */
var tipTag = "a,label,input,img,select,textarea,object,span,tr"; //Which tag do you want to tip-ize? Keep it lowercase!//
var tipX = 10; //This is tip's X offset//
var tipY = -25; //This is tip's Y offset//
var tipYTop = 25; //If y is lower then this, move the tip down.//
var tipYOffsetTop = 15; //Ho down to move the tip when y < tipYTop//

function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}

function getIframeOffsets() {
	if (window.top != window) {
		var iframes = window.top.document.getElementsByTagName("iframe");
		for (var i = 0; i < iframes.length; i++) {
			var id = iframes[i].id || iframes[i].name || i;
			if (window.top.frames[id] == window) {
				return $(iframes[i]).offset();
			}
		}
	} 

	return {top:0,left:0};
}


//There's No need to edit anything below this line// //famous  last words
tooltip = {
	name : "tip",
	offsetX : tipX,
	offsetY : tipY,
	offsetYTop : tipYOffsetTop,
	topYLimit : tipYTop,
	tip : null
};

tooltip.init = function () {
	if (!window.top.document.getElementById) return;

	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";

	// create container in top window (in case of iframe)
	var tipContainer = window.top.document.getElementById(this.name);

	if (!tipContainer) {
		tipContainer = window.top.document.createElementNS ? window.top.document.createElementNS(tipNameSpaceURI, "div") : window.top.document.createElement("div");
		tipContainer.setAttribute("id", this.name);
		window.top.document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}

	this.tip = tipContainer;
	if (this.tip) document.onmousemove = function (evt) {tooltip.move (evt);};

	var a, sTitle, elements;

	var elementList = tipTag.split(",");
	for (var j = 0; j < elementList.length; j++)	{
		elements = document.getElementsByTagName(elementList[j]);
		if (elements) {
			for (var i = 0; i < elements.length; i ++) {
				a = elements[i];
				sTitle = a.getAttribute("title");
				if (sTitle) {
					sTitle = str_replace("::", "<br />",sTitle);
					sTitle = str_replace("[!]", "<strong>",sTitle);
					sTitle = str_replace("[/!]", "</strong><br />", sTitle);
					sTitle = str_replace("[", "<", sTitle);
					sTitle = str_replace("]", ">", sTitle);

					a.setAttribute("tiptitle", sTitle);
					a.removeAttribute("title");
					a.removeAttribute("alt");
					a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'));};
					a.onmouseout = function() {tooltip.hide();};
				}
				else if (a.getAttribute("tiptitle")) {
					a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'));};
					a.onmouseout = function() {tooltip.hide();};
				}	
			}
		}
	}
};

tooltip.move = function (evt) {
	var x=0, y=0;
	if (document.all) {//IE
		x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		x += window.event.clientX;
		y += window.event.clientY;

	} else {//Good Browsers
		x = evt.pageX;
		y = evt.pageY;
	}

	// Add offset if in IFrame
	var offset = getIframeOffsets();

	x = x + offset.left;
	y = y + offset.top;

	this.tip.style.left = (x + this.offsetX) + "px";
	if (y < this.topYLimit){
		this.tip.style.top = (y + this.offsetYTop) + "px";
	} else {
		this.tip.style.top = (y + this.offsetY + 35) + "px";
	}
};

tooltip.show = function (text) {
	if (!this.tip) return;
	this.tip.innerHTML = text;
	if 	($.browser.msie && $.browser.version == "7.0") {
		this.tip.style.display = "inline-block";
		this.tip.style.position = "relative";
		this.tip.style.letterSpacing = "0";
		this.tip.style.zIndex = "100000";
	} else {
		this.tip.style.display = "block";
	}
};

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
};

jQuery(document).ready(function() {
// Fixed EJU-19 - Roll over text not consistent throughout the Portal.
$("body").find("img").each(function(i) {
	if ($(this).attr("title") == undefined || $(this).attr("title").length == 0) {
		$(this).attr("title","");
	}
});
tooltip.init();
});

