// Copyright (c) Admeta AB 2008-2011
// Version 2.18.1

Admeta.Text.initCached = function (_adsX,_adsY,_w,_h,_clip,_font,_bfs,_bc,_fc,_lc,_tc,_bdc,_pc,_ult,_ull,_pubType,_pubInfo,_pubUrl,_brdType,_clkTp,_imgW,_imgH,_bkgrImg,_centerImg,_txtSize,_layout,_centerText,_additionalText,_additionalTextPos,_storeImg,_storeX,_storeY,_pubFontS,_pwrdByH)
{
	Admeta.Text.TAD=[];
	Admeta.Text.HTML="";
	_imgW=_imgW===undefined?50:_imgW;
	_imgH=_imgH===undefined?_imgW:_imgH;
	var imgMrg=_w>=250?6:4,
		bdt=_brdType===undefined?1:(_brdType==2&&_adsY==1?1:_brdType),
		pubType=_pubType===undefined?0:_pubType,
		pwrdByH=pubType!=0?(_pwrdByH!==undefined&&pubType==1?_pwrdByH:(_h>=90?15:12)):0;

	Admeta.Text.params=
		{adsX:_adsX,adsY:_adsY,width:_w,height:_h,containerHeight:_h,ads:0,clip:_clip,font:_font,bfs:_bfs,bc:_bc,fc:_fc,lc:_lc,bdc:_bdc,pc:_pc,tc:_tc,
		ult:_ult,ull:_ull,pubType:pubType,pubInfo:_pubInfo,pubUrl:_pubUrl,borderType:bdt,imgW:_imgW,imgH:_imgH,
		imageMargin: imgMrg,pwrdByH:pwrdByH,clkTp:_clkTp===undefined?1:_clkTp,bkgrImg:_bkgrImg,centerImg:_centerImg===undefined?0:_centerImg,
		startSz:_txtSize===undefined?0:_txtSize,imgF:1,imgFC:_bdc,margin:3,layout:_layout===undefined?0:_layout, centerText:_centerText===undefined?0:_centerText,
		additionalText:_additionalText!==undefined?_additionalText:0,additionalTextPos:_additionalTextPos,additionalTextBold:1,additionalTextItalic:0,
		storeImg:_storeImg===undefined||_storeImg==""?undefined:_storeImg,storeX:_storeX,storeY:_storeY,pubFontS:_pubFontS===undefined?9:_pubFontS};
};

Admeta.Text.idMeasurePoint="ADMmeasurePoint";
Admeta.Text.idOperaSib="operaSib";

Admeta.Text.createTestElemDI = function()
{
	document.write("<div id='"+this.idMeasurePoint+"' style='position:absolute;visibility:hidden;'></div>");
	this.testElem=document.getElementById(this.idMeasurePoint);
	if(!this.testDiv()) return this.createTestElemProper();
	document.write("<div id='"+this.idOperaSib+"' style='visibility:hidden;'></div>");
	this.operaSib=document.getElementById(this.idOperaSib);
}

Admeta.Text.createTestElemProper = function()
{
	this.deleteTestElem();
	if(document.body===undefined){this.measureOK=false;return;}
	this.testElem=document.createElement("div");
	this.testElem.style.visibility="hidden";
	document.body.appendChild(this.testElem);
	if(!this.testDiv()){this.deleteTestElem();this.measureOK=false;return;}
	this.operaSib=document.createElement("div");
	this.operaSib.style.visibility="hidden";
	document.body.appendChild(this.operaSib);
}

Admeta.Text.deleteTestElem = function()
{
	if(this.testElem) this.testElem.parentNode.removeChild(this.testElem);
	if(this.operaSib) this.operaSib.parentNode.removeChild(this.operaSib);
	this.testElem=this.operaSib=undefined;
}

Admeta.Text.testDiv = function()
{
	var OK=this.testElem!==null;
	if(OK){
		this.testElem.innerHTML='A';
		OK=this.testElem.offsetHeight>0;
	}
	return OK;
}

Admeta.Text.createTestElem = function()
{
	this.measureOK=true;
	if(this.params.Operation==1){
		if(!document.body)
			document.write("<body>");
		this.createTestElemDI();
	} else
		this.createTestElemProper();

	if(!this.measureOK) return;
	
	var es=this.testElem.style;
	es.height="auto";
	es.margin=0;
	es.padding=0;
	es.fontStyle="normal";
	es.position="absolute";
	es.lineHeight="normal";
}

Admeta.Text.getCached = function(w,t,ff,fsz,fslh,b,ul,nw)
{
	var m=this.cachedM[w+ff+fsz+t+b+ul+nw+"_"+t];
	return m;
}

