/*
	Unobtrusive tooltips - Cathal McCarthy (camccarthy@fexco.com) 26.07.07 - FEXCO I.T.
*/

window.onload = function() { enableTooltips() };

/*if (typeof document.attachEvent != 'undefined') {
	window.attachEvent('onload', enableTooltips(document)); 
} else {
	window.addEventListener('load', enableTooltips(document), false);
}*/

var enableFade = true;

function enableTooltips(id) {
	var links,i,h;
	if (!document.getElementById || !document.getElementsByTagName) return;
	attachCSS();
	h = document.createElement("span");
	h.id = "tooltip";
	h.setAttribute("id", "tooltip");
	h.style.position = "absolute";
	document.getElementsByTagName("body")[0].appendChild(h);
	if (id == null) links = document.getElementsByTagName("*");
	else links = document.getElementById(id).getElementsByTagName("*");
	for (i=0; i<links.length; i++) {
		if (links[i].className == "tooltip") {
			setupEl(links[i]);
		}
	}
}

function setupEl(el) {
	var tooltip, t, b, s, l;
	t = el.getAttribute("title");
	if (t == null || t.length == 0) t = "link:";
	el.removeAttribute("title");
	tooltip = createEl("span", "tooltip");
	s = createEl("span", "top");
	s.appendChild(document.createTextNode(t));
	tooltip.appendChild(s);
	b = createEl("b", "bottom");
	l = el.getAttribute("alt");
	if (l.length > 30) l = l.substr(0, 27) + "...";
	b.appendChild(document.createTextNode(l));
	tooltip.appendChild(b);
	setOpacity(tooltip, 0);
	el.tooltip = tooltip;
	el.onmouseover = showTooltip;
	el.onmouseout = hideTooltip;
	el.onmousemove = getLocation;
}

function showTooltip(e) {
	el = document.getElementById("tooltip").appendChild(this.tooltip);
	getLocation(e);
	if (enableFade) fadeOpacity(el, 0, 85, 300);
		else setOpacity(el, 85);
}

function hideTooltip(e) {
	var d = document.getElementById("tooltip");
	if (d.childNodes.length > 0) d.removeChild(d.firstChild);
}

function getLocation(e) {
	var posx = 0, posy = 0;
	if (e == null) e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX; 
		posy = e.pageY;
	}
	else if(e.clientX || e.clientY){
		if(document.documentElement.scrollTop) {
			posx = e.clientX + document.documentElement.scrollLeft;
			posy = e.clientY + document.documentElement.scrollTop;
		} else {
			posx = e.clientX + document.body.scrollLeft;
			posy = e.clientY + document.body.scrollTop;
		}
	}
	document.getElementById("tooltip").style.top = (posy - 1) + "px";
	document.getElementById("tooltip").style.left = (posx + 1) + "px";
}

function setOpacity(el, opacity) {
	if (opacity == null) opacity = 90;
	el.style.filter = "alpha(opacity:" + opacity + ")";
	el.style.KHTMLOpacity = (opacity / 100);
	el.style.MozOpacity = (opacity / 100);
	el.style.opacity = (opacity / 100);
}

function createEl(t, c){
	var x = document.createElement(t);
	x.className = c;
	x.style.display = "block";
	return(x);
}

function attachCSS() {
	var l = createEl("link");
	l.setAttribute("type", "text/css");
	l.setAttribute("rel", "stylesheet");
	l.setAttribute("href", "/tooltip.css");
	l.setAttribute("media", "screen");
	document.getElementsByTagName("head")[0].appendChild(l);
}

var timeout = null;

function fadeOpacity(el, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;
	
	el.id = 'fade_tooltip';
    
    /*if (opacStart > opacEnd) { // determine the direction for the blending, if start and end are the same nothing happens
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + el.id + "')", (timer * speed));
            timer++;
        }
    } else */
	
	if (timeout != null) clearTimeout(timeout);
	
	if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            timeout = setTimeout("changeOpac(" + i + ",'" + el.id + "')", (timer * speed));
            timer++;
        }
    }
}

function changeOpac(opacity, id) {
    if (document.getElementById(id) != null) {
		var object = document.getElementById(id).style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KHTMLOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}
} 

sfHover = function() {
	var sfEls = document.getElementById("page-bar").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function adjustIFrameSize (iframeWindow) {
  if (iframeWindow.document.height) {
    var iframeElement = parent.document.getElementById(iframeWindow.name);
    iframeElement.style.height = iframeWindow.document.height + 'px';
    iframeElement.style.width = iframeWindow.document.width + 'px';
  }
  else if (document.all) {
    var iframeElement = parent.document.all[iframeWindow.name];
    if (iframeWindow.document.compatMode &&
        iframeWindow.document.compatMode != 'BackCompat') 
    {
      iframeElement.style.height = 
        iframeWindow.document.documentElement.scrollHeight + 5 + 'px';
      iframeElement.style.width = 
        iframeWindow.document.documentElement.scrollWidth + 5 + 'px';
    }
    else {
      iframeElement.style.height = iframeWindow.document.body.scrollHeight + 5 + 'px';
      iframeElement.style.width = iframeWindow.document.body.scrollWidth + 5 + 'px';
    }
  }
}
