var tip = {
	'tooltip' : null,
	'parent' : null,
	'timer' : null
	};

//find object position
tip.getRealPosition = function(ele, dir)
{
	tip.pos = (dir=='x') ? ele.offsetLeft : ele.offsetTop;
	tip.tmp = ele.offsetParent;
	while(tip.tmp != null)
	{
		tip.pos += (dir=='x') ? tip.tmp.offsetLeft : tip.tmp.offsetTop;
		tip.tmp = tip.tmp.offsetParent;
	}
	return tip.pos;
}




//delay timer
tip.focusTimer = function(e,content)
{
	//second loop	
	if(tip.timer != null)
	{
		//clear timer
		clearInterval(tip.timer);
		tip.timer = null;
	
		//pass object to create tooltip
		tip.focusTip(e,content);
	}
	//first loop
	else
	{
		//get focussed object to pass back through timer
		tip.tmp = e.target? e.target : event.srcElement;
	
		//set interval
		tip.timer = setInterval('tip.focusTimer(tip.tmp,\''+content+'\')',400);
	}
}


//create tooltip
tip.focusTip = function(obj,content)
{

	//remove any existing tooltip
	tip.blurTip();

	//if tooltip is null
	if(tip.tooltip == null)
	{
		//get window dimensions
		if(typeof window.innerWidth!="undefined")
		{
			tip.window = {
				x : window.innerWidth,
				y : window.innerHeight
				};
		}
		else if(typeof document.documentElement.offsetWidth!="undefined")
		{
			tip.window = {
				x : document.documentElement.offsetWidth,
				y : document.documentElement.offsetHeight
				};
		}
		else 
		{
			tip.window = {
				x : document.body.offsetWidth,
				y : document.body.offsetHeight
				};
		}

		//create toolTip, detecting support for namespaced element creation, in case we're in the XML DOM
		tip.tooltip = (typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'div') : document.createElement('div');

		//add classname
		tip.tooltip.setAttribute('class','');
		tip.tooltip.className = 'tooltip';

		//get focussed object co-ordinates
		if(tip.parent == null)
		{
			tip.parent = {
				x : tip.getRealPosition(obj,'x') + obj.offsetWidth + 2,
				y : tip.getRealPosition(obj,'y') + - obj.offsetHeight
				};
		}

		// offset tooltip from object
		tip.parent.y += obj.offsetHeight;

		//apply tooltip position
		tip.tooltip.style.left = tip.parent.x + 'px';
		tip.tooltip.style.top = tip.parent.y + 'px';

		//write in title attribute 
		var  _content=document.createElement("span");
        _content.innerHTML=content;
		tip.tooltip.appendChild(_content);

		//add to document
		document.getElementsByTagName('body')[0].appendChild(tip.tooltip);

		//restrict width
		if(tip.tooltip.offsetWidth > 300)
		{
			tip.tooltip.style.width = '300px';
		}

		//get tooltip tip.extent
		tip.extent = {
				x : tip.tooltip.offsetWidth,
				y : tip.tooltip.offsetHeight
				};

//		//if tooltip exceeds window width
//		if((tip.parent.x + tip.extent.x) >= tip.window.x)
//		{
//			//shift tooltip left
//			tip.parent.x -= tip.extent.x;
//			tip.tooltip.style.left = tip.parent.x + 'px';
//		}
//		
//		//get scroll height
//		if(typeof window.pageYOffset!="undefined")
//		{
//			tip.scroll = window.pageYOffset;
//		}
//		else if(typeof document.documentElement.scrollTop!="undefined")
//		{
//			tip.scroll = document.documentElement.scrollTop;
//		}
//		else 
//		{
//			tip.scroll = document.body.scrollTop;
//		}

//		//if tooltip exceeds window height
//		if((tip.parent.y + tip.extent.y) >= (tip.window.y + tip.scroll))
//		{
//			//shift tooltip up
//			tip.parent.y -= (tip.extent.y + obj.offsetHeight + 4);
//			tip.tooltip.style.top = tip.parent.y + 'px';
//		}


	}

}


//remove tooltip
tip.blurTip = function()
{
	//if tooltip exists
	if(tip.tooltip != null)
	{
		//remove and nullify tooltip
		document.getElementsByTagName('body')[0].removeChild(tip.tooltip);
		tip.tooltip = null;
		tip.parent = null;
	}
	
	//cancel timer
	clearInterval(tip.timer);
	tip.timer = null;
}