Admeta.Text.storeCached = function(w,t,ff,fsz,fslh,b,ul,nw,m)
{
	this.cachedM[w+ff+fsz+t+b+ul+nw+"_"+t] = m;
}

Admeta.Text.measureText = function (w,t,ff,fsz,fslh,b,ul,it,nw)
{
	if(t==undefined) return {h:0,w:0};
	var m=Admeta.Text.getCached(w,t,ff,fsz,fslh,b,ul,nw);
	if(m!==undefined) return m;
	if(Admeta.Text.measures!==undefined) Admeta.Text.measures++;
	var ele=Admeta.Text.testElem;
	ele.innerHTML=t;
	var es=ele.style;

	es.width=w+"px";
	es.fontFamily=ff;
	es.fontSize=fsz+"px";
	es.lineHeight="normal";
	es.fontStyle=it?"italic":"normal";
	es.fontWeight=b?"bold":"normal";
	es.textDecoration=ul?"underline":"none";
	es.whiteSpace=nw?"nowrap":"";
	es.overflowX="auto";
	var w=ele.scrollWidth;
	es.overflowX="hidden";

	var m={h:ele.offsetHeight,w:w};

	Admeta.Text.storeCached(w,t,ff,fsz,fslh,b,ul,nw,m);
	return m;
};

Admeta.Text.measureAd = function (s)
{
	if(this.iter!==undefined) this.iter++;
	var fsz=s.fs-s.tld,th=0,mw=0,m,mt=this.measureText;
	var fits=1;
	if(s.dtl) {m=mt(s.tlw,s.tlt,s.ff,fsz,Math.ceil(fsz+fsz/4),1,s.ult,0,0);th=m.h;mw=m.w;if(m.w>s.tlw) fits=0;};
	fsz=s.fs-s.td;
	var m=mt(s.tw,s.tt,s.ff,fsz,Math.ceil(fsz+fsz/4),0,0,0,0),tth=m.h;
	if(m.w>mw)mw=m.w;
	if(s.lyo==1)
		th+=(s.imgFits?s.embImgH:0)+tth;
	else
		th+=s.tlw!=s.tw&&tth<s.embImgH&&s.imgFits?s.embImgH:tth;
	if(m.w>s.tw) fits=0;
	fsz=s.fs-s.ld;
	var lh=0,addtxtTh=0;
	if(s.dl) {lh=mt(s.lw,s.lt,s.ff,fsz,Math.ceil(fsz+fsz/4),0,s.ull,0,1).h;};
	if(s.hasAddTxt) {addtxtTh=mt(s.lw,s.addtxtT,s.ff,fsz,Math.ceil(fsz+fsz/4),s.bp,0,s.itp,1).h;}
	th+=(lh>addtxtTh?lh:addtxtTh);
	if(th>s.ch) fits=0;
	return {h:th,w:mw,fits:fits};
};

Admeta.Text.Steps=[[[0,-1,2],[1,-1,2],[1,0,2],[1,0,3],[2,0,3],[2,1,3],[2,2,3],[3,2,3],[3,3,3]],
				   [[0,-1],[1,-1],[1,0],[2,0],[2,1],[2,2],[3,2],[3,3]],
				   [[0],[1],[2],[3]]];
Admeta.Text.t2Start=[[0,0,0],[2,2,1],[5,3,2]];

Admeta.Text.setDec = function(s,t1,t2,img)
{
	var st=this.Steps[t1][t2];

	s.dtl=t1<2;
	s.dl=t1<1;

	if(s.dtl) s.tld=st[1];
	if(s.dl) s.ld=st[2];
	s.td=st[0];

	if(!img)
		{s.tlw=s.cw;s.tw=s.cw;s.lw=s.cw;s.imgFits=0;}
	else
		{s.tlw=s.tlwMI;s.tw=s.twMI;s.lw=s.lwMI;s.imgFits=1;}
}

Admeta.Text.binSearch = function(s)
{
	var t=Math.floor((s.end-s.start)/2);
	if(t==0) return;
	t+=s.start;

	this.setDec(s,s.t1,t-1,s.tImg==0);

	m=this.measureAd(s);

	if(m.fits)
	{
		s.t2=t-1;
		s.end=t;
		s.foundFit=1;
	}
	else
		s.start=t;

	this.binSearch(s);
}

