var tooltipTemplate = '';
var tooltipClass = '';
var tooltipContainer = false;
var useTargetPosition = false;
var moveWithMouse = true;
var tipTimer;
var docUri = document.location.toString();

function load_tooltips( strTagName, blnTargetPosition, blnMoveToMouse, strTooltipClass, strAttrName, strTooltipTemplate )
{

	var anchors = document.getElementsByTagName(strTagName);
	tooltipTemplate = strTooltipTemplate;
	tooltipClass = strTooltipClass;
	useTargetPosition =  blnTargetPosition;
	moveWithMouse = blnMoveToMouse;
	var i=0;
	var anchorTitle='';

	if( !tooltipContainer )
	{
		if( document.createElementNS ){ tooltipContainer = document.createElementNS('http://www.w3.org/1999/xhtml', 'div') } else {  tooltipContainer = document.createElement('div'); }
		tooltipContainer.setAttribute('id', 'tooltipcontainer');
		tooltipContainer.className = tooltipClass;
		tooltipContainer.style.display='none';
		tooltipContainer.style.visibility='hidden';
		tooltipContainer.style.position='absolute';
		tooltipContainer.innerHTML = tooltipTemplate;
		document.getElementsByTagName('body').item(0).appendChild(tooltipContainer);
	}

	for(i=0;i<anchors.length;i++)
	{
		anchorTitle = anchors[i].getAttribute(strAttrName);
		if( ( anchorTitle ) &&  ( anchorTitle != '') )
		{
			anchors[i].setAttribute('tooltip', anchorTitle);
			anchors[i].setAttribute(strAttrName, '');
			addEvent(anchors[i], 'mouseover', show_tooltip);
			addEvent(anchors[i], 'focus', show_tooltip);
			addEvent(anchors[i], 'mouseout', hide_tooltip);
			addEvent(anchors[i], 'blur', hide_tooltip);
		}
	}
}

function show_tooltip(e)
{
	if( tooltipContainer.style.visibility==='visible' ){ hide_tooltip(); }
	var objTarget;
	var posTop=0;
	var posLeft=0;
	if (e.target){objTarget = e.target; }else if (e.srcElement){objTarget = e.srcElement;}

	if(!objTarget.getAttribute('tooltip'))
	{
		while(objTarget.parentNode){ objTarget = objTarget.parentNode; if(objTarget.getAttribute('tooltip')){ break; } }
	}

	if( useTargetPosition ){
		posLeft = findPosX(objTarget);
		posTop = findPosY(objTarget) + objTarget.offsetHeight;
	}
	else{ posLeft = mX; posTop = mY; }

	var onUrl = objTarget.getAttribute('href');
	if( objTarget.nodeName.toLowerCase() != 'a' ){ onUrl = ''; }
	if( !onUrl ){ strUri = ''; }else{ strUri = get_fulluri( onUrl ); }
	
	var onTip = wrapText( objTarget.getAttribute('tooltip'), 36 );
	strTip = bbcode(onTip);
	strUri = wrapText( strUri, 32, '/' );
	//if( onUrl != '' ){ onUrl = onUrl; }
	template_tooltip( strTip, strUri )
	
	posTop = check_yPosition( posTop );
	posLeft = check_xPosition( posLeft );
	
	tooltipContainer.style.left = posLeft + 'px';
	tooltipContainer.style.top = posTop + 'px';

	tipTimer = setTimeout(timershow_tooltip, 350);
}

function timershow_tooltip()
{
	tooltipContainer.style.display='block';
	tooltipContainer.style.visibility='visible';
	if( !useTargetPosition )
	{
		posLeft = mX;
		posTop = mY;
		posTop = check_yPosition( posTop );
		posLeft = check_xPosition( posLeft );
		tooltipContainer.style.left = posLeft + 'px';
		tooltipContainer.style.top = posTop + 'px';
	}
	
}

function hide_tooltip()
{
	clearInterval(tipTimer);
	tooltipContainer.style.display='none';
	tooltipContainer.style.visibility='hidden';
}

function template_tooltip( text, url )
{
	var strTemp = tooltipTemplate.replace(/{text}/g, text);
	tooltipContainer.innerHTML = strTemp.replace(/{url}/g, url)
}

function check_yPosition( yTop )
{
	var pageS = getPageSize();
	var scrollP = getPageScroll();
	var yT = pageS[3] + scrollP[1];
	if( yTop + tooltipContainer.offsetHeight > yT ){ yTop = yT - tooltipContainer.offsetHeight - 20; }
	return yTop;
}

function check_xPosition( xLeft )
{
	var pageS = getPageSize();
	var scrollP = getPageScroll();
	var xT = pageS[2] + scrollP[0];
	if( xLeft + tooltipContainer.offsetHeight > xT ){ xLeft = xT - tooltipContainer.offsetHeight - 20; }
	return xLeft;
}

