var the_frame=self;
var over_div=self;
var posi_X = 0;
var posi_Y = 0;

//--------------------------------------------------------------------------------------------------
//Decide browser version
var isMac = (navigator.userAgent.indexOf("Mac") != -1);
var brOP = (navigator.userAgent.toLowerCase().indexOf('opera 7.') > -1);
var brNS4 = (navigator.appName=='Netscape' && parseInt(navigator.appVersion) == 4);
var brNS6 = (document.getElementById) ? true : false;
var brIE4 = (document.all) ? true : false;
var brIE5 = false; 
if (brIE4) {	// Special Microsoft check version
	var versNum=parseFloat(navigator.userAgent.match(/MSIE (\d\.\d+)\.*/i)[1]);
	if (versNum >= 5) { brIE5=true; brNS6=false; }
	if (brNS6) brIE4 = false;
}

//The document root (string value)
var docRoot = 'document.body';
if (document.compatMode && document.compatMode == 'CSS1Compat') {
	docRoot= ((brIE4 && !brOP) ? 'document.documentElement' : docRoot);
}

//--------------------------------------------------------------------------------------------------
//Capture events (mouse...) and set some global variables
var checkMouseCapture = true;
function _mouseMove(e) {
	var e = (e) ? e : event;
	if (e.pageX) {
		posi_X = e.pageX;
		posi_Y = e.pageY;
	} else if (e.clientX) {
		posi_X = eval('e.clientX+the_frame.'+docRoot+'.scrollLeft');
		posi_Y = eval('e.clientY+the_frame.'+docRoot+'.scrollTop');
	}
}

function _mouseCapture() {
	capExtent = document;
	var fN;
	var mseHandler = _mouseMove;
	var re = /function[ ]+(\w+)\(/;
	if (document.onmousemove || (!brIE4 && window.onmousemove)) {
		if (window.onmousemove) capExtent = window;
		fN = capExtent.onmousemove.toString().match(re);
		if (fN[1] == 'anonymous' || fN[1] == '_mouseMove') {
			checkMouseCapture = false;
			return;
		}
		var str = fN[1]+'(e); ' + '_mouseMove(e); ';
		mseHandler = new Function('e', str);
	}
	capExtent.onmousemove = mseHandler;
	if (brNS4) capExtent.captureEvents(Event.MOUSEMOVE);
}
if ((brNS4 || brNS6 || brIE4))  _mouseCapture();

//--------------------------------------------------------------------------------------------------
//Macromedia functions to manage the Layers
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

//Set the layer where to write into
function _setLayer(nL) {
	if (brNS4) { over_div = the_frame.document.layers[nL]; }
	else if (document.all) { over_div = the_frame.document.all[nL]; }
		else if (document.getElementById) { over_div = the_frame.document.getElementById(nL); }
}

//Move the layer
function _moveLayerTo(obj, xL, yL) {
	var theObj=(brNS4 ? obj : obj.style);
	theObj.left = xL + (!brNS4 ? 'px' : 0);
	theObj.top = yL + (!brNS4 ? 'px' : 0);
}

//Write the content in the layer
function _layerWrite(txt) {
	txt += "\n";
	if (brNS4) {
		var lyr = over_div.document
		lyr.write(txt)
		lyr.close()
	} else if (typeof over_div.innerHTML != 'undefined') {
		if (brIE5 && isMac) over_div.innerHTML = '';
		over_div.innerHTML = txt;
	} else {
		range = the_frame.document.createRange();
		range.setStartAfter(over);
		domfrag = range.createContextualFragment(txt);
		while (over_div.hasChildNodes()) { over_div.removeChild(over_div.lastChild); }
		over_div.appendChild(domfrag);
	}
}

//--------------------------------------------------------------------------------------------------
//Main function to write in the layer (call it in the html)
function pubLayerWrite(fr,la,px,py,txt) {
	the_frame=fr;
	_setLayer(la);
	if (checkMouseCapture) _mouseCapture();
	_moveLayerTo(over_div, px, py);
	_layerWrite(txt);
}