Admeta.Text.measureBin = function(s)
{
	var fl=true;
	for(;s.tImg<2;s.tImg++){
		for(;s.t1<this.Steps.length;s.t1++){
			if(!fl) s.t2=this.t2Start[this.params.startSz][s.t1];
			fl=false;
			var st=this.Steps[s.t1];
			s.start=s.t2;
			s.end=st.length+1;
			s.foundFit=0;
			Admeta.Text.binSearch(s);

			if (s.foundFit){
				this.setDec(s,s.t1,s.t2,s.tImg==0);
				return s;
			}
		}
		s.t1=0;
	}
	s.tImg=1;
	s.t1=this.Steps.length-1;s.t2=this.Steps[s.t1].length-1;
	return s;
}

Admeta.Text.advancePos = function(s)
{
	s.t2++;
	var t2s=this.t2Start[this.params.startSz];
	if(s.t2>=this.Steps[s.t1].length) {s.t1++;s.t2=t2s[s.t1];}
	if(s.t1>=3) {s.t1=0;s.t2=t2s[0];s.tImg++;}
}

Admeta.Text.FindOpt = function(s)
{
	if(!this.measureOK){
		s.t1=0;
		s.t2=this.Steps[0].length-1;
	}
	this.setDec(s,s.t1,s.t2,s.tImg==0);
	if(!this.measureOK) return;
	var m=this.measureAd(s);
	if(m.fits||(s.tImg==1&&s.t1==this.Steps.length-1&&s.t2==this.Steps[s.t1].length-1)) return;

	if((m.h-s.ch)/s.ch>0.15&&!Admeta.FORCEMEASUREINC)
		return this.measureBin(s);

	this.advancePos(s);
	this.measureInc(s);
}

Admeta.Text.measureInc = function (s)
{
	var m;
	var fl=true;
	for(;s.tImg<2;s.tImg++){
		for(;s.t1<this.Steps.length;s.t1++){
			if(!fl) s.t2=this.t2Start[this.params.startSz][s.t1];
			fl=false;
			var st=this.Steps[s.t1];
			for(;s.t2<st.length;s.t2++){
				s.lastImg=(s.tImg==0);
				this.setDec(s,s.t1,s.t2,s.lastImg);
				m=this.measureAd(s);
				if(m.fits) return;
			}
		}
		s.t1=0;
	}
	s.tImg=1;
	s.t1=this.Steps.length-1;s.t2=this.Steps[s.t1].length-1;
};

Admeta.Text.createCached = function (title,text1,text2,url,ct,imageUrl,additionalText,storeLink)
{
	this.params.ads++;
	this.TAD.push({title:title,text1:text1,text2:text2,url:url,ct:(ct.substring(0,4).toLowerCase()!="http"?"http://":"")+ct,
		imageUrl:imageUrl,additionalText:additionalText,storeLink:storeLink});
};

Admeta.Text.getCellHeight = function()
{
	var p=this.params,
		hasOuterBorder=p.bdc!==undefined&&p.borderType==1,
		innerHeight=p.containerHeight-(hasOuterBorder?2:0);
	return Math.floor((innerHeight-p.pwrdByH-(p.adsY*(p.borderType==2?4:2)-2))/p.adsY);
};

Admeta.Text.getCellWidth = function()
{
	var p=this.params,
		innerMargin=p.adsX>1?8:0,
		hasOuterBorder=p.bdc!==undefined&&p.borderType!=0,
		width=p.width-(hasOuterBorder?2:0),
		innerWidth=width-p.margin*2;
	return Math.floor((innerWidth-innerMargin*(p.adsX-1))/p.adsX);
};

