var xOffset = 5;
var yOffset = 5;

var shown = false;
var Params = {};

var ie = (document.all) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById) ? true : false;

var tOpacity = (ie) ? 15 : 25;

if(ie || ns4 || ns6)
{
	var myBox;
}

function ieRealBody()
{
	if(document.compatMode && document.compatMode != 'BackCompat')
	{
		return document.documentElement;
	}
	else
	{
		return document.body;
	}
}

function moveMyBox(e)
{
	if(shown)
	{
		if(ie)
		{
			var posX = event.x + ieRealBody().scrollLeft;
			var posY = event.y + ieRealBody().scrollTop;

			if(!window.opera)
			{
				var winWidth = ieRealBody().clientWidth;
				var winHeight = ieRealBody().clientHeight;

				var rightEdge = winWidth - event.clientX - xOffset;
				var bottomEdge = winHeight - event.clientY - yOffset;
			}
		}
		else
		{
			var posX = e.pageX;
			var posY = e.pageY;

			var winWidth = window.innerWidth - 20;
			var winHeight = window.innerHeight - 20;

			var rightEdge = winWidth - e.clientX - xOffset;
			var bottomEdge = winHeight - e.clientY - yOffset;
		}

		var leftEdge = (xOffset < 0) ? xOffset * -1 : -1000;

		if(myBox.offsetWidth > winWidth / 3)
		{
			myBox.style.width = (winWidth / 3) + 'px';
		}

		if(rightEdge < myBox.offsetWidth)
		{
			myBox.style.left = 5 + posX - myBox.offsetWidth + 'px';
		}
		else
		{
			if(posX < leftEdge)
			{
				myBox.style.left = '5px';
			}
			else
			{
				myBox.style.left = posX + xOffset + 'px';
			}
		}

		if(bottomEdge < myBox.offsetHeight)
		{
			myBox.style.top = posY - myBox.offsetHeight - yOffset + 'px';
		}
		else
		{
			myBox.style.top = 20 + posY + yOffset + 'px';
		}
	}
}

function AffBulle(title, text, width)
{
	shown = true;
	title = title.replace(/^\s*/, '');

	var content;
	var t = (typeof title != 'undefined' && title != '') ? true : false;

	content = '<table style="margin: ' + Params.Margin + '; padding: 0px; width: auto;">'
			+ '  <tr>'
			+ '    <td style="background: ' + Params.Color + '; border-width: ' + Params.Border.Width + '; border-style: ' + Params.Border.Style + '; -moz-border-radius: ' + Params.Border.Radius + '; padding: ' + Params.Padding + '; width: 100%; vertical-align: middle;">'
			+ '      <table style="background: ' + Params.Background + '; width: 100%; margin: 1px;">';

	if(t)
	{
		content += '        <tr>'
				+  '          <td style=\"padding: 0px 5px;\">&raquo;&nbsp;&nbsp;<span style="white-space: nowrap; font-weight: bold;">' + title + '</span></td>'
				+  '        </tr>'
				+  '        <tr>'
				+  '          <td style=\"padding: 0px; height: 1px; background-color: ' + Params.Color + '; width: 100%;\"></td>'
				+  '        </tr>';
	}

	content += '        <tr>'
			+  '          <td style=\"padding: 0px;' + ((t) ? ((ie) ? ' filter: alpha(opacity=' + (Params.Opacity - tOpacity) + '); background: ' + Params.Background + ';' : 'opacity: ' + ((Params.Opacity - tOpacity) / 100)) : '') + '\">' + text + '</td>'
			+  '        </tr>'
			+  '      </table>'
			+  '    </td>'
			+  '  </tr>'
			+  '</table>';

	if(ns4)
	{
		myBox.document.write(content);
		myBox.document.close();
		myBox.visibility = 'show';
	}
	else
	{
		myBox.innerHTML = content;
		myBox.style.visibility = 'visible';

		if(ie)
		{
			myBox.style.filter = 'alpha(opacity=' + Params.Opacity + ')';
		}
		else if(ns6)
		{
			myBox.style.opacity = Params.Opacity / 100;
		}
	}
}

function HideBulle()
{
	shown = false;

	if(ns4)
	{
		myBox.visibility = 'hide';
		myBox.top = '-1000px';
		myBox.bgcolor = '';
		myBox.width = '';
	}
	else
	{
		myBox.style.visibility = 'hidden';
		myBox.style.top = '-1000px';
		myBox.style.backgroundColor = '';
		myBox.style.width = '';
	}
}

function InitBulle(background, color, params)
{
	if(typeof params == 'object')
	{
		Params = {
			Padding: params.padding + 'px',
			Margin: params.margin + 'px',

			Background: background,
			Color: color,
			Opacity: params.opacity,

			Border: {
				Width: params.border.width + 'px',
				Style: params.border.style,
				Radius: params.border.radius + 'px'
			}
		};
	}
	else
	{
		Params = {
			Padding: '0px',
			Margin: '0px',

			Background: background,
			Color: color,
			Opacity: 90,

			Border: {
				Width: '0px',
				Style: 'solid',
				Radius: '3px'
			}
		};
	}

	if(ns4)
	{
		document.write('<layer name="box" top="0" left="0" visibility="hide"></layer>');

		window.captureEvents(Event.MOUSEMOVE);
		window.onMouseMove = moveMyBox;

		myBox = document.layers['box'];
	}
	else
	{
		document.write('<div id="box" style="position: absolute; top: 0px; left: 0px; visibility: hidden;"></div>');

		document.onmousemove = moveMyBox;

		if(ie)
		{
			myBox = document.all['box'];
		}
		else if(ns6)
		{
			myBox = document.getElementById('box');
		}
	}
}