var _it = {
	tad : "tad" + new Date().getTime(),
	local : __it.local,
	host : __it.host ? __it.host : "adcentre.magnetisemedia.com",
	clickTag : __it.click,
	pub : null,
	resp : null,
	ads : null,
	waitCount: 0,
	setPublisher : function(pub) {
		this.pub = pub;
		this._write(this.resp);
	},
	writeAds : function(resp) {
		this.resp = resp;
		var pub = resp.publisher;
		var src;
		if (this.local) {
			src = "http://" + this.host + "/li/ad?jsonp=_it.setPublisher&layoutType=publisher_text&layoutId=" + pub.id;
		} else {
			src = 'http://cloud.leadintelligence.co.uk/a/publisher-text/'
					+ pub.id
					+ '/'
					+ pub.jsonp
					+ '.jsonp?jsonp=_it.setPublisher&layoutType=publisher_text&layoutId='
					+ pub.id + '';
		}
		document.write('<scr' + 'ipt src="' + src + '"></scr' + 'ipt>');
	},
	nvl: function(num, def) {return num ? num : def;},
	_write : function(resp) {
		var adtag = document.getElementById(this.tad);
		var pub = this.pub;
		var ads = resp.ads;
		this.ads = ads;
		for (var i = 0; i < ads.length; i++) {
			var ad = ads[i];

			var s = document.createElement("script");
			this.waitCount++;
			if (this.local) {
				s.src = "http://" + this.host + "/li/ad?jsonp=_it.writeAd&layoutType=static_text&layoutId="
						+ ad.id;
			} else {
				s.src = "http://cloud.leadintelligence.co.uk/a/static-text/"
						+ ad.id
						+ "/"
						+ ad.jsonp
						+ ".jsonp?jsonp=_it.writeAd&layoutType=static_text&layoutId="
						+ ad.id;
			}
			adtag.appendChild(s);
		}
	},
	writeAd : function(text) {
		for ( var i = 0; i < this.ads.length; i++) {
			var ad = this.ads[i];
			if (ad.id == text.staticTextLayoutId) {
				ad.text = text;
			}
		}
		this.waitCount--;
		if (this.waitCount == 0) {
			this.display();
		}
	},
	display : function() {
		var adtag = document.getElementById(this.tad);
		var html = this.pub.htmlTemplate;
		if (!html) html = "";
		for (var i = 0; i < this.ads.length; i++) {
			var idx = i + 1;
			var layout = this.ads[i];
			var ad = layout.text;
			var link = "http://formregistration.com/register/" + ad.adId + "/" + ad.micrositeId + "/" + this.pub.siteId + "?fromLayoutType=static_text&fromLayout=" + layout.id + "&ba=" + this.pub.adId;
			if (ad.clickUrl) link = ad.clickUrl;
			if (this.clickTag) link = this.clickTag + link;
			var click = "http://" + this.host + "/click?layoutType=static_text" +
					"&layout=" + layout.id +
					"&ad=" + ad.adId +
					"&site=" + this.pub.siteId +
					"&ba=" + this.pub.adId +
					"&url=" + escape(link);

			html = html.replace(new RegExp("\\[TITLE_" + idx + "\\]", "ig"), ad.titleText);
			html = html.replace(new RegExp("\\[LINK_" + idx + "\\]", "ig"), click);
			html = html.replace(new RegExp("\\[BODY_" + idx + "\\]", "ig"), ad.bodyText)
			html = html.replace(new RegExp("\\[CTA_" + idx + "\\]", "ig"), ad.ctaText)
			html = html.replace(new RegExp("\\[IMAGE_" + idx + "\\]", "ig"), ad.miniImage);
			html = html.replace(new RegExp("\\[IMAGE_64_32_" + idx + "\\]", "ig"), ad.image64x32);
		}		
		adtag.innerHTML = html;
	}
	
};

document.write("<div id='" + _it.tad + "'></div>");
document
		.write('<scr'
				+ 'ipt src="http://' + (__it.host?__it.host:'adcentre.magnetisemedia.com') + '/track?layoutType=publisher_text&layoutId='
				+ __it.publisherLayout
				+ '&jsonp=_it.writeAds'
				+ (__it.textLayouts ? '&overrideLayouts=' + __it.textLayouts
						: '') + '"></scr' + 'ipt>');

