var foxforwords = {
	init: function(options){
		// Initialize options
		this.clientId = options.clientId;
		this.wordCount = options.wordCount || 10;
		this.channels = options.channels || '';
		this.encoding = options.encoding || 'utf-8';
		this.style = options.style || false;
		this.elements = options.elements || [document.getElementsByTagName("body")[0]];
		this.newWindow = options.newWindow || false;
		this.page = options.page || window.location.href;
		// Process our hacky cross-site jsonp request
		var jp = +new Date;
		var fn = 'jp'+jp;
		var words = 'http://ad.adfox.cz/ppcbe.js?mb=0&charset='+foxforwords.encoding+'&kanaly='+foxforwords.channels+'&format=666699ffffff3366ff000000339966104&partner='+foxforwords.clientId+'&foxback='+fn+'&_='+jp+'&stranka='+foxforwords.page+'';
		var script = document.createElement("script"), head = document.getElementsByTagName("head")[0] || document.documentElement;
		// Defining temp function
		window[fn] = function(json){
			foxforwords.replace(json);
			// Garbage collect
			window[fn] = undefined;
			try{delete window[fn];} catch(e){}
			if(head){head.removeChild(script);}
		};
		// Adding <script>
	    script.setAttribute("src", words);
	    script.setAttribute("type", "text/javascript");
	    head.appendChild(script);
	},
	replace: function(json){
		var changed = false;
		var counter = 0;
		// Walk the nodes and replcace words
		var nodeIterator = function(element, word){
			var nodes = element.childNodes;
			for(var n in nodes){
				if(!changed){
					var node = nodes[n];
					if(node.nodeType == 3){
						var txt = node.nodeValue;
						if(/\w/.test(txt)){
							var rx = new RegExp("(^|[ \(\);:,.!?\n\t-]+)("+word.replace(/[ ]+/g,"[ \n\t-]+")+")($|[ \(\);:,.!?\n\t-]+)", "i");
							if(rx.test(txt)){
								// Split node
								var slave = txt.replace(rx,"$1!word!$3");
								var midNode = node.splitText(slave.indexOf('!word!'));
								midNode.splitText(word.length);
								// Create link
								var newElement = a = document.createElement('a');
								a.innerHTML = midNode.nodeValue;
								a.setAttribute('href', json[word]);
								if(foxforwords.newWindow){
									a.setAttribute('target', '_blank');
								}
								if(foxforwords.style){
									a.className = foxforwords.style;
								}else{
									// Default anchor style
									a.style.textDecoration = 'underline';
									a.style.paddingBottom = '0px';
									a.style.borderBottom = '1px solid';
									a.onmouseover = function() { this.style.borderBottom = '2px solid'; }
									a.onmouseout = function() { this.style.borderBottom = '1px solid'; }
								}
								// Insert link
								node.parentNode.insertBefore(newElement, midNode);
								node.parentNode.removeChild(midNode);
								counter++;
								changed = true;
							}
						}
					} else if(typeof(node.tagName) != 'undefined'){
						var tag = node.tagName.toLowerCase();
						if(tag != 'a' && tag != 'br' && tag != 'script'){
							nodeIterator(node, word);
						}
					}
				}
			}
		}
		// Words-links cycle
		for(var word in json){
			if(counter < foxforwords.wordCount){
				changed = false;
				for(e in foxforwords.elements){
					if(!changed && !!foxforwords.elements[e]){
						nodeIterator(foxforwords.elements[e], word);
					} else break;
				}
			} else break;
		}
	}
}
