baseURL = "http://ctx.inin.ro/";

function getPos(theObj){
	  x = y = 0;
	  h = theObj.offsetHeight;
	  w = theObj.offsetWidth;
	  while(theObj){
		x += theObj.offsetLeft;
		y += theObj.offsetTop;
		theObj = theObj.offsetParent;
	  }
	  return {h:h,w:w,x:x,y:y}
}

var float;
var iShowTimeout,iHideTimeout;
var oB={oBject:'',word:'' }



function Underline(cpg)
{
	var oBanner=cpg[0].banner;
	var oWords=cpg[0].words;	
	
	var	load_script=false;
	var wordcount=0, lastClick = 0, lastOpen=0;
	
	
	function  Top()
	{
		return '<table cellpadding="0" cellspacing="0" border="0" style="width: '+ (oBanner.iBWidth+20) +'px;"><tr>'+
			'<td style="width: '+ (oBanner.iBWidth+20) +'px; border: solid 1px #DBDBE3; background-color: #E6E8E8; padding:1px;">'+
			'<table cellpadding="0" cellspacing="0" border="0" style="width:100%;">'+
			'<tr>'+
			' <td style="border-top: solid 1px #DADFE4; background-color:#E2E2E9; padding:1px 2px 2px 2px;">'+
			'  <table cellpadding="0" cellspacing="0" border="0" width="100%">'+
			'  <tr>'+
			'   <td width="99%" style="font: bold 10px tahoma; color: gray;">Publicitate</td>'+
			'   <td style="font: bold 10px verdana; background-color: #BEBEBE; color: white; '+
									'cursor:pointer; padding:0px 3px 2px 4px; border: solid 1px white;"'+
			'    onClick="CloseBanner(); return false;" onMouseOver="this.style.backgroundColor = \'#171A9D\';" '+
			'	 onMouseOut="this.style.backgroundColor = \'#BEBEBE\';" title="Close">x</td>'+
			'  </tr>'+
			'  </table>'+
			' </td>'+
			'</tr>'+
			
			'<tr>'+
			' <td style="border: solid 1px #EBE9E7; border-top-color: #C6C7D5; border-bottom: solid 1px #A3A5BA; '+
						'padding: 10px 4px 10px 4px; background-color: #FFFFFF;" '+
			'  onMouseOver="this.style.backgroundColor = \'#FFFFFF\';" onmouseout="this.style.backgroundColor = \'#FFFFFF\';">';
	}
	
	function Bottom()
	{
		 return ' </td>'+
			'</tr>'+
			'</table>'+
			'</td></tr></table>';
	}
	
	function Text()
	{
		return Top() +
		
		'  <table cellpadding="0" cellspacing="0" border="0" width="100%" style="cursor:pointer;" onClick="ClickBanner(\'ClickOnCreative\');">'+
		'  <tr>'+
		'   <td style="font: bold 14px tahoma; color: #0F62BD; text-decoration: underline; text-align: center; padding:2px 5px 2px 5px;">'+
		oBanner.title + '</td>'+
		'  </tr>'+
		'  <tr>'+
		'   <td style="font: normal 11px verdana; padding:5px 10px 0px 10px; color: #000000;">'+ oBanner.text + '</td>'+
		'  </tr>'+
		'  <tr>'+
		'   <td style="padding:5px 3px 0px 10px; text-align:right;"><a href="javascript:;" '+
		'    style="color: #BF4343; font: normal 11px arial; text-decoration: underline;" '+
		'   onClick="ClickBanner(\'ClickOnCreativeSiteName\'); return false;" '+
		'	onMouseOver="this.style.color = \'#EF4743\';" onMouseOut="this.style.color = \'#BF4343\';">'+ oBanner.site_name +'</a></td>'+
		'  </tr>'+
		'  </table>'+oBanner.picture_traking+
		 
		Bottom();
	}
	
	function ImageAndText()
	{
		return Top() +
		
		'  <table cellpadding="0" cellspacing="0" border="0" width="100%" style="cursor:pointer;" onClick="ClickBanner(\'ClickOnCreative\');">'+
		'  <tr>'+
		'   <td colspan="2" style="font: bold 14px tahoma; color: #0F62BD; text-decoration: underline; text-align: center; padding:2px 5px 2px 5px;">'+
		oBanner.title + '</td>'+
		'  </tr>'+
		
			'<tr>'+
			'  <td style="padding:5px 2px 0px 2px; width:10px;"><img src="'+ oBanner.src +'" alt="" border="0"></td>'+
			( (oBanner.text!="") ?('  <td style="font: normal 11px verdana; padding:5px 10px 0px 10px; color: #000000;">'+ oBanner.text + '</td>'):('<td></td>') )+
			'</tr>'+
		
		'  <tr>'+
		'   <td colspan="2" style="padding:5px 3px 1px 10px; text-align:right;"><a href="javascript:;" '+
		'    style="color: #BF4343; font: normal 11px arial; text-decoration: underline;" '+
		'   onClick="ClickBanner(\'ClickOnCreativeSiteName\'); return false;" '+
		'	onMouseOver="this.style.color = \'#EF4743\';" onMouseOut="this.style.color = \'#BF4343\';">'+ oBanner.site_name +'</a></td>'+
		'  </tr>'+
		'  </table>'+oBanner.picture_traking+
		 
		Bottom();
	}
	
	function Flash()
	{
		s=baseURL+"click.php?"+escape(oBanner.url+'&suref=NoText_ClickOnCreative');
		var string = Top() +
		
		'<table cellpadding="0" cellspacing="0" border="0" width="100%">'+
		'<tr>'+
		' <td style="text-align: center;">'+

		' <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+ oBanner.w +'" height="'+ oBanner.h +'" align="middle">'+
		' <param name="movie" value="'+ oBanner.src +'?clickTag='+s+'" />'+
		' <param name="allowScriptAccess" value="always" />'+
		' <param name="quality" value="high" />'+
		' <param name="salign" value="lt" />'+
		' <param name="bgcolor" value="#ffffff" />'+
		
		' <embed type="application/x-shockwave-flash" width="'+ oBanner.w +'" height="'+ oBanner.h +'" align="middle" '+
		' flashVars="clickTag='+s+'"'+
		' src="'+ oBanner.src +'" '+
		' allowScriptAccess="always" ' + (window.opera ? 'wmode="transparent"' : '') + ' quality="high" salign="lt" bgcolor="#ffffff"/>'+
		
		'  </object>'+

		' </td>'+
		'</tr>'+
		'</table>'+oBanner.picture_traking+
		Bottom();
		
		return string;
	}
	
	function FlashNoBorder()
	{
		s=baseURL+"click.php?"+escape(oBanner.url+'&suref=NoText_ClickOnCreative');
		
		return '<table cellpadding="0" cellspacing="0" border="0" width="'+ oBanner.w +'">'+
		'<tr>'+
		' <td style="text-align: center;">'+

		'  <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+ oBanner.w +'" height="'+ oBanner.h +'" align="middle">'+
		
		'	<param name="movie" value="'+ oBanner.src +'?clickTag='+s+'" />'+
		'	<param name="allowScriptAccess" value="always" />'+
		'	<param name="quality" value="high" />'+
		'	<param name="salign" value="lt" />'+
		'	<param name="bgcolor" value="#ffffff" />'+
		'   <param name="wmode" value="transparent" /> '+
		'	<embed type="application/x-shockwave-flash" width="'+ oBanner.w +'" height="'+ oBanner.h +'" align="middle" '+
		'    flashVars="clickTag='+s+'"'+
		'	 src="'+ oBanner.src +'" '+
		'	 allowScriptAccess="always" wmode="transparent" quality="high" salign="lt" bgcolor="#ffffff"/>'+
		
		'  </object>'+

		' </td>'+
		'</tr>'+
		'</table>'+oBanner.picture_traking	;
	}

	this.BannerShow = function()
	{
		var iTNow = (new Date()).getTime();
		if (iTNow - lastOpen > 2000) 
		{
			lastOpen = iTNow;	
			var oImg = document.createElement("DIV");	
			oImg = document.body.insertBefore(oImg, document.body.firstChild);
			oImg.style.position = 'absolute';
			oImg.style.top = '-1000px';
			oImg.style.left = '-1000px';
			oImg.style.width = '1px';
			oImg.style.height = '1px';
			oImg.style.display = '';		
			oImg.innerHTML = '<img src="'+baseURL+'view.php?d='+iTNow+'&' + oB.url + '&suref=' + oB.word + '" width="1" height="1" border="0"/>';
		}
		
		
		if (!float)
		{
			float = document.createElement("DIV");
			float = document.body.insertBefore(float, document.body.firstChild);
			float.style.position = 'absolute';
			float.style.top = '-10000px';
			float.style.left = '-10000px';
			float.style.width = 'auto';
			float.style.height = 'auto';
			float.style.display = '';
			float.style.overflow = 'visible';
			float.style.zIndex = 99999;
			
			float.onmouseover = this.BannerOver;
			float.onmouseout = this.BannerOut;

		}
		switch(oBanner.type)
		{
			case 0:  float.innerHTML="";   break;
			case 1:  float.innerHTML=Text();   break;
			case 2:  float.innerHTML=ImageAndText();   break;
			case 3:  float.innerHTML=ImageAndText();   break;
			case 4:  float.innerHTML=Flash(); float.innerHTML = float.innerHTML.replace(/suref%3DNoText/gi, 'suref%3D' + escape(encodeURIComponent(oB.word))); break;
			case 5:  float.innerHTML=FlashNoBorder(); float.innerHTML = float.innerHTML.replace(/suref%3DNoText/gi, 'suref%3D' + encodeURIComponent(oB.word));  break;
			
		}
	
		iVx = document.pageXOffset?document.pageXOffset:document.documentElement.scrollLeft||document.body.scrollLeft, 
		iVy = document.pageYOffset?document.pageYOffset:document.documentElement.scrollTop||document.body.scrollTop, 
		iVw = window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth, 
		iVh = window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight;
		
		
		rect=getPos(oB.oBject);
		
		bannerWH=getPos(float);
		
		dV=(iVy+bannerWH.h>rect.y)?"bottom":"top";
		dO=((rect.x-iVx)+oBanner.iBWidth>iVw)?"left":"right";
		
		var ttop=rect.y-bannerWH.h;
		var lleft=rect.x;
		
		if (dV=="bottom") { ttop = rect.y+rect.h; }
		if (dO=="left") { lleft = rect.x-oBanner.iBWidth-20; }
		
		float.style.left=lleft+'px';
		float.style.top=ttop+'px';
	}
	
	
	this.TextOver = function(oBject,crtCpg,word)
	{
		this.BannerHide();
		
		oBanner=cpg[crtCpg].banner;
		oWords=cpg[crtCpg].words;	
		
		oB.oBject=oBject;
		oB.word=word;
		oB.url=oBanner.url;
		var iShowTimeout = setTimeout('this.BannerShow();',30);
	}
	
	this.TextOut = function()
	{
		iHideTimeout = setTimeout('this.BannerHide();',2000);
	}
	
	this.BannerHide = function()
	{
		clearTimeout(iShowTimeout);
		clearTimeout(iHideTimeout);
	
		if (!oB.oBject) return;
		
		//oB.style.display = 'none';
		float.style.top = '-10000px';
		float.style.left = '-10000px';
	
	}
	
	this.CloseBanner = function()
	{
		this.BannerHide();
	}
	
	this.ClickBanner = function(sURef)
	{
		if (!oB.oBject) return;
		
		var iTNow = (new Date()).getTime();
		
		if (iTNow - lastClick < 500) return;
		lastClick = iTNow;		
		window.open( baseURL+"click.php?" + oB.url+ '&suref=' +oB.word + (sURef ? ('_' + sURef) : ''));
	}
	
	this.BannerOver = function()
	{
		clearTimeout(iHideTimeout);
	}
	
	this.BannerOut = function()
	{
		iHideTimeout = setTimeout('this.BannerHide();', 200);
	}

			
	// Build paragraphs
	
	var sWordB = String.fromCharCode
		(
			0x0020,0x00A0,0x0009,0x000A,0x000D, 0x002E,0x003A, 0x0027,0x00B7,0x05F4,0x2019,0x2027, 0x30FC,0xFF70,0xFF9E,0xFF9F
		),
		sSentenceB = String.fromCharCode
		(
			0x0085,0x2028,0x2029, 
			0x0021,0x003F,0x0589,0x061F,0x06D4,0x0700,0x0701,0x0702,0x0964,0x1362,0x1367,0x1368,0x104A,
			0x104B,0x166E,0x1803,0x1809,0x203C,0x203D,0x2047,0x2048,0x2049,0x3002,0xFE52,0xFE57,0xFF01,0xFF0E,0xFF1F,0xFF61
		)
	;
	
	function GetCharType(c)
	{
		var i = c.charCodeAt(0);
		
		if ((sWordB.indexOf(c) > -1) || (i>=0 && i<=35) || (i>=37 && i<=47) || (i>=58 && i<=64) || (i>=91 && i<=96) || (i>=123 && i<=127))
			return 1;
			
		if (sSentenceB.indexOf(c) > -1) return 2;
		
		return 0;
	}
	
	var vET = {A:1,APPLET:1,AREA:1,CODE:1,FRAME:1,FRAMESET:1,IFRAME:1,MAP:1,NOSCRIPT:1,OBJECT:1,SCRIPT:1,SELECT:1,STYLE:1,TEXTAREA:1,DL:1},
		vPT = {B:1,BIG:1,BR:1,EM:1,I:1,INS:1,Q:1,S:1,SMALL:1,SPAN:1,STRIKE:1,STRONG:1,SUB:1,SUP:1,U:1,VAR:1},
		domNodeArray = [], ii = 0, jj = 0;
	
	domNodeArray[0] = [];
	
	this.Content = function()
	{
		function getDomeNodeString(domNode)
		{
			var k, numberChilds, nD;
		
			if (domNode.nodeType == 3 && typeof(domNode.nodeValue) == 'string')
			{
				if (domNode.nodeValue.replace(/^\s+|\s+$/g, '') != '')
				{
					domNodeArray[ii][jj] = domNode;
					jj++;
				}
			}
			
			if (domNode.hasChildNodes())
			{
				
				if (!vPT[domNode.nodeName.toUpperCase()])
				{
					if (jj > 0)
					{
						ii++;
						domNodeArray[ii] = [];
						jj = 0;
					}
				}
				numberChilds = domNode.childNodes.length;
				
				for (k = 0; k < numberChilds; k++)
				{
					nD = domNode.childNodes[k];
					if (nD)
					{
						if (!vET[nD.nodeName.toUpperCase()])
						{
							
							getDomeNodeString(nD);
							
						}
					}
				}
			}
			
			
		}
		
		getDomeNodeString(document.body);
		
		wordcount = [];
		for (counter = 0; (counter < domNodeArray.length); counter++)
		{
			//alert(iP);
			for (i = 0; i < (domNodeArray[counter].length); i++)
			{
				cNode = domNodeArray[counter][i];
				cNodeParent = cNode.parentNode;
				
				if (!cNodeParent) { 
				}
				
				nValue = cNode.nodeValue;
				
				var start=-1;
				matchArray = [];
				
				for (crtCpg=0; (crtCpg<cpg.length) ; crtCpg++)
				{
					tmpWord=cpg[crtCpg].words;
					tmpBanner=cpg[crtCpg].banner;
					if (wordcount[crtCpg]==null) { wordcount[crtCpg]=0; }
					for (k=0; (k<tmpWord.length) && (wordcount[crtCpg]<tmpBanner.words_nr) ; k++)
					{
						end = -1;
					
						while (true)
						{
							start = nValue.toLowerCase().indexOf(tmpWord[k].toLowerCase(),end+1);	 if (start < 0) break;
							end = start + tmpWord[k].length - 1;
						
							ok = true;
							if ( tmpBanner.repl==1 )
							{
							}else{
								if (start > 0 && GetCharType(nValue.charAt(start-1)) == 0) ok = false;
								if (ok && (end < nValue.length - 1) && GetCharType(nValue.charAt(end+1)) == 0) ok = false;
							}
							
							if (ok)
							{
								matchArray_length = matchArray.length;								
								for (j = 0; j < matchArray_length; j++)//check if exist in other parts
								{
									nod = matchArray[j];
									
									if ((start >= nod.start && start <= nod.end) || (end >= nod.start && end <= nod.end))
									{
										ok = false;
										break;
									}
								}
								if (ok)
								{
									wordcount[crtCpg]++;
									matchArray[matchArray.length] = {start:start, end:end, crtCpg:crtCpg};
									if (wordcount[crtCpg]>=tmpBanner.words_nr) break;
								}
							}
						}//end while (true)
					}//end 	k<tmpWord.length
				}//end for cpg.length
				
				function CN(a, b){return a.start - b.start};
				matchArray.sort(CN);
				
				//create new cNode
				iLastInd = 0;
				sliceNewWords	= [];
				if (matchArray.length>0)
				{
					for (iW2 = 0; iW2 < matchArray.length; iW2++)
					{
						nod= matchArray[iW2];
						
						if (iLastInd < nod.start)
						{
							sliceNewWords[sliceNewWords.length] = nValue.substring(iLastInd, nod.start - 1 + 1);
						}
						
						sText = nValue.substring(nod.start, nod.end + 1); //get original word
						
						if ( cpg[nod.crtCpg].banner.repl==1 )
						{
							  sliceNewWords[sliceNewWords.length] = '<a href="javascript:;" onClick="ClickBanner(\'ClickOnKeyword\');" onmouseover="TextOver(this,'+nod.crtCpg+',\''+sText+'\')" onmouseout="TextOut()"><img src="'+cpg[nod.crtCpg].banner.ico+'" border="0" style="text-decoration:none; vertical-align:middle"></a>';
						}else{
							  sliceNewWords[sliceNewWords.length] = '<a class="link_cAds" href="javascript:;" onClick="ClickBanner(\'ClickOnKeyword\');" onmouseover="TextOver(this,'+nod.crtCpg+',\''+sText+'\')" onmouseout="TextOut()" style="color:#'+cpg[nod.crtCpg].banner.color+'; text-decoration:underline">'+sText+'</a> <a href="javascript:;" onClick="ClickBanner(\'ClickOnKeyword\');" onmouseover="TextOver(this,'+nod.crtCpg+',\''+sText+'\')" onmouseout="TextOut()">'+((cpg[nod.crtCpg].banner.ico)?'<img src="'+cpg[nod.crtCpg].banner.ico+'" border="0" style="text-decoration:none; vertical-align:middle">':"")+'</a>';
						}
						
						
						iLastInd = nod.end + 1;
					}
					
					if (iLastInd < nValue.length)
					{
							sliceNewWords[sliceNewWords.length] = nValue.substr(iLastInd);
					}
					
					publicitate = document.createElement('pub');
					if (publicitate)
					{	
						publicitate.innerHTML=sliceNewWords.join('');
						cNodeParent.replaceChild(publicitate, cNode);
					}
				}
			}
		}
	}
	
	this.loadScript = function()
	{
		//alert("load");
		if (location.pathname=="/" || location.pathname=="") return;
		
		if (load_script != false) return;
		load_script = true;
		setTimeout('this.Content()', 300);
	}
	
	
	if (window.addEventListener)
	{
		window.addEventListener("DOMContentLoaded",this.loadScript, false);
		window.addEventListener("load",this.loadScript, false);
	}
	else if (window.attachEvent)
	{
		window.attachEvent("onload",this.loadScript);
	}
	
	setTimeout('this.loadScript();', 20000);
}