Admeta.Text.getMaxSizes = function()
{
	var p=this.params,adl=this.TAD.length;

	if(p.adsX*p.adsY>adl){
		if(adl<=p.adsY){p.adsY=adl;p.adsX=1;}
		else if(adl<p.adsX*p.adsY){
			var y=Math.ceil(adl/2);
			if(y<p.adsY) p.adsY=y;
			p.adsX=Math.ceil(adl/p.adsY);
		}
	}

	var embedImg=p.imgH<=25&&p.layout==0,
		rM=p.layout==1?0:p.imageMargin-p.imgF,
		lM=embedImg||p.layout==1?0:(p.imageMargin-p.margin-p.imgF),
		imageTotalWidth=p.imgW+p.imgF*2+lM+rM,
		storeImgW=p.storeImg!==undefined&&p.layout==0?p.storeX+2:0,
		cw=this.getCellWidth()-storeImgW,
		ch=this.getCellHeight()-1-(p.storeImg!==undefined&&p.layout==1?p.storeY+2:0),
		allImg=true,
		imgBefore=false,
		hasReset=false;

	Admeta.Text.cachedM={};

	this.createTestElem();
	var a=0,adc=0;

	while(a<p.adsY*p.adsX&&adc<adl){

		var m,
			ad=this.TAD[adc],
			hasImg=ad.imageUrl!==undefined,
			hasTitle=ad.title!==undefined,
			hasLink=ad.url!==undefined,
			center=p.adsX==1||p.centerImg,
			embImgTotH=(p.layout==0?((!center&&ch>=p.imgH+5)?3:1):4)+p.imgH+1+p.imgF*2,
			imgFits=embImgTotH<=ch&&((p.layout==1&&imageTotalWidth<=cw)||(p.layout==0&&cw-imageTotalWidth>100))&&(m==undefined||m.tImg==0||!imgBefore),
			maxWidth=(hasImg&&!embedImg&&imgFits&&p.layout==0?cw-imageTotalWidth:cw),
			twMaxWidth=(hasImg&&imgFits&&p.layout==0?cw-imageTotalWidth:cw),
			calcForImg=hasImg&&imgFits;

		if(m===undefined) m={lhFit:-1,t1:0,t2:this.t2Start[p.startSz][0],tImg:calcForImg?0:1,ff:p.font,fs:p.bfs,tld:-1,td:0,ld:2,ult:p.ult,ull:p.ull,dtl:1,dl:1,imgFits:imgFits,fits:1,cw:cw,ch:ch,embImgH:embImgTotH,lyo:p.layout,hasAddTxt:p.additionalText,itp:p.additionalTextItalic,bp:p.additionalTextBold};

		if(!imgBefore){
			m.tImg=calcForImg?0:1;
			m.imgFits=imgFits;
		}

		m.tlwMI=maxWidth;
		m.lwMI=maxWidth;
		m.twMI=twMaxWidth;
		m.tlt=ad.title;
		m.tt=ad.text1+" "+ad.text2;
		m.lt=ad.url;
		m.addtxtT=ad.additionalText;
		Admeta.Text.FindOpt(m);

		m.tlfs=m.fs-m.tld;
		m.tfs=m.fs-m.td;
		m.lfs=m.fs-m.ld;

		if(m.tImg==1&&calcForImg&&!hasReset){
			a=0;adc=0;
			calcForImg=false;
			m={lhFit:-1,t1:0,t2:this.t2Start[p.startSz][0],tImg:1,ff:p.font,fs:p.bfs,tld:-1,td:0,ld:2,ult:p.ult,ull:p.ull,dtl:1,dl:1,imgFits:false,fits:1,cw:cw,ch:ch,embImgH:embImgTotH,lyo:p.layout,hasAddTxt:p.additionalText,itp:p.additionalTextItalic,bp:p.additionalTextBold};
			hasReset=imgBefore=true;
		}
		else {
			a++;adc++;
		}

		if(!calcForImg)
			allImg=false;
		else
			imgBefore=true;
	}

	this.deleteTestElem();

	return {fsz:m,allImg:allImg};
};

