// ------------
// Library used to analyse keywords / C-2007 / Gabriel Klein / AdsClick
// ------------

var timeObjectCreationR3546R = (new Date()).getTime();

if (document["ACCommon"] != "1.0") {
document["ACCommon"] = "1.0";

if (document["ACDebug"] == null) {
	document["ACDebug"] = false;
}

//--
function ACCommon() {
	//
}

// Check that this browser is supported
ACCommon.prototype.isSupported = function() {
	var isDOM=document.getElementById?true:false;
	if (!isDOM) {this.info("This browser is not supported");return false};
	return true;
}

ACCommon.prototype.addScript = function(url, doReload) {
	if (doReload) url = url+"&_rnd="+Math.random();
	ACCommon.prototype.info("Load URL: "+url);
	var head  = document.getElementsByTagName("head")[0];
	var myScript = document.createElement("script");
	myScript.type = "text/javascript";
	myScript.src  = url;	
	head.appendChild(myScript);
}


// Analyse the DOM. Return true if model is ready or browser is not supported, false if model is not ready.
ACCommon.prototype.isDomReady = function() {

	var body  = document.getElementsByTagName("body");
	if (body == null || body.length==0) {this.info("DOM Body is not ready");return false;}	
	var head  = document.getElementsByTagName("head");
	if (head == null || head.length==0) {this.info("DOM Head is not ready");return false;}	

	if (ACCommon.prototype.isIE()) {
		var rs = document.readyState;
		if (rs == "loaded") {
			return true;
		}
	}
	
	var t = (new Date()).getTime() - timeObjectCreationR3546R;
	
	// Wait 200 ms, so we are +/- sure that the dom exists
	if (t < 300) return false;	
	return true;
}

ACCommon.prototype.isIE = function() {
	var ua = window.navigator.appName;
	var isIE = ua.indexOf( "Microsoft Internet Explorer" ) >= 0;
	return isIE;
}

// Debug
ACCommon.prototype.info = function(message) {
	if (!document["ACDebug"] || console==null) return;
	console.info(message);
}


//--
}
// ------------
// Library used to analyse keywords / C-2007 / Gabriel Klein / AdsClick
// ------------

document.spellcheck=true;