function NewsAlert(banner)
{
	
	var fltop = document.createElement("DIV");
	fltop.id = "news_alert";
	fltop.style.zIndex = 65002;
	fltop = document.body.insertBefore(fltop, document.body.firstChild);
	
	document.write('<link href="http://ctx.inin.ro/css/news_alert.css" rel="stylesheet" type="text/css" media="screen" />');
	
	fltop.innerHTML='<div class="news_'+banner.type+'">'+
						'<div style="float:left">'+
						'<a href="'+baseURL+banner.url+'" target="_blank" class="newsalert_text_'+banner.type+'">'+
						banner.name+
						'</a>'+
						'</div>'+
						'<div style="width:40px; height:15px; float:right; padding-top:3px;">'+
							'<a href="javascript:CloseBanner()"><img src="http://ctx.inin.ro/images/newsalert/newsalert_close_'+banner.type+'.jpg" border="0" /></a>'+
						'</div>'+
					'</div>';
	
	this.CloseBanner = function()
	{
		var oImg = document.createElement("DIV");	
		oImg = document.body.insertBefore(oImg, document.body.firstChild);
		oImg.style.position = 'absolute';
		oImg.style.top = '-1000px';
		oImg.style.left = '-1000px';
		oImg.style.width = '1px';
		oImg.style.height = '1px';
		oImg.style.display = '';		
		oImg.innerHTML = '<img src="'+baseURL+banner.url+"&close=1" + '" width="1" height="1" border="0"/>';
		
		fltop.style.top = '-10000px';
		fltop.style.left = '-10000px';
	}				
}


function cAdsRequest()
{
	loadURL=baseURL+"show.php?site="+cAds.site;	
	document.write('<scr'+'ipt src="'+ loadURL +'"><\/scr'+'ipt>');	
}
cAdsRequest();
