//function returning bounds of the element
var getBounds=function(p){var left=p.offsetLeft,top=p.offsetTop,w=p.offsetWidth;h=p.offsetHeight;while(p=p.offsetParent){left+=p.offsetLeft;top+=p.offsetTop;}return{left:left,top:top,width:w,height:h};};

/* x,y    - window x and y
*  sx,sy  - screen x and y
*  px,py  - page x and y (with scrolling) */
var getSize=function(){var x,y,sx,sy,px,py;if(self.innerHeight){x=self.innerWidth;y=self.innerHeight;}else if(document.documentElement&&document.documentElement.clientHeight){x=document.documentElement.clientWidth;y=document.documentElement.clientHeight;}else if(document.body){x=document.body.clientWidth;y=document.body.clientHeight;}if(self.pageYOffset){sx=self.pageXOffset;sy=self.pageYOffset;}else if(document.documentElement&&document.documentElement.scrollTop){sx=document.documentElement.scrollLeft;sy=document.documentElement.scrollTop;}else if(document.body){sx=document.body.scrollLeft;sy=document.body.scrollTop;}var test1=document.body.scrollHeight;var test2=document.body.offsetHeight;if(test1>test2){px=document.body.scrollWidth;py=document.body.scrollHeight;}else{px=document.body.offsetWidth;py=document.body.offsetHeight;}return {x:x,y:y,sx:sx,sy:sy,px:px,py:py};};

//crossbrowser mouse coordinates
//var getCoords=function(e){var posx=0;var posy=0;if(!e)var e=window.event;if(e.pageX||e.pageY){posx=e.pageX;posy=e.pageY;}else if(e.clientX||e.clientY){posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;posy=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;}return {x:posx,y:posy};};
var getCoords=function(e){
	var posx=0;
	var posy=0;
	if(!e)
		var e=window.event;
	if((e.pageX||e.pageY)){
		posx=e.pageX;
		posy=e.pageY;
	}else 
		if(e.clientX||e.clientY){
			posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
			posy=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;
		}
	return {x:posx,y:posy};
};

function getToolTip(e, obj, createShim){

   if (typeof arguments.callee.tooltip == 'undefined') {

   		var t = document.createElement('div');
   		var title  = t.appendChild(document.createElement('div'));
   		var content = t.appendChild(document.createElement('div'));
   		var closing = t.appendChild(document.createElement('div'));
   		t.id = 'myToolTip';
   		title.id = 'myToolTipTitle';
   		content.id = 'myToolTipContent';
   		closing.id = 'myToolTipCross';
   		closing.innerHTML = '&times;';
   		closing.onclick = function(e){ this.parentNode.style.display = 'none'; var shim = document.getElementById('myShim'); if(shim) shim.style.display='none'; toolTipFlag = 0; if(window.event) window.event.cancelBubble = true; if (e) e.stopPropagation(); };
   		t.onclick = function(e){ this.style.display = 'block'; if (window.event) window.event.cancelBubble = true; if (e) e.stopPropagation(); };
   		
   		t.setTitle = function(ttl){ this.firstChild.innerHTML = ttl; };
   		t.setContent = function(cnt) { this.childNodes[1].innerHTML = cnt; };
   		t.style.position = 'absolute';
   		t.style.zIndex = '9999';
   		
   		arguments.callee.tooltip = document.body.appendChild(t);
   		
   }

   var mc = getBounds(obj);
   with(arguments.callee.tooltip.style) {
     display = 'block';
     left = mc.left + 'px';
     top = (mc.top + mc.height + 10) + 'px';
     
   }
   
   if (createShim) {
     //we need to create shim only once and hide it, then position it when required
     if (typeof arguments.callee.shim == 'undefined') {
       var shim = document.createElement('iframe');
       shim.id = 'myShim';
       shim.src = "javascript:'<html></html>'";
       with (shim.style) {
         position = 'absolute';
         zIndex = '1';
         background = 'red';
       }
       arguments.callee.shim = document.body.appendChild(shim);
     }
     
     arguments.callee.shim.style.display = 'block';
     //now position it right under the tooltip
     /*with(arguments.callee.shim.style) {
       display = 'block';
       top = arguments.callee.tooltip.style.top;
       left = arguments.callee.tooltip.style.left;
     }*/
   }
   
   return arguments.callee.tooltip;
}

function showTooltip(e, obj, msg){
	var createShim = false; 
	var tooltip = getToolTip(e, obj, createShim);
	
	tooltip.setTitle('Description');
	tooltip.setContent(msg);

	// if BROWSER = IE
	if('\v'=='v'){
		var mc = getCoords(e);
		tooltip.style.display = 'block';
		tooltip.style.left = mc.x + 10 + 'px';
		tooltip.style.top = mc.y + 20 + 'px';		
	}
	toolTipFlag = 1
	
	if(window.event) window.event.cancelBubble = true
	if(e)if(e.stopPropagation) e.stopPropagation();
	
	return false;
}