Admeta.Text.get = function ()
{
	var sizes=this.getMaxSizes(),
		displayType=navigator.userAgent.toLowerCase().indexOf('msie')!=-1?"inline":"inline-table",
		p=this.params,
		wt=Admeta.Text.writeTag,
		smh=this.setMaxHeight,
		embedImg=p.imgH<=25&&p.layout==0,
		innerMargin=p.adsX>1?8:0,
		hasOuterBorder=p.bdc!==undefined&&p.borderType==1,
		hasInnerBorder=p.bdc!==undefined&&p.borderType==2,
		width=p.width-(hasOuterBorder?2:0),
		innerWidth=width-p.margin*2,
		innerHeight=p.containerHeight-(hasOuterBorder?2:0),
		rowHeight=Admeta.Text.getCellHeight(),
		baseFont=";font-style:normal;font-family:"+p.font+";",
		baseFontTxtA=";text-align:left;"+baseFont,
		np = ";padding:0px;margin:0px;",
		bc="background-color:"+(p.bkgrImg!==undefined?"transparent":p.bc)+";",
		resetTA=baseFont+";font-weight:normal;line-height:normal;background-color:"+bc+";text-decoration:none"+np,
		fc="color:"+p.fc+";",
		bcb=hasInnerBorder?"background-color:transparent;":bc,
		resetTb=";overflow:hidden;"+np+"border:0;text-align:inherit;font-size:inherit;background:none;width:inherit;",
		lnkAttr=" target='_blank' rel='nofollow'",
		centerContent=p.adsX>1,
		rows=Math.floor(this.TAD.length/p.adsX);

	rows=rows>p.adsY?p.adsY:rows;
	
	var idOuter=(((1+Math.random())*0x1000000)|0).toString(16).substring(1);

	var oRowH=p.adsX==1?"auto":Math.floor((innerHeight-p.pwrdByH)/rows)+"px",
		ch=p.adsX==1&&p.adsY==1,
		totalCellWidth=Admeta.Text.getCellWidth(),
		imgRM=p.layout==1?0:p.imageMargin-p.imgF,
		imgLM=embedImg||p.layout==1?0:(p.imageMargin-p.margin-p.imgF),
		imageTotalWidth=p.imgW+p.imgF*2+imgLM+imgRM,
		OKCenterAd=!Admeta.isIE()||Admeta.IEVersion()>=8||totalCellWidth>300,
		idOuterFS=(hasOuterBorder?"":"_")+"AMFS"+idOuter,
		idOuterTB=(hasOuterBorder?"_":"")+"AMTB"+idOuter;

	if(hasOuterBorder)
		wt("fieldset","style='border: solid 1px "+p.bdc+";padding:0px;margin:0px;width:"+(p.width-2)+"px;height:"+(p.height-2)+"px;display:inline-table;' id='"+idOuterFS+"'");

	wt("table",
		"id='"+idOuterTB+"' border='0' cellpadding='0' cellspacing='0' width='"+width+"px' style='display:"+
		displayType+";"+bcb+baseFontTxtA+resetTb+bcb+";width:"+width+"px;height:"+innerHeight+"px;"+(p.bkgrImg!==undefined?"background-image:url(\""+p.bkgrImg+"\");background-repeat:repeat;'":"'"));

	var adc=0;

	if(p.pubType==1){
		wt("tr");
		wt("td","style='"+resetTb+"height:"+p.pwrdByH+"px;background-color:"+p.bdc+";overflow:hidden;vertical-align:middle;' valign='middle'");
		wt("div","style='position:relative;width:"+width+"px;max-width:"+width+"px;"+resetTb+";white-space:nowrap;text-align:center;color:"+p.bc+";background-color:"+p.bdc+";'");
		wt("a","style='color:"+p.pc+";background-color:"+p.bdc+np+"font-size:"+p.pubFontS+"px;font-weight:bold;font-style:normal;line-height:normal;font-family:"+p.font+";text-decoration:none;' href='"+p.pubUrl+"'"+lnkAttr);
		this.HTML+=p.pubInfo;
		wt("/a");
		wt("/div");
		wt("/td");
		wt("/tr");
	}

	if(p.pubType==2){
		wt("tr");
		wt("td","style='"+resetTb+bcb+"'");
		wt("div","style='position:relative;width:"+width+"px;height:"+innerHeight+"px;padding:0px;margin:0px;"+bcb+"'");
			wt("a","href='"+p.pubUrl+"' style='position:absolute;right:0px;bottom:0px;z-index:100;'"+lnkAttr);
			wt("img","src='"+p.pubInfo+"' style='border:0px;margin:0;padding:0;display:block !important'");
			wt("/img");
			wt("/a");
		wt("table","border='0' cellpadding='0' cellspacing='0' width='100%' style='"+resetTb+bcb+";width:100%;height:"+innerHeight+"px'");
	}

	for (var y=0;y<p.adsY;y++){
		if(adc>=this.TAD.length) break;

		wt("tr");
		var pd = "0 "+p.margin+"px 0 "+p.margin+"px;";
		wt("td","style='padding: "+(p.borderType!=2?pd:"0px;")+"margin:0px;vertical-align:middle;border:0;background:none;"+bcb+";height:"+oRowH+(ch?"' align='center'":"'"));
		if(ch) wt("div","style='position:relative;width:"+totalCellWidth+"px;max-width:"+totalCellWidth+"px;"+resetTb+"'");

		var rm=0;
		if(p.borderType==2){
			rm=2;
			wt("fieldset","style='"+bc+";padding:"+pd+";border: solid 1px "+p.bdc+";margin:"+(y>0?2:0)+"px 0 0 0;'");
		}

		wt("table","cellspacing='0' cellpadding='0' border='0' style='"+resetTb+bc+";width:100%;"+(centerContent?"height:auto'":"'"));
		wt("tr");
		for (var x=0;x<p.adsX;x++){

			var imgIm=(!embedImg&&x!=0?innerMargin:0);

			if(adc>=this.TAD.length){
				wt("td","style='"+resetTb+bc+"'");
				wt("/td");
				break;
			}
			var ad=this.TAD[adc],
				m=sizes.fsz,
				hasImg=ad.imageUrl!==undefined&&m.imgFits,
				hasTitle=ad.title!==undefined&&m.dtl,
				hasLink=ad.url!==undefined&&m.dl,
				hasAddText=p.additionalText&&(ad.additionalText!==undefined&&ad.additionalText!=""),
				eimg=embedImg&hasImg,
				doRows=eimg||(p.layout==1&&hasImg),
				tclk=p.clkTp==0||(p.clkTp==2&&!hasLink)||(p.clkTp==1&&doRows),
				cellWidth=totalCellWidth-(hasImg&&p.layout==0?imageTotalWidth:0)-(p.storeImg!==undefined&&p.layout==0?p.storeX+2:0),
				center=p.adsX==1||(p.centerImg&&sizes.allImg),
				valign=center?"middle":"top",
				valignImgText=eimg?"middle":valign,
				imgMarginTop=(!center&&innerHeight>=p.imgH+3+p.imgF*2)?3:1,
				mtcws=totalCellWidth+"px",
				tcws=ch&&OKCenterAd?"auto":mtcws,
				mcws=cellWidth+"px",
				cws=ch&&OKCenterAd?"auto":mcws,
				halign="text-align:"+(p.centerText?"center;":"left;");

			if(doRows&&(hasTitle||hasLink||hasAddText||p.layout==1)){
				wt("td","align='left' style='"+resetTb+bc+";text-align:left;vertical-align:"+valign+";'");
				wt("div","style='position:relative;overflow:hidden;"+bc+np+";width:"+tcws+";max-width:"+mtcws+";"+smh(rowHeight)+(x!=0?"margin-left:"+innerMargin+"px":"")+"'");
				wt("table","cellspacing='0' cellpadding='0' border='0' width='"+tcws+"' style='text-align:left;"+resetTb+bc+";'");
				wt("tr");
			}

			if(hasTitle&&doRows){
				wt("td","style='"+resetTb+bc+"' colspan='2' valign='bottom'");
				wt("div","style='position:relative;line-height:normal;"+bc+np+";width:"+tcws+";max-width:"+mtcws+"'");
				this.writeTitle(tclk,ad,bc,np,baseFont+halign,lnkAttr,m.tlfs);
				wt("/div");
				wt("/td");
				wt("/tr");
				wt("tr");
			}

			if(hasImg){
			    var an=(p.layout==0?"right":(p.centerText?"center":"left"));
				wt("td","valign='"+valign+"' align='"+an+"' style='"+resetTb+bc+";text-align:"+an+";vertical-align:"+valign+";"+(p.layout==0?"width:"+(imageTotalWidth+imgIm)+"px;":"")+"'");
				wt("a","href='"+ad.ct+"' style='"+resetTA+"'"+lnkAttr);
				wt("img","src='"+ad.imageUrl+"' width='"+p.imgW+"px' height='"+p.imgH+"px' style='border:solid "+p.imgF+"px "+p.imgFC+";padding:0px;margin:"+imgMarginTop+"px "+
					imgRM+"px 1px "+imgLM+"px;vertical-align:"+valign+";display:block !important'");
				wt("/img");
				wt("/a");
				wt("/td");
				if(p.layout==1){wt("/tr");wt("tr");}
			}

			wt("td","style='vertical-align:"+valignImgText+";"+resetTb+bc+
					(p.clip==1?"white-space:nowrap;'":"'"));

			wt("div","style='position:relative;overflow:hidden;line-height:normal;"+bc+"padding:0px 0px 1px 0px;width:"+cws+";max-width:"+mcws+";"+smh(rowHeight-1)+
				"margin:0 0 0 "+(x===0||hasImg?0:innerMargin)+"px;"+(p.clip==1?"white-space:nowrap;'":"'"));

			if(p.clkTp==1)
				wt("a","href='"+ad.ct+"' style='text-decoration:none;display:block;width:"+cws+";max-width:"+mcws+";height:100%;"+resetTA+fc+"'"+lnkAttr);

			if(hasTitle&&!doRows){
				this.writeTitle(tclk,ad,bc,np,baseFont+halign,lnkAttr,m.tlfs);
			}

			var dstl = "style='font-weight:normal;text-decoration:none;font-size:"+m.tfs+"px;line-height:normal;"+fc+bc+np+baseFont+halign+";display:block;' ",
				lnk=p.clkTp!=1&&!hasTitle&&!hasLink;

			wt(lnk?"a":"span",dstl+(lnk?lnkAttr+(lnk?" href='"+ad.ct+"'":""):""));
			this.HTML+=ad.text1+" "+ad.text2;
			wt(lnk?"/a":"/span");

			var atbf=";font-family:"+p.font+";"+halign+";font-style:"+(p.additionalTextItalic?"italic":"normal")+";font-weight:"+(p.additionalTextBold?"bold":"normal")+";",atnp=";margin:0;padding: 0 10px 0 0;";

			if(!doRows){
				if(hasAddText&&hasLink){wt("span","style='display:block;"+bc+np+halign+"'");}

				if(hasAddText){
					this.writeAddText(ad,bc,atnp,atbf+halign,m.tfs,!hasLink);
				}

				if(hasLink){
					this.writeLink(p.clkTp!=1,ad,bc,np,baseFont+halign,lnkAttr,m.lfs,!hasAddText);
				}

				if(hasAddText&&hasLink){wt("/span");}
				
				if(p.storeImg!==undefined&&p.layout==1)
				{
					this.writeStoreLink(ad.storeLink);
				}
			}

			wt("/a");
			wt("/div");
			wt("/td");
			
			if(p.storeImg!==undefined&&p.layout==0)
			{
				wt("td","style='"+resetTb+bc+"width:"+p.storeX+"px;' valign='bottom'");
				this.writeStoreLink(ad.storeLink);
				wt("/td");
			}

			if((hasLink||hasAddText)&&doRows){
				wt("/tr");
				wt("tr");
				wt("td","style='"+resetTb+bc+"' colspan='2' valign='top'");
				wt("div","style='position:relative;overflow:hidden;line-height:normal;"+bc+np+halign+";width:"+tcws+"px;white-space:nowrap;'");
				if(hasAddText) this.writeAddText(ad,bc,atnp,atbf,m.tlfs,!hasLink);
				if(hasLink) this.writeLink(true,ad,bc,np,baseFont+halign,lnkAttr,m.lfs,!hasAddText);
				wt("/div");
				wt("/td");
			}
			
			if(doRows&&p.storeImg!==undefined&&p.layout==1)
			{
				wt("/tr");
				wt("tr");
				wt("td","style='"+resetTb+bc+"' colspan='2' valign='top'");
				this.writeStoreLink(ad.storeLink);
				wt("/td");
			}

			if(doRows&&(hasTitle||hasLink||p.layout==1)){
				wt("/tr");
				wt("/table");
				wt("/div");
				wt("/td");
			}

			adc++;
		}
		wt("/tr");

		wt("/table");

		if(p.borderType==2){
			wt("/fieldset");
		}

		if(ch) wt("/div");
		wt("/td");
		wt("/tr");
	}

	if(p.pubType==2){
		wt("tr");
		wt("td","align='right' valign='bottom'"+(p.pwrdByH>0?" style='"+resetTb+bcb+";text-align:right;height:"+p.pwrdByH+"px'":""));
		wt("/td");
		wt("/tr");
		wt("/table");
		wt("/div");
		wt("/td");
		wt("/tr");
	}

	wt("/table");
	if(hasOuterBorder)
		wt("/fieldset");

	return this.HTML;
};

