var showMessage = function ()
{
	var ths = this, glb;
	ths.glb = glb = {};
};

showMessage.prototype.message = function (dt)
{
	var ths = this, xh, xhLdn, xhTmr;
	//
	if (!dt['texto'] && !dt['url'])
	{
		ths.error (dt,'Es obligatorio especificar "url" o "texto" para el contenido del mensaje.');
		return;
		//
	} else if (dt['texto'] && dt['url'])
	{
		ths.error (dt,'Debe especificar sólo "url" o "texto" para el contenido del mensaje, no ambos.');
		return;
		//
	} else if (dt['texto'] && !dt['url'])
	{
		ths.showLayer (dt);
		//
	} else if (!dt['texto'] && dt['url'])
	{
		xhLdn = document.createElement('div');
		xhLdn.className = 'messageBox_loading';
		xhLdn.style.position = 'fixed';
		document.body.appendChild( xhLdn );
		xhLdn.style.left = ((ths.winWH('w') - xhLdn.offsetWidth) / 2) + 'px';
		xhLdn.style.top = ((ths.winWH('h') - xhLdn.offsetHeight) / 2) + 'px';
		//
		xh = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
		xh.open('GET',dt['url'],false);
		xh.onreadystatechange = function ()
		{
			if (xh.readyState == 4 && xh.status == 200)
			{
				clearTimeout( xhTmr );
				document.body.removeChild( xhLdn );
				dt['texto'] = xh.responseText;
				ths.showLayer (dt);
			};
		};
		xh.send('');
		//
		xhTmr = setTimeout(function ()
		{
			xh.abort();
			document.body.removeChild( xhLdn );
			ths.error (dt,'El archivo con la url <strong>"' + dt['url'] + '"</strong> no fué encontrado. Favor revisar.');
		},10000);
	};
};

showMessage.prototype.showLayer = function (dt)
{
	var ths = this, ms = dt['texto'], msW = dt['ancho'], msH = dt['alto'];
	//
	ths.glb.box = document.createElement ('div');
	ths.glb.box.className = 'messageBox';
	ths.glb.box.style.position = 'fixed';
	ths.glb.box.style.top = ths.glb.box.style.left = '0';
	document.body.appendChild (ths.glb.box);
	//
	var fdo = document.createElement ('div');
	fdo.className = 'messageBox_fdo';
	fdo.style.position = 'absolute';
	fdo.style.top = fdo.style.left = '0';
	ths.glb.box.appendChild (fdo);
	//
	var cnt = document.createElement ('div');
	cnt.className = 'messageBox_cnt';
	cnt.style.width = (msW) ? msW + 'px' : '300px';
	cnt.style.height = (msH) ? msH + 'px' : 'auto';
	cnt.style.position = 'absolute';
	ths.glb.box.appendChild (cnt);
	//
	var inf = document.createElement ('div');
	inf.innerHTML = ms;
	cnt.appendChild (inf);
	//
	var cls = document.createElement ('div');
	cls.className = 'messageBox_botClose';
	cls.style.position = 'absolute';
	ths.glb.box.appendChild (cls);
	cls.onclick = function () { ths.closeMessage(ths.glb.box); };
	//
	ths.listener ('resize', 'window', repos);
	repos ();
	//
	function repos ()
	{
		cnt.style.left = ((ths.winWH('w') - cnt.offsetWidth) / 2) + 'px';
		cnt.style.top = ((ths.winWH('h') - cnt.offsetHeight) / 2) + 'px';
		cls.style.top = (cnt.offsetTop - (cls.offsetHeight / 2)) + 'px';
		cls.style.left = ((cnt.offsetLeft + cnt.offsetWidth) - (cls.offsetWidth / 2)) + 'px';
		//
		if (navigator.appName == 'Microsoft Internet Explorer')
		{
			ths.glb.box.style.width = fdo.style.width = ths.winWH('w') + 'px';
			ths.glb.box.style.height = fdo.style.height = ths.winWH('h') + 'px';
		};
	}; 
};

showMessage.prototype.closeMessage = function () { document.body.removeChild( this.glb.box ); };

showMessage.prototype.error = function (dt,tx)
{
	var ths = this;
	//
	dt['texto'] = '<h3 style="margin:0;font-family:sans-serif;font-size:18px;border-bottom:dashed 1px #CCC;padding-bottom:3px;">showMessage[ERROR]</h3>';
	dt['texto'] += '<br/><span style="font-family:sans-serif;font-size:14px;line-height:16px;">' + tx + '</span>';
	dt['ancho'] = 300;
	//
	ths.showLayer (dt);
};

showMessage.prototype.winWH = function (wh)
{
	var siz = 0;
	//
	if ( typeof (window.innerWidth) == 'number' )
	{
		siz = (wh == 'w') ? window.innerWidth : window.innerHeight;
	} else if ( document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight) )
	{
		siz = (wh == 'w') ? document.documentElement.clientWidth : document.documentElement.clientHeight;
	} else if ( document.body && (document.body.clientWidth || document.body.clientHeight) )
	{
		siz = (wh == 'w') ? document.body.clientWidth : document.body.clientHeight;
	};
	//
	return siz;
};

showMessage.prototype.listener = function (evnt,elem,func)
{
	elem = window[elem];
	//
	if (elem.addEventListener)
	{
		elem.addEventListener (evnt, func, false);
	} else if (elem.attachEvent)
	{
		return elem.attachEvent ('on' + evnt, func);
	};
};

