// hints.js by antonone
// http://anadoxin.org/blog

function addEvent(obj, evName, cbFunc) {
	if(document.addEventListener)
		obj.addEventListener(evName, cbFunc, false);
	else if(document.attachEvent)
		return obj.attachEvent("on" + evName, cbFunc);
	else
		return false;

	return true;
}

function is_ie() {
    if(navigator.appVersion && navigator.appVersion.indexOf("MSIE") != -1) 
        return true;
    else
        return false;
}

function hintsInit() {
	//if(window.opera) {
		hookTags("A", "title");
		//hookTags("SPAN", "title");
		hookTags("IMG", "alt");
	//}
}

function hookTags(tagname, attrname) {
	var tags = document.getElementsByTagName(tagname);
	for(var i = 0, len = tags.length; i < len; i++) {
		if(tags[i]) {
			var alt_text = tags[i].getAttribute(attrname);
			if(alt_text != null) {
				tags[i].removeAttribute(attrname);
				tags[i].setAttribute("custom_title", alt_text);

				hookTag(tags[i], alt_text);
			}
		}
	}
}

function hookTag(tag, text) {
	addEvent(tag, "mouseover", tagMouseOver);
	addEvent(tag, "mouseout", tagMouseOut);
}

function getTarget(evt) {
	if(evt.srcElement)
		return evt.srcElement; // IE
	else {
		var obj = evt.target; // FF, O

		// Safari
		if(obj.nodeType == 3) {
			return obj.parentNode;
		} else {
			return obj;
		}
	}
}

var hint = null;
function showHint(x, y, obj) {
	var text = obj.getAttribute("custom_title");

	if(hint == null) {
		hint = document.createElement("div");
		hint.style.position = "absolute";
		hint.style.left = x + "px";
		hint.style.top = y + "px";
		hint.style.background = "white";
		hint.style.visibility = "visible";
		hint.style.padding = "3px";
		hint.style.border = "1px solid #990000";
		hint.style.opacity = "0.9";

		hint.innerHTML = text;

		document.body.appendChild(hint);
	}
}

function tagMouseOver(e) {
	var target = getTarget(e), x, y;
	
	//if(window.opera) {
		//x = parseInt(e.screenX), y = parseInt(e.screenY);
	//} else {
		//x = parseInt(e.clientX) + parseInt(document.body.scrollLeft);
		//y = parseInt(e.clientY) + parseInt(document.body.scrollTop);
		x = parseInt(e.pageX);
		y = parseInt(e.pageY);
	//}

	showHint(x + 15, y + 15, target);
}

function tagMouseOut(e) {
	var target = getTarget(e);

	if(hint) {
		document.body.removeChild(hint);
		hint = null;
	}
}

