(function(){
	var ad="_adl"; var query;
	_ = {
		client_id:"adl-000",
		flash:false,
		cookie:false,
		adla:0,
		adlhid:0,
		adlite_elements:[],
		type:"text",
		target:0
	}
	_.getAds = function() {
		b=window;
		checkAdlCookies();
		addToQuery("client_id", b.adlite_client_id);
		addToQuery("flash", getFlashVersion().toString());
		addToQuery("cookie", b.adlGlobal.from_cookie);
		addToQuery("adla", b.adlGlobal.vid);
		addToQuery("adlhid", b.adlGlobal.hid);
		addToQuery("type", b.adlite_type);
		addToQuery("target", b.adlite_target);
		writeAdsScript();
	}
	function addToQuery(key, value) {
		if(!value)
			value=getAdliteString(key);
		if(!query && value)
			query = "?"+key+"="+value
		else if(value)
			query += "&"+key+"="+value
	}
	function getAdliteString(key) {
		return _[key] || null;
	}
	function encodeUri(a)
	{
		return typeof encodeURIComponent=="function"?encodeURIComponent(a):escape(a)
	}
	function checkAdlCookies() {
		var cookie = document.cookie,
		a = Math.round((new Date).getTime() / 1000),
		hasCookieA = cookie.indexOf("_adla=") > -1,
		hasCookieB = cookie.indexOf("_adlb=") > -1,
		hasCookieC = cookie.indexOf("_adlc=") > -1,
		f,
		g = {};
		if (hasCookieA) {
			// Separate out the numbers of a utma-style cookie. Example:
			// 173272373.449341548.1193258541.1194650238.1195068298.3
			//
			f = cookie.split("_adla=")[1].split(";")[0].split(".");
			// If not all cookies exist, use new value.
			g.sid = (!hasCookieB || !hasCookieC ? a : f[1]) + "";
			g.vid = f[0] + "." + g.sid;
			g.from_cookie = true;
		} else {
			g.sid = window && window.adlGlobal && window.adlGlobal.sid ?
			window.adlGlobal.sid : a + "";

			g.vid = window && window.adlGlobal && window.adlGlobal.vid ?
			window.adlGlobal.vid : Math.round(Math.random()*2147483647) + "." + a;

			g.from_cookie = false;
		}

		g.hid = window && window.adlGlobal && window.adlGlobal.hid ?

		window.adlGlobal.hid : Math.round(Math.random()*2147483647);

		window.adlGlobal = g;
		setCookie("_adla", g.vid, (730*60*24), '/', '', '');
		setCookie("_adlb", g.sid, 30, '/', '', '');
		setCookie("_adlc", g.sid, null, '/', '', '');
		return g;
	}
	function getFlashVersion(){
		return"0"
	}
	function insertBanner(banner) {
		d = document;
		url = 'http://tracker.adlite.se/track?client_id='+window.adlite_client_id+'&adla='+window.adlGlobal.vid+'&target='+banner.t+'&url='+banner.l;
		e = d.getElementById(adlite_elements[0]);
		a = d.createElement("a");
		i = d.createElement("img");
		a.setAttribute('href', url);
		i.setAttribute('src',banner.i);
		i.setAttribute('style', 'width:'+banner.w+'px;height:'+banner.h+'px');
		a.appendChild(i);
		e.appendChild(a);
	}
	function linkWords(words) {
		elements = adlite_elements;
		for(i=0;i<elements.length;i++) {
			if(elements[i].substr(0,1)==".") {
				c = getElementsByStyleClass(elements[i].substr(1));
				for(j=0;j<c.length;j++) {
					for(k=0;k<words.length;k++)
						replaceWord(c[j], words[k]);
				}
			}
			else if(elements[i]=="body") {
				for(k=0;k<words.length;k++)
					replaceWord(document.body, words[k]);
			}
			else {
				for(k=0;k<words.length;k++) 
					replaceWord(document.getElementById(elements[i]), words[k]);
			}
		}
	}
	function replaceWord(el,ol) {
		url = 'http://tracker.adlite.se/track?client_id='+window.adlite_client_id+'&adla='+window.adlGlobal.vid+'&url='+ol.l;
		el.innerHTML=el.innerHTML.replace(new RegExp("( )("+ol.w+")( |,|\\.)", 'i'), '$1<a href="'+url+'">$2</a>$3');
	}

	function getElementsByStyleClass (className) {
		var all = document.all ? document.all :
		document.getElementsByTagName('*');
		var elements = new Array();
		for (var e = 0; e < all.length; e++)
			if (all[e].className == className)
				elements[elements.length] = all[e];
		return elements;
	}

	function setCookie(name, value, expires, path, domain, secure)
	{
		var today = new Date();
		if ( expires )
			expires = expires * 1000 * 60;
		var expires_date = new Date(today.getTime() + expires);
		document.cookie = name + "=" +value+
		(expires ? ";expires=" + expires_date.toGMTString() : "" ) +
		(path ? ";path=" + path : "" ) +
		(domain ? ";domain=" + domain : "" ) +
		(secure ? ";secure" : "" );
	}

	function writeAdsScript() {
		document.write(unescape("%3Cscript src='http://tracker.adlite.se/ads"+query+"' type='text/javascript'%3E%3C/script%3E"));
	}

	_.linkWords = linkWords;
	_.insertBanner = insertBanner;
	window[ad]=_;
	_.getAds();
})()