//Copyright 2007 Envivomedia
// Programmer: Ismael Canales Luis


var link_decoration_type=0;

function debug(a)
{
	//document.write("<span style=\"color:red;\">"+a+"</span><br>");
	//	alert(a);
}

function getMousePosition(e){
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)	{
		posx = e.pageX;
		posy = e.pageY;
	}else if (e.clientX || e.clientY){
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return [posx,posy];
}



var idsx=0;

function envivoads_run(e)
{
	if(e.toElement){
		o=e.toElement;
	}else{
		o=this;
	}

	iframe=document.createElement('iframe');
	iframe.setAttribute('src',o.banner.url);
	div_w=o.banner.size[0];
	div_h=o.banner.size[1];

	iframe.width=div_w;
	iframe.height=div_h;
	iframe.scrolling="no";
	iframe.frameborder=0;
	iframe.style.border="0px;";
	iframe.border=0;
	iframe.frameBorder="0";
	iframe.allowTransparency="1";

	div=document.createElement('div')
	id_div='promotext_'+idsx;
	iframe.setAttribute('name',id_div);

	div.setAttribute('id',id_div);

	if (div.addEventListener){
		div.addEventListener('mouseout', envivoads_drop, false); 
	} else if (a.attachEvent){
		div.attachEvent('onmouseout', envivoads_drop);
	}
	div.style.position="absolute"

	div.appendChild(iframe)

	if(e.toElement){
		document.body.appendChild(div)
	}else{
		document.documentElement.appendChild(div)
	}


	pos=getMousePosition(e);

	l= (pos[0]-(div_w/2) );
	t= (pos[1]-(div_h/2) );

	if(l < 0 ) {
		l=1;
	}

	if(t < 0 ) {
		t=1;
	}

	div.style.top=t + "px";
	div.style.left=l + "px";

	var opac=0;

	o.style.visibility="visible";

}


function envivoads_drop(e)
{
	var o;
	if(e.fromElement){
		o=e.fromElement;
	}else{
		o=this;
	}

	function chapa(){
		o.style.visibility="hidden";
		o.style.display="none";
		try{
			o.parentNode.removeNode(o)
		}catch(e){
		
		}
	}
	setTimeout(chapa,1000);
}

rlevel=0;
var counter=0;
var nodos=0;

	
function trim(caden)
{
	cadena=caden
	for(i=0; i<cadena.length; )
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1)
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	
	return cadena;
}


function notify(id_banner,id_setting){	
		this.url='http://www.adshines.es/?jump=stat.notify'
		function send(tag)
		{
			if( id_banner && id_setting && tag ){
				document.getElementById('stat').src=this.url + '&tag='+encode(tag)+'&b='+id_banner+'&s='+id_setting;
				document.getElementById('stat').src='javascript: void(0);';
			}
		}
	}


function envivoads_replace_node(nodeRoot,word,banners){
	counter++;
	if(counter<=10000) {
		var cc=0
		while(e=nodeRoot.childNodes[cc] ){
			nodos++;

			if( e.nodeName=='#text' && ( nodeRoot.nodeName=='P' || nodeRoot.nodeName=='DIV' || nodeRoot.nodeName == 'TD' || nodeRoot.nodeName == 'SPAN' || nodeRoot.nodeName == 'B' || nodeRoot.nodeName == '#text'  || nodeRoot.nodeName=='FONT' ) ){
				text=e.nodeValue
				dt=text.split(word);
				if(dt.length>1){
					words=text.match(word);
					var i=0;
					inserted=0;
					while(dt && i<dt.length){
						if(i)
							txt=document.createTextNode(" "+dt[i]);
						else
							txt=document.createTextNode(dt[i]);
						cc++;
						if(i< (dt.length-1) ){
							a=document.createElement('a');
							anchor=words.shift();
							
							if(cc<nodeRoot.childNodes.length){
								nodeRoot.insertBefore(txt,nodeRoot.childNodes[cc]);
							}else{
								nodeRoot.appendChild(txt);
							}

							for(ban in banners){
								kyw = new RegExp(ban,"g")
								if(anchor.match(kyw)){
									a.banner=banners[ban];
									break;
								}
						
							}
							
							if( link_decoration_type == 1)
								a.appendChild(document.createTextNode(trim(anchor)+'*'));
							else
								a.appendChild(document.createTextNode(trim(anchor)));							    
							
								
							if( a.banner.presentation_type == 2 && a.banner.direct_link ){
							
							
								a.setAttribute('href', a.banner.direct_link );							    
								a.setAttribute('target','_blank');
								
								/* Lo desabilito por que hay que meter el id_setting y el id_banner para que rule el notify
								
								function pica_view()
								{
									notify = new notify()
									notify.send('click');
									return true;
								}
				
								
   							    if (a.addEventListener){
									a.addEventListener('click', pica_view, false); 
							    } else if (a.attachEvent){
									a.attachEvent('onclick', pica_view);
							    }
								
								*/
							
							}else{
    							    if (a.addEventListener){
								a.addEventListener('mouseover', envivoads_run, false); 
							    } else if (a.attachEvent){
								a.attachEvent('onmouseover', envivoads_run);
							    }
								a.setAttribute('href','javascript:void(0)');
							}
							
		
							
							if(link_decoration_type == 2 ){
								a.style.textDecoration='underline';
	//							a.style.borderLeft='5px double';
	//							a.style.borderRight='5px double';
							}else	if(link_decoration_type == 3){
								a.style.textDecoration='overline';							
	//							a.style.borderLeft='1px solid';
	//							a.style.borderRight='1px solid';
							}
							
							
							
							//a.setAttribute('keyword',anchor);
							cc++;

							a.keyword=anchor

							nodeRoot.childNodes[cc-1].nodeValue += ' ';

							if(cc<nodeRoot.childNodes.length){
								nodeRoot.insertBefore(a,nodeRoot.childNodes[cc]);
								dt[i]=" "+dt[i]	
							}else{
								nodeRoot.appendChild(a);
							}

								
						}else{

							if(cc<nodeRoot.childNodes.length){
								nodeRoot.insertBefore(txt,nodeRoot.childNodes[cc]);
							}else{
								nodeRoot.appendChild(txt);
							}
							nodeRoot.removeChild(e);

							cc--;
						}
						i++;
					}
				}
			}else{
				envivoads_replace_node(e,word,banners)
			}
			cc++;
		}
	}else{
		return;
	}
	counter--;
}

function envivoads_replace_document(parameters){
	keyword=[]
	for( a in parameters.banners ){
		keyword[keyword.length]=' '+a+'[ (s )$]';
	}
	reg=keyword.join("|");
	keyword = new RegExp(reg,"g")
	if(keyword ){
		root=document.body.childNodes;
		envivoads_replace_node(document.body,keyword,parameters.banners);
	}
}

function __init__(parameters){
	link_decoration_type=parameters.link_decoration_type
	envivoads_replace_document(parameters);
}

__init__(

	

);