Admeta.Text.writeStoreLink = function(link)
{
	var wt=this.writeTag,p=this.params;
	wt("a","href='"+link+"' target='_blank' rel='nofollow' style='border:0px;margin:0;padding:0;'");
	wt("img","src='"+p.storeImg+"' width='"+p.storeX+"' height='"+p.storeY+"' style='border:0px;padding:0;"+(p.layout==0?"margin-left":"margin-top")+":2px;display:block !important'");
	wt("/img");
	wt("/a");
}

Admeta.Text.writeTitle = function (clk,ad,bc,np,bf,lnkAttr,fst)
{
	var wt=this.writeTag,
		p=this.params,
		tstl = "style='display:block;font-weight:bold;color:"+p.tc+";font-size:"+fst+"px;line-height:normal;"+"text-decoration:"+
				(p.ult?"underline;":"none;")+bc+np+bf+"' ";
	wt(clk?"a":"span",tstl+(!clk?"":lnkAttr+" href='"+ad.ct+"'"));
	this.HTML+=ad.title;
	wt(clk?"/a":"/span");
}

Admeta.Text.writeLink = function(clk,ad,bc,np,bf,lnkAttr,fsl,block)
{
	var wt=this.writeTag,
		p=this.params,
		lstl = "style='"+(block?"display:block;":"")+"font-weight:normal;white-space:nowrap;text-decoration:"+(p.ull?"underline;":"none;")+
				";font-size:"+fsl+"px;line-height:normal;color:"+p.lc+";"+bc+np+bf+"' ";
	wt(clk?"a":"span",lstl+(!clk?"":lnkAttr+" href='"+ad.ct+"'"));
	this.HTML+=ad.url;
	wt(clk?"/a":"/span");
}