function get_fulluri( href )
{
	return href;
	var strReturn='';
	if(IE){  return href; }
	if( href[href.length-1] === '/' ){ href = href.substring(1,href.length-2); }
	if( href[0] === '?' )
	{
		var strTemp = docUri;
		strTemp = strTemp.split( "?" );
		if( strTemp ){ strReturn = strTemp[0] + href; }else { strReturn = docUri + href; }
	}
	else if( href.substring(0,7) === 'http://' )
	{
		strReturn = href;
	}
	else if( href[0] === '#' )
	{
		if( href.length == 1 ){ strReturn = docUri; }
		else
		{
			var strTemp = docUri;
			strTemp = strTemp.split( "#" );
			if( strTemp ){ strReturn = strTemp[0] + href; }else { strReturn = docUri + href; }
		}
	}
	else
	{
		var strTemp = docUri;
		strTemp = strTemp.split( "/" );
		var strUrl='';
		for (j=0;j<(strTemp.length-1);j++)
		{
			strUrl += strTemp[j];
		}
		strReturn = strUrl + '/' + href;
	}
	if( strReturn[strReturn.length-1] === '/' ){ strReturn = strReturn.substring(0,strReturn.length-1); }

	var strTemp = strReturn;
	var strUrl='';

	strTemp = strTemp.split( "&amp;skey=" );

	if( strTemp.length-1 )
	{
		for (j=0;j<(strTemp.length-1);j++)
		{
			strUrl += strTemp[j];
		}
		strReturn = strUrl;
	}
	else
	{
		strTemp = strReturn
		strTemp = strTemp.split( "&skey=" );
		strUrl=''
		if( strTemp.length-1 )
		{
			for (j=0;j<(strTemp.length-1);j++)
			{
				strUrl += strTemp[j];
			}
			strReturn = strUrl;
		}
		else
		{
			strTemp = strReturn
			strTemp = strTemp.split( "?skey=" );
			strUrl=''
			if( strTemp.length-1 )
			{
				for (j=0;j<(strTemp.length-1);j++)
				{
					strUrl += strTemp[j];
				}
				strReturn = strUrl;
			}
		}
	}

	strUrl = ''

	if( strTemp.length-1 )
	{
		var strTemp2 = strTemp[strTemp.length-1].split( "&amp;" );
		if( strTemp2.length-1 )
		{
			for (j=1;j<(strTemp.length);j++)
			{
				strUrl += strTemp2[j];
			}
		}
		else
		{
			var strTemp2 = strTemp[strTemp.length-1].split( "&" );
			if( strTemp2.length-1 )
			{
				for (j=1;j<(strTemp.length);j++)
				{
					strUrl += strTemp2[j];
				}
			}
		}
	}

	if( strUrl != '')
	{
		if( strReturn.indexOf('?')+1 )
		{
			strReturn += '&amp;' + strUrl;
		}
		else
		{
			strReturn += '?' + strUrl;
		}
	}

	return strReturn;
}

function bbcode( strText )
{
		//alert( 'bbcode  ' + strText );
		search = new Array(
		          /\[b\](.*?)\[\/b\]/gi,
				  /\[i\](.*?)\[\/i\]/gi,
		          /\[c\](.*?)\[\/c\]/gi,
		          /\[center\](.*?)\[\/center\]/gi,
		          /\[\img\](.*?)\[\/img\]/gi,
		          /\[br\]{1,}/gi
				  );

		replace = new Array(
		          '<strong>$1</strong>',
				  '<em>$1</em>',
		          '<center>$1</center>',
		          '<center>$1</center>',
		          '<img src="$1" alt="" />',
		          '<br />'
				  );

		for(i = 0; i < search.length; i++) {
		     strText = strText.replace(search[i],replace[i]);
		}
		for(i = 0; i < search.length; i++) {
		     strText = strText.replace(search[i],replace[i]);
		}
		return strText;
}

function wrapText( strText, intLimit, strNeedle, strReplace )
{
	if( !strNeedle ){ strNeedle = ' '; }
	if( !strReplace ){ strReplace = '<br />'; }
	if( (!strText) || (strText==='') ){ return ''; }
	var strTemp = strText.split(strNeedle);
	var t=0;
	var count=0;
	var buildStr = '';

	if( strTemp )
	{
		for( t=0;t<strTemp.length;t++ )
		{
			count += strTemp[t].length;
			if( (strTemp[t].indexOf( '<br>' )>0) || (strTemp[t].indexOf( '<br />' )>0) || (strTemp[t].indexOf( '%br%' )>0) ){ count=0; }
			if( (strTemp[t].indexOf( '<center>' )>0) || (strTemp[t].indexOf( '</center>' )>0) || (strTemp[t].indexOf( '%c%' )>0) || (strTemp[t].indexOf( '%/c%' )>0) ){ count=0; }
			if( t<strTemp.length-1 )
			{
				if( (strTemp[t+1].indexOf( '<br>' )>0) || (strTemp[t+1].indexOf( '<br />' )>0) || (strTemp[t+1].indexOf( '[br]' )>0) ){ count=0; }
				if( (strTemp[t].indexOf( '<center>' )>0) || (strTemp[t].indexOf( '</center>' )>0) || (strTemp[t].indexOf( '%c%' )>0) || (strTemp[t].indexOf( '%/c%' )>0) ){ count=0; }
			}
			if( count >= intLimit ){ buildStr += strReplace; count=0; }
			buildStr += strTemp[t] + strNeedle;
		}
		buildStr = buildStr.substring(0,buildStr.length-1);
		return buildStr;
	}else { return strText; }
}
function tooltip_document_loaded()
{
	load_tooltips( "a", false, true, "tooltip", "title", "<div class=\"text\">{text}<\/div\><p class=\"uri\">{url}<\/p\>" );
	load_tooltips( "img", false, true, "tooltip", "alt", "<div class=\"text\">{text}<\/div\><p class=\"uri\">{url}<\/p\>" );
	load_tooltips( "acronym", false, true, "tooltip", "title", "<div class=\"text\">{text}<\/div\><p class=\"uri\">{url}<\/p\>" );
}
addEvent(window, "load", tooltip_document_loaded);
