/*
举例：
var AD_Keys = [
	{ keyword: '外界', url:'http://www.jongo.com/', title:'天蝎座', attribute:[{keyword: 'Nasdaq', URL: 'http://www.nasdaq.com'}, {keyword: 'Sina', URL: 'http://www.sina.com'}]},
	{ keyword: '受外界环境', url:'http://www.51job.com/', title:'前程无忧', attribute:[{keyword: 'Nasdaq', URL: 'http://www.nasdaq.com'}, {keyword: 'Sina', URL: 'http://www.sina.com'}]},
	{ keyword: '学习', url:'http://www.edu.cn/', title:'教育部', attribute: [{keyword: 'Nasdaq', URL: 'http://www.nasdaq.com'}, {keyword: 'Sina', URL: 'http://www.sina.com'}] }
];
*/
var isIE = (navigator.appName.indexOf("Microsoft")!=-1) ;
var isFull = false;  // 用于判断是否替换文本中的全部匹配关键词 为false时只替换找到的第一个关键词
var contentId = 'article-content';  //需要替换的内容区域

for (var i=0; i<AD_Keys.length; i++) {
	var _kw = AD_Keys[i].keyword;
	var _ih = '<a href="'+ AD_Keys[i].url +'" title="'+ AD_Keys[i].title +'" target="_blank">'+ _kw +'</a>';
	
	_ih += (AD_Keys[i].attribute.length > 0) ? '(' : '';
	for (var j=0; j<AD_Keys[i].attribute.length; j++) {
		_ih += '<a href="' + AD_Keys[i].attribute[j].URL + '" title="'+ AD_Keys[i].attribute[j].title +'" target="_blank">' + AD_Keys[i].attribute[j].keyword + '</a>';
		_ih += (j < AD_Keys[i].attribute.length -1 ) ? ' | ' : ''; 
	}
	_ih += AD_Keys[i].attribute.length>0 ? ')' : '';
	
	if (isIE) { 
		var _tr = document.body.createTextRange();		
		while(_tr.findText(_kw,1,4,"asis")) {
			var _r = _tr.parentElement();
			if(checkAcceptNode(_r, contentId)) {
				_tr.pasteHTML(_ih);
				if (!isFull) break;
			}
			_tr.collapse(false);
		}
	} else {
		Accept_Node = function (node){
			if (checkErrorTagName(node))
				return NodeFilter.FILTER_REJECT;
			else
				return NodeFilter.FILTER_ACCEPT;			
		};
		var divContent = (contentId != '')? document.getElementById(contentId): document.body;
		var tw = document.createTreeWalker(divContent, NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_TEXT, Accept_Node, false);
		var vNodes = new Array();
		var range = document.createRange();
		while (cNode = tw.nextNode()) {
			if(cNode.nodeValue != null) vNodes[vNodes.length]=cNode;			
		};
		for (var j=0; j<vNodes.length; j++) {
			var key = vNodes[j].nodeValue;
			var pos = key.indexOf(_kw);
			if (pos != -1) {
				range.selectNode(vNodes[j]);
				range.setStart(vNodes[j], pos);
				range.setEnd(vNodes[j], range.startOffset+_kw.length);
				range.deleteContents();
				var nNode = document.createElement('span');
				nNode.innerHTML = _ih;
				range.insertNode(nNode);
				if (!isFull) break;
			}
		}
	}
}
function checkAcceptNode(node, contentId) {
	if (node) {
		var parent = node;
		while (parent && parent.tagName.toUpperCase()!="BODY") {
			if (checkErrorTagName(parent)) {
				return false;
			} else if (parent.id == contentId) {
				return true;
			}
			parent = parent.parentElement;
		}
	}
	if (contentId == '') {
		return true;
	} else {
		return false;
	}
}
function checkErrorTagName(node) {
	return (node.tagName=="A" || node.tagName=="IMG" || node.tagName=="INPUT" || node.tagName=="SELECT" || node.tagName=="SCRIPT");
}