/***********************************************
* Freejavascriptkit.com
* Visit http://www.freejavascriptkit.com for more free Javascripts source code
***********************************************/

var offsetxpoint=10 //Customize x offset of tooltip
var offsetypoint=10 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enablestip=false
var lastcaller
if (ie||ns6)
	var stipobj=document.all? document.all["statictooltip"] : document.getElementById? document.getElementById("statictooltip") : ""

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function showstatictip(thetext, thecolor, thewidth){
	if (ns6||ie){
		if (typeof thewidth!="undefined") 
			stipobj.style.width=thewidth+"px"
		stipobj.style.backgroundColor=document.body.bgColor
		stipobj.innerHTML=thetext
		enablestip=true
		return false
	}
}

function positionstatictip(e){
	if (enablestip){
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<stipobj.offsetWidth)
			//move the horizontal position of the menu to the left by it's width
			stipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-stipobj.offsetWidth-5+"px" : window.pageXOffset+e.clientX-stipobj.offsetWidth-5+"px"
		else if (curX<leftedge)
			stipobj.style.left="5px"
		else
			//position the horizontal position of the menu where the mouse is positioned
			stipobj.style.left=curX+offsetxpoint+"px"

		//same concept with the vertical position
		if (bottomedge<stipobj.offsetHeight)
			stipobj.style.top=ie? ietruebody().scrollTop+event.clientY-stipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-stipobj.offsetHeight-offsetypoint+"px"
		else
			stipobj.style.top=curY+offsetypoint+"px"
		stipobj.style.visibility="visible"
	}
}

function updatestatictippos() {
	var bottomedge=ie&&!window.opera? ietruebody().clientHeight-lastevent.clientY-offsetypoint : window.innerHeight-lastevent.clientY-offsetypoint-20

	var left = fetch_object_posleft(stipobj);
	var right = left + stipobj.offsetWidth;
	var top = fetch_object_postop(stipobj);
	var bottom = top + stipobj.offsetHeight;

	/*stipobj.innerHTML="top: " + fetch_object_postop(lastcaller) + "<br>offsetHeight: " + stipobj.offsetHeight;*/

	if(bottom > bottomedge) {
		stipobj.style.top = fetch_object_postop(lastcaller) - stipobj.offsetHeight + "px";
	}
}

function hidestatictip(){
	if (ns6||ie){
		enablestip=false
		stipobj.style.visibility="hidden"
		stipobj.style.left="-1000px"
		stipobj.style.backgroundColor=''
		stipobj.style.width=''
	}
}

function userstatictip(userid, caller){
	var left;
	var rightedge=ie&&!window.opera? ietruebody().clientWidth-offsetxpoint : window.innerWidth-offsetxpoint-20

	thewidth=210;
	if (ns6||ie){
		var rightpos = fetch_object_posleft(caller)+thewidth;

		lastcaller = caller;

		if (typeof thewidth!="undefined") 
			stipobj.style.width=thewidth+"px"
/*		stipobj.style.backgroundColor='black'*/
		stipobj.innerHTML="Loading userid: " + userid;
/*		stipobj.innerHTML="Loading userid: " + userid + "<br>rightedge: " + rightedge + "<br>posleft: " + fetch_object_posleft(caller) + "<br>thewidth: " + thewidth + "<br>rightpos: " + rightpos;*/

		if (rightedge < rightpos)
			left = rightedge-thewidth-10;
		else
			left = fetch_object_posleft(caller);
			
		stipobj.style.left = left + "px";
		stipobj.style.top = fetch_object_postop(caller) + caller.offsetHeight + "px";
		stipobj.style.visibility="visible"
		enablestip=true
		new Ajax.Updater('statictooltip', '/usertip.php?userid=' + userid, {asynchronous:true, evalScripts:true});
		return false
	}
}

function changedate(groupid, sessionid, day, month, year, caller) {
	var left;
	var rightedge=ie&&!window.opera? ietruebody().clientWidth-offsetxpoint : window.innerWidth-offsetxpoint-20

	thewidth=310;
	if (ns6||ie){
		var rightpos = fetch_object_posleft(caller)+thewidth;

		lastcaller = caller;

		if (typeof thewidth!="undefined") 
			stipobj.style.width=thewidth+"px"
		stipobj.innerHTML="Loading calendar ";

		if (rightedge < rightpos)
			left = rightedge-thewidth-10;
		else
			left = fetch_object_posleft(caller);
			
		stipobj.style.left = left + "px";
		stipobj.style.top = fetch_object_postop(caller) + caller.offsetHeight + "px";
		stipobj.style.visibility="visible"
		enablestip=true
		new Ajax.Updater('statictooltip', 'datetip.php?groupid=' + groupid + '&sessionid=' + sessionid + '&day=' + day + '&month=' + month + '&year=' + year, {asynchronous:true, evalScripts:true});
		return false
	}
}

function checktip(e) {
	if(enablestip){
		if(cursoroverelement(e,stipobj) || cursoroverelement(e,lastcaller)) {
			var x = 1
		} else {
			hidestatictip()
		}
	}
}

function cursoroverelement(e,element) {
	var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
	var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

	var left = fetch_object_posleft(element);
	var right = left + element.offsetWidth;
	var top = fetch_object_postop(element);
	var bottom = top + element.offsetHeight;

	/*stipobj.innerHTML="curX: " + curX + "<br>curY: " + curY + "<br>left: " + left + "<br>right: " + right + "<br>top: " + top + "<br>bottom: " + bottom;*/

	return(curX < right+2 && curX > left-2 && curY < bottom+2 && curY > top-2);
}

// #############################################################################
// get left position of elm
function fetch_object_posleft(elm)
{
        var left = elm.offsetLeft;
        while((elm = elm.offsetParent) != null)
        {
                left += elm.offsetLeft;
        }
        return left;
}

// #############################################################################
// get top position of elm
function fetch_object_postop(elm)
{
        var top = elm.offsetTop;
        while((elm = elm.offsetParent) != null)
        {
                top += elm.offsetTop;
        }
        return top;
}

