//DETECCION DE NAVEGADOR
var ns4=(document.layers)?true:false;
var dom=(ns4)?false:((document.getElementById)?true:false);
var op=(ns4)?false:((window.opera)?true:false);
var ie=(ns4||op)?false:((document.all)?true:false);
var ffx=(ns4||op)?false:((navigator.userAgent.indexOf('Firefox')>-1)?true:false);
var ns=(ns4||op)?false:((navigator.userAgent.indexOf('Netscape')>-1)?true:false);
var moz=(ns4||op)?false:((navigator.appName.indexOf('Netscape')>-1)?((ffx||ns)?false:true):false);


var hayPublicidadActiva = false;
var idsPublicidad = new Array();

var minOffsetY = 30;


//PARA INSERTAR PUBLICIDAD
//	id = identificador del div (obligatorio)
//	url = destino al hacer click (obligatorio)
//	imagen = url de la imagen a mostrar (obligatorio)
//	ancho = ancho del div
//	alto = alto del div
//	anchoImg = ancho de la imagen 
//	altoImg = alto de la imagen
//	borde = tamaño del borde que se deja alrededor de la imagen (opcional, por defecto 1)
//  css = clase del div (opcional)
//  metodo = método para incluir la publicidad (1: con script o 2: con imagen)
//	segundos = cuanto tiempo se mantiene la publicidad visible (opcional, por defecto 0 = siempre visible)
//
//		Se puede definir un estilo para el título usando la clase css 'titulo_publi'
function publicidad(id,url,imagen,ancho,alto,anchoImg,altoImg,borde,css,metodo,segundos)
{
	var s='';

	var wbot=16;
	var hbot=16;
	var tam=10;
	var tmp;

	if (ie)	tam -= 2;

	//Almacenar los ids
	idsPublicidad[idsPublicidad.length] = id;

	if (!ns4)
	{
		if (readCookie(id)!=null) return;
		if ((id==null)||(url==null)||(imagen==null)) return;
		if (css==null) css=id;

		if (borde==null) borde=1;
		if (segundos<1) segundos=0;

		s = "<div id='" + id + "' style='position:absolute;z-index:1000000;";
		if (ancho!="") s += "width:" + (ancho+borde*2) + "px;";
		


		if (metodo == 1)
		{
			s += "'>" + imagen;
		}
		else
		{
			s += "' class='" + css + "'>";
			if (url!='') s += "<a href='" + url + "' onclick='mostrar(\"" + id + "\",false);' target='_blank'><div>";

			
			s += "<div class='titulo_publi' style='text-align:center;padding:2px;padding-bottom:" + (2+borde) + "px;vertical-align:baseline;'>";

			if (ie) s += "<span style='float:right;display:block;width:"+wbot+"px;height:"+hbot+"px;font-size:"+tam+"px;text-align:center;padding-top:"+((hbot-2-tam)/2-1)+"px;background-color:silver;color:black;border-left:2px solid #F5F4EB;border-top:2px solid #F5F4EB;border-right:2px solid #A6A498;border-bottom:2px solid #A6A498;'>X</span>";
			else s += "<div style='float:right;'><input type='button' value='X' style='font-family:Verdana,sans-serif;font-size:"+tam+"px;font-weight:bold;width:"+wbot+"px;height:"+hbot+"px;background-color:silver;' onfocus='blur();'></div>";

			s += "Publicidad</div><div style='clear:right;padding:" + borde + "px;"
			if (ancho!="") s += "width:" + ancho + "px;";
			if (alto!="") s += "height:" + alto + "px;";
			s += "'>";
			s += "<img src='" + imagen + "' style='border:0px solid transparent;";

			if ((ancho!="")&&(anchoImg!=""))
			{
				tmp = (ancho-anchoImg);
				tmp = ((tmp%2==1)?tmp-1:tmp)/2;
				s += "margin-left:" + tmp + "px;";
			}
			if ((alto!="") && (altoImg!=""))
			{
					tmp = (alto-altoImg);
					tmp = ((tmp%2==1)?tmp-1:tmp)/2;

					s += "margin-top:" + tmp + "px;";
			}
			s += "'>";
			s += "</div>";

			if (url!='') s += "</div></a>";

		}

		s += "</div>";
		document.write(s);

		if (segundos>0)
		{
			setTimeout('mostrar("' + id + '",false);',segundos*1000);
		}

		//createCookie(id,'1',0);
	}
	else
	{
		if (ancho!="") s += "width=" + ancho + "px,";
		if (alto!="") s += "height=" + alto + "px,";
		s +='dependent';
		var wnd=window.open('',id,s);
		if (metodo == 1)
		{
			wnd.document.write(imagen);
		}
		else
		{
			wnd.document.write('<a href="'+url+'"><img src="' + imagen + '" border="0"></a>');
		}
		//wnd.focus();
	}

	hayPublicidadActiva = true;
}



function mostrar(id,ver)
{
	var obj=(dom)?document.getElementById(id):((ie)?document.all[id]:null);

	if (!ns4)
	{
		if ((ver==null)||(ver)) obj.style.visibility='visible';
		else 
		{
			obj.style.visibility='hidden';
			createCookie(id,'1',0);
		}
	}
}


function findPos(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curtop += obj.offsetTop;
		}
	}
	return curtop;
}


var procesosVerificacion = 5 * idsPublicidad.length;

function actualizarPosicion()
{
	for (i=0; i<idsPublicidad.length ; i++)
	{
		obj=document.getElementById(idsPublicidad[i]);

		if (obj)
		{
			//x = document.body.scrollLeft;
			y = document.body.scrollTop;

			yObj = findPos(obj);

			if (y < minOffsetY)
			{
				if (yObj > minOffsetY)
				{
					obj.style.top = minOffsetY;
				}
			}
			else if (y != yObj)
			{
				obj.style.top = y;
			}
			else
			{
				procesosVerificacion -= 1;
			}
		}
	}

	if (procesosVerificacion)
	{
		setTimeout("actualizarPosicion();",200);
	}
}

var evntOnScroll = window.onscroll;
window.onscroll= function(e)
{
	var i,obj;
	var x,y;
	var yObj;

	if (!e) e=window.event;

	if(evntOnScroll)
	{
		evntOnScroll(e);
	}

	if (hayPublicidadActiva)
	{
		procesosVerificacion = 5 * idsPublicidad.length;
		actualizarPosicion();
	}
}


//GESTION DE COOKIES
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}