Admeta.Text.writeAddText = function(ad,bc,np,bf,fs,block)
{
	var wt=this.writeTag,
		p=this.params,
		lstl = "style='"+(block?"display:block;":"")+"white-space:nowrap;text-decoration:none;font-size:"+fs+"px;line-height:normal;color:"+p.fc+";"+bc+np+bf+"' ";
	wt("span",lstl);
	this.HTML+=ad.additionalText;
	wt("/span");
}

Admeta.Text.setMaxHeight = function(height)
{
	return "height:expression(this.scrollHeight>"+height+"?\""+height+"px\":\"auto\");max-height:"+height+"px;"
}

Admeta.Text.writeTag = function (tag,attribs)
{
	Admeta.Text.HTML+="<"+tag+(attribs!==undefined?" "+attribs:"")+">";
};

Admeta.Text.insertCached = function (pid,o,adId,SIV,gid,renderF,RF,eff)
{
	mat = Admeta.setupMaterial(pid,o,adId,gid,SIV,RF,eff,4,this.params.width,this.params.height);

	var c=Admeta.getContainer(pid,o,gid);

	if(renderF===undefined||(!renderF.cb(c,this.TAD,this.params.width,this.params.height)&&!renderF.force))
		c.innerHTML=Admeta.embedMaterial(mat,this.params.width,this.params.height,this.get());

	if(Admeta.postP) Admeta.postP(mat);
};