if (document["ACAnalyseVersion"] != "1.0") {
document["ACAnalyseVersion"] = "1.0";

//--
function ACKeyword(keyword) {
	this.canUnderline = false;
	this.count = 0;
	this.boost = 0;
	this.isProposed = false;
	this.keyword=keyword;
}

ACKeyword.prototype.add = function(boost) {
	this.count = this.count+1;
	this.boost = this.boost+boost;
}

// ------------

function ACAnalyse(proposedKeywords) {

	this.MINSIZE = 3;
	this.MAXSIZE = 50;
	this.MAXDOMTIMEMS = 3000;
	this.proposedKeywords = proposedKeywords;
	this.proposedKeywordsMap = new Object();
	for (var i=0; i<this.proposedKeywords.length; i++) this.proposedKeywordsMap[this.proposedKeywords[i].toLowerCase()]="*";
		
	this.debug = true;
	var o = new Object();
	o["a"] = o["h1"] = "*";
	this.noUnderline = o;
	o = new Object();
	o["script"] = o["noscript"] = o["textarea"] = o["embed"] = o["object"] = o["img"] = o["param"] = "*";
	this.noEnter = o;
	o = new Object();
	o["h1"] = 10; o["h2"] = 7; o["h3"] = 6; o["h4"] = o["h5"] = 5; o["a"] = 5; o["b"] = 4; o["i"] = 2; o["small"] = 0.5; 
	this.boost = o;
	this.boostTitleAndMeta = 10;
	this.regSplit = new RegExp("[\\|\\,\\.\\-\\;\\:\\!\\?\\'\\`\\^\\~\\+\\\"\\*\\/\\(\\)\\[\\]\\{\\} \\n\\r\\t\\_\\+\\%\\<\\>\\=]", ""); //g
	this.keywords = new Object();
		
	this.maxAnalyseNode = 10000;
	this.timeObjectCreation = (new Date()).getTime();
	
}

ACAnalyse.prototype.processHeadBody = function(onlyUnderline, countKeywords) {

	ACCommon.prototype.info("Analyse document");
	var s = (new Date()).getTime();
	this.analyseHead();
	// Check if we have a special span
	var spans = document.getElementsByTagName("contextual");
	var isParsed = false;
	var result;
	for (var i=0; i<spans.length; i++) {
		var span = spans[i];
		result = this.analyseNode(s, span, false, 1);
		isParsed=true;
	}
	spans = document.getElementsByTagName("span");
	for (var i=0; i<spans.length; i++) {
		var span = spans[i];
		if (span.id=="contextual" || span.name=="contextual") {
			result = this.analyseNode(s, span, false, 1);
			isParsed=true;
		}
	}


	if (!isParsed) {
		result = this.analyseNode(s, document.getElementsByTagName("body")[0], false, 1);
	}
	var e = (new Date()).getTime();
	ACCommon.prototype.info("Analysed "+(result?"full":"partial")+" document in "+(e-s)+" ms");
}


// Analyse the DOM.
// Add "true" if you are only interested by keywords that could be underlined.
// Return a list of sorted keywords.
ACAnalyse.prototype.analyse = function(onlyUnderline, countKeywords) {

	var body  = document.getElementsByTagName("body")[0];
	if (body == null) {ACCommon.prototype.info("DOM Body is not ready");}
	var e1 = (new Date()).getTime();

	this.processHeadBody(onlyUnderline, countKeywords);
	
	var sortedList = this.sortKeywords(onlyUnderline, countKeywords);
	// Some cleanup
	this.proposedKeywords = null;
	this.proposedKeywordsMap = null;

	this.keywords = null;

	var e2 = (new Date()).getTime();
	ACCommon.prototype.info("Keywords sorted in "+(e2-e1)+" ms");
	
	return sortedList;
}

ACAnalyse.prototype.analyseAndGetUrl = function(onlyUnderline, baseUrl) {
	var ua = window.navigator.appName;
	var maxURL = ( ua==null || ua.indexOf( "Microsoft Internet Explorer" ) >= 0) ? 2000: 3800;
	var countKeywords = Math.round((maxURL-baseUrl.length)/5);
	var sortedList = this.analyse(onlyUnderline, countKeywords);
	var ctx = "&ctx=";
	var ctxsafe = "&ctxsafe=";
	for (var i=0; i<sortedList.length && baseUrl.length+ctx.length+ctxsafe.length<maxURL; i++) {
		var kw = sortedList[i];
		if (baseUrl.length+ctx.length+ctxsafe.length+kw['keyword'].length>=maxURL) break;
		if (kw.isProposed)
			ctx=ctx+","+escape(kw['keyword']);
		else
			ctxsafe=ctxsafe+","+escape(kw['keyword']);
	}	
	
	var nu = baseUrl+ctx+ctxsafe;	
	return nu;

}

ACAnalyse.prototype.sortKeywords = function(onlyUnderline, numberOfKeywords){

	var mapPerBoostNormal = new Array(101);
	var mapPerBoostProposed = new Array(101);
	for (var i=0; i<=100; i++)  {mapPerBoostNormal[i]=new Array();mapPerBoostProposed[i]=new Array();};

	for (keyword in this.keywords) {
		keyword = keyword+"";
		var stats =  this.keywords[keyword];
		if (!onlyUnderline || stats.canUnderline) {
			ACCommon.prototype.info(keyword+" ("+stats.keyword+") =  UL:" + stats.canUnderline+" CNT:"+stats.count+" Boost:"+stats.boost+" Proposed:"+stats.isProposed);
			var weight = Math.round(stats.boost);
			if (weight<0) weight=0;
			if (weight>100) weight=100;
			if (stats.isProposed) 
				mapPerBoostProposed[weight][mapPerBoostProposed[weight].length]=stats;
			else
				mapPerBoostNormal[weight][mapPerBoostNormal[weight].length]=stats;
		}
	}

	var res = new Array();
	for (var i=100; i>=0 &&res.length<numberOfKeywords; i--) {
		var e = mapPerBoostProposed[i];
		for (var j=0; j<e.length &&res.length<numberOfKeywords; j++) {
			res[res.length] = e[j];
		}
	}
	for (var i=100; i>=0&&res.length<numberOfKeywords; i--) {
		var e = mapPerBoostNormal[i];
		for (var j=0; j<e.length &&res.length<numberOfKeywords; j++) {
			res[res.length] = e[j];
		}
	}
	
	return res;
}

// Return 2 => no enter, 1 => no underling, 0 => 
ACAnalyse.prototype.getTagType = function(tagType){
	tagType = tagType.toLowerCase();
	if (this.noEnter[tagType] == "*") return 2;
	if (this.noUnderline[tagType] == "*") return 1;
	return 0;
}

ACAnalyse.prototype.analyseNode = function(timeStarted, b, isNoUnderline, boost) {

	this.maxAnalyseNode=this.maxAnalyseNode-1;
	if (this.maxAnalyseNode<0) {ACCommon.prototype.info("Done maximum call of analyse node");return false;}
	if ( (new Date()).getTime()-timeStarted>this.MAXDOMTIMEMS) {
		ACCommon.prototype.info("Overtime on dom processing: "+( (new Date()).getTime()-timeStarted)+" ms");
		return false;
	}

	if (b.firstChild != null) {
		for (var i=0; i<b.childNodes.length;i++) {
			if (b.childNodes[i].nodeType==3) {
				var v = b.childNodes[i].nodeValue;
				try {
					i+=this.addText(v, isNoUnderline, boost, false, b, b.childNodes[i], i);
				}
				catch(e) {
					//console.warn(e);
					ACCommon.prototype.info("Error changing dom: "+e);
				}
			}
			else {
				var nodeS = ""+b.childNodes[i].nodeName.toLowerCase();
				var tagType = this.getTagType(nodeS);
				if ( tagType == 0 || tagType == 1) {
					if (b.childNodes[i].nodeName.toLowerCase()=="span"
						&& b.childNodes[i].id == "adsclickad") {
							//
					}
					else {
						var nBoost = Math.round(this.boost[nodeS]);
						if ((""+nBoost)=='NaN') nBoost = 1; 
						if (!this.analyseNode(timeStarted, b.childNodes[i], isNoUnderline||(tagType == 1), nBoost)) {
							return false;
						}
					}
				}
				// else 2

			}
		}
	}
	return true;
}	

ACAnalyse.prototype.analyseHead = function() {

	this.addText(document.title, true, this.boostTitleAndMeta, false, null, null,0);

	var metas  = document.getElementsByTagName("meta");
	for (var i=0; i<metas.length; i++) {
		var meta=metas[i];
		var name=(""+metas[i].name).toLowerCase();
		if (name=="keywords" || name=="description") {
			this.addText(meta.content, true, this.boostTitleAndMeta, name=="keywords",null,null,0);
		}
	}

}

ACAnalyse.prototype.addText = function(text, isNoUnderline, boost, onlyKeywords, parentNode, node, posChild) {
	
	if (text == null) return 0;
	
	text = (""+text.replace(/^\s+|\s+$/, '')); // trim!
	text = (text+"").toLowerCase();
	if (text.length == 0) return 0;
	
	var tax = (text+"").split(this.regSplit);
	var previousKeyword = null;
	var keyword = null;
	
	if (this.proposedKeywords!=null) {
		for (var i=0; i<this.proposedKeywords.length; i++) {
			var kw = (""+this.proposedKeywords[i]).toLowerCase();
			var p = text.indexOf(kw);
			if (p!=-1) {

	            var s=(p<=0?" ":text[p-1]);
        		var e=(p>=text.length?" ":text[p+kw.length]);
        		if (s==null || s.length==0) s=" ";
        		if (e==null || e.length==0) e=" ";
        		if (this.regSplit.test(s) && this.regSplit.test(e)) {
					keyword = this.keywords[kw];
					if (keyword == null) {
						keyword = new ACKeyword(kw);
					}
					keyword.isProposed = true;
					if (!isNoUnderline)
						keyword.canUnderline = true;
					keyword.add(boost);
					this.keywords[kw] = keyword;
					//ACCommon.prototype.info("KW proposed: "+kw+" = "+isNoUnderline+" Boost: "+boost+" Count: "+keyword.count);	
	        	}			
			}
		}
	}

	for (var t=0; t<tax.length;t++) {
		var txt = tax[t]+"";
		if (txt.length>=this.MINSIZE && txt.length<=this.MAXSIZE) {
		
			// Add the keyword, not best perfo
			if (this.proposedKeywordsMap[txt] == null) {
				keyword = this.keywords[txt];
				if (keyword == null) {
					keyword = new ACKeyword(txt);
				}
				if (!isNoUnderline)
					keyword.canUnderline = true;
				keyword.add(boost);
				this.keywords[txt] = keyword;
				//ACCommon.prototype.info("KW single: "+txt+" = "+isNoUnderline+" Boost: "+boost+" Count: "+keyword.count);	
			}
			
			// Add keyword + keyword before
			if (!onlyKeywords && previousKeyword!=null && previousKeyword!=txt) {
				var kw = previousKeyword+" "+txt;
				var p = text.indexOf(kw);
				if (p==-1) {
					kw = previousKeyword+"-"+txt;
					p = text.indexOf(kw);
				}
				if (p!=-1 && this.proposedKeywordsMap[kw] == null) {
					keyword = this.keywords[kw];
					if (keyword == null) {
						keyword = new ACKeyword(kw);
					}
					if (!isNoUnderline)
						keyword.canUnderline = true;
					keyword.add(boost);
					this.keywords[kw] = keyword;
					//ACCommon.prototype.info("KW multi: "+kw+" = "+isNoUnderline+" Boost: "+boost+" Count: "+keyword.count);	
				}
			}
			
			previousKeyword = txt;
		}
	}
	return 0;
}

// ------------
// Class used to replace keywords
// ------------
function AKeywordsReplace(proposedKeywords, functionReplaceHTML, maxUnterlineTime, maxSameKeywordUnderline) {

	this.myACAnalyse = new ACAnalyse(proposedKeywords);
	// Maximum number of element we underline
	this.myACAnalyse.maxUnderlineTime = maxUnterlineTime;
	// Maximum time we could underline the same keyword
	this.myACAnalyse.maxSameKeywordUnderline = maxSameKeywordUnderline;
	this.myACAnalyse.countByKeywords = new Object();
	this.myACAnalyse.countTotal = 0;
	this.myACAnalyse.replaceHTML = functionReplaceHTML;
	this.myACAnalyse.myReplacedSpan = this.myReplacedSpan = new Object();
	this.myACAnalyse.insertAfter = function(parent, newnode, referenceNode) {
		if (referenceNode.nextSibling == null) {
			parent.appendChild(newnode);
		}
		else {
			parent.insertBefore(newnode, referenceNode.nextSibling);
		}
	}
	this.myACAnalyse.addText = function(text, isNoUnderline, boost, onlyKeywords, parentNode, node, posChild) {
	
		if(isNoUnderline || node == null || parentNode == null)
			return 0;
		if (this.countTotal>this.maxUnderlineTime)
			return 0;
		var text2 = text.toLowerCase();
		
		for (var i=0; i<this.proposedKeywords.length; i++) {
			kw = (""+this.proposedKeywords[i]).toLowerCase();
			var count = this.countByKeywords[kw];
			if (count == null) count=0;
			if (count<this.maxSameKeywordUnderline) {
				var p = text2.indexOf(kw);
				if (p!=-1) {

		            var s=(p<=0?" ":text2[p-1]);
	        		var e=(p>=text2.length?" ":text2[p+kw.length]);
	        		if (s==null || s.length==0) s=" ";
   		     		if (e==null || e.length==0) e=" ";
    	    		if (this.regSplit.test(s) && this.regSplit.test(e)) {
						this.countByKeywords[kw] = count+1;
						this.countTotal = this.countTotal+1;
						if (this.countTotal>this.maxUnderlineTime)
							return 0;
						var kwWithCase = text.substring(p,p+kw.length);
						var before = document.createTextNode(text.substring(0,p));
						var span = document.createElement('span');
						span.innerHTML = this.replaceHTML(kwWithCase);
						span.id="adsclickad";
						var after = document.createTextNode(text.substring(p+kw.length));
												
						this.insertAfter(parentNode, after, node);
						this.insertAfter(parentNode, span, node);
						this.insertAfter(parentNode, before, node);
						parentNode.removeChild(node);
						
						node = before;
						nodeChanged = 1;
						return -1;

						if (this.myReplacedSpan[kw]==null) this.myReplacedSpan[kw]=new Array();
						this.myReplacedSpan[kw][this.myReplacedSpan[kw].length]=span;
					}
				}
			}
		}

		return 0;
	}
}

AKeywordsReplace.prototype.replaceKeywords = function() {
	this.myACAnalyse.processHeadBody(true, 100000);
}


//--
}
// Smartlink first call - Copyright (c) 2007 www.ads-click.com

var parsedR3546R = false;

var ackwiR3546R=new Array("");
var ackwR3546R =new Array();


{
	var anaR3546R = new ACAnalyse(ackwR3546R, ackwiR3546R);
	function doItR3546R() {
		if (ACCommon.prototype.isDomReady()) {
			var r = anaR3546R.analyseAndGetUrl(true, "http://adserver.mynet.com/AdServer?&format=a_smartlink2b&id=38567_2640&&rndid=R3546R&uts=1190188231332&AC&");
			ACCommon.prototype.addScript(r, true);
			anaR3546R = null;
		}
		else {
			ACCommon.prototype.info("Dom is not yet ready yet");
			setTimeout("doItR3546R()",100);
		}
	}
	if (ACCommon.prototype.isSupported()) {
		doItR3546R();
	}
}
