/**
 * Jact Media LLC (Milosh Boroyevich) - 2008-02-13
 * Show/hide the tooltip layer at the appropriate position
 */


function initToolTip(t) {
    if (!t.init) {
	t.onTime = 250; // in milliseconds
	t.offTime = 260; // in milliseconds

	this[t.id+'_O'] = t;
	t.init = true;
	t.eventCount = 0;
	t.offTime = (t.onTime > t.offTime) ? t.onTime+10 : t.offTime; // offTime must be greater than onTime
	// off and on methods that are to be invoked repeatedly
	t.offTimeout = function() {
	    this.eventCount--;
	    setTimeout(this.id+'_O.off()', this.offTime);
	}
	t.onTimeout = function(e,d) {
	    this.eventCount++;
	    //if (this.eventCount > 0 && this.style.visibility == 'hidden') {
	    var scrollY = (d.body.scrollTop > 0) ? d.body.scrollTop : ((d.documentElement.scrollTop > 0) ? d.documentElement.scrollTop : 0);
	    var x, w = d.width / 2;
	    var y, h = (window ? window.innerHeight : (d.defaultView ? d.defaultView.innerHeight : (d.body.clientHeight ? d.body.clientHeight : (d.body.content.height ? d.body.content.height : d.height)))) / 3 + scrollY;
	    if (e.pageY > 10 && e.pageX > 10) {
		x = e.pageX;
		y = e.pageY;
	    } else {
		if (e.clientY > 0 && e.clientX > 0) {
		    x = e.clientX;
		    y = e.clientY;
		} else if (e.offsetY > 0 && e.offsetX > 0) {
		    x = e.offsetX;
		    y = e.offsetY;
		} else if (e.screenY > 0 && e.screenX > 0) {
		    x = e.screenX;
		    y = e.screenY;
		}
		// Adjust vertically if page has been scrolled
		y += scrollY;
	    }
	    this.newX = x + 25;
	    this.newY = (y < h ? y + 20 : y - 50);
	    //}
	    setTimeout(this.id+'_O.on()', this.onTime);
	}
	t.off = function() {
	    if (--this.eventCount < 1) {
		this.eventCount = 0;
		this.style.visibility = 'hidden';
	    }
	}
	t.on = function() {
	    if (++this.eventCount > 1) { // should be 2 if the mouseout didn't already go through
		// Make sure the object doesn't show up under the mouse cursor because that will immediately generate the mouseOut event
		if (this.style.top == "" || Math.abs(this.newY - this.offsetTop) > 20) {
		    this.style.top  = this.newY+"px";
		    this.style.left = this.newX+"px";
		}
		//this.style.left = this.newX;
		//this.style.top = this.newY;
		this.eventCount = 2;
		this.style.visibility = 'visible';
	    }
	}
    }
}


function hideToolTip(id) {
    var d = this.window.document;
    var t = d.getElementById ? d.getElementById(id) : (d.all ? d.all[id] : d.layers[id]);
    initToolTip(t);
    //t.eventCount--;
    //t.off();
    t.offTimeout();
}


function showToolTip(id,e) {
    e = e ? e : (window.event ? window.event : "");
    if (e) {
	var d = this.window.document;
	var t = d.getElementById ? d.getElementById(id) : d.all ? d.all[id] : d.layers[id];
	initToolTip(t);
	t.onTimeout(e,d);
    }
}