Admeta.Text.writeCached = function (pid,o,adId,SIV,gid,renderF,RF,eff)
{
	mat = Admeta.setupMaterial(pid,o,adId,gid,SIV,RF,eff,4,this.params.width,this.params.height);

	if(renderF===undefined||(!renderF.cb(this.TAD,this.params.width,this.params.height)&&!renderF.force))
		document.write(Admeta.embedMaterial(mat,this.params.width,this.params.height,this.get()));

	if(Admeta.postP) Admeta.postP(mat);
};

Admeta.isIE = function()
{
	return navigator.userAgent.indexOf("MSIE ") != -1;
};

Admeta.IEVersion = function ()
{
	var ua = navigator.userAgent;
	var MSIEOffset = ua.indexOf("MSIE ");

	if (MSIEOffset == -1) {
		return 0;
	} else {
		return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
	}
}

Admeta.Text.ProcessTextAds = function()
{
	var at=Admeta.Text;
	var agp=at.AdGroup;
	if(agp===undefined||agp.length==0) return;

	for(v=0;v<agp.length;v++){
		var ag=agp[v];
		var p=ag.placementDetails;
		var a=ag.initArgs;
		var gid=p[4];
		var noAdd=Admeta.MatC.full===undefined||Admeta.MatC.full(Admeta.MatC.getExisting(gid));

		if (!noAdd) Admeta.MatC.addTextInit(gid,a);
		at.initCached.apply(this, a);

		if (at.forceCenterImg!==undefined) at.params.centerImg=at.forceCenterImg;
		if (at.forceSize!==undefined) at.params.startSz=at.forceSize;
		if (at.forceBaseFont!==undefined) at.params.font=at.forceBaseFont;
		if (at.forceBaseFontSize!==undefined) at.params.bfs=at.forceBaseFontSize;
		at.params.Operation=ag.Operation;

		for(i=0;i<ag.Ads.length;i++){
			var ad=ag.Ads[i];
			this.createCached.apply(this,ad);
			if (!noAdd) Admeta.MatC.addTextAd(gid,ad);
		}
		
		var xfT=typeof ag.xRenderF;

		if(xfT!="undefined"){
			if(xfT=="string")
				ag.xRenderF={cb:eval(ag.xRenderF), force:false};
			else
				ag.xRenderF={cb:ag.xRenderF, force:true};
		}
		
		if(typeof(AdmetaSkipCustomRender)!="undefined"&&AdmetaSkipCustomRender) ag.xRenderF=undefined;
		
		var realArgs=[p[0],p[1],p[2],p[3],p[4],ag.xRenderF,p[5],p[6]];

		if (!noAdd) Admeta.MatC.addTextInsert(gid,realArgs);

		switch (ag.Operation){
			case 0:at.insertCached.apply(at,realArgs);break;
			case 1:at.writeCached.apply(at,realArgs);break;
		}
	}
	at.AdGroup=[];
	at.forceCenterImg=undefined;
	at.forceSize=undefined;
	at.forceBaseFont=undefined;
	at.forceBaseFontSize;
};

Admeta.Text.ProcessTextAds();

