var setbkcolor = false;
var markanchor = false;
var anchored = null;
var anchororder = null;
var maxlevel = 80;

var TAG_LN = "<BR>";
function _nextPosition(temp, tempI)
{
	while( temp.substr( tempI,TAG_LN.length ) == TAG_LN.toUpperCase() ){
		tempI = tempI + TAG_LN.length;
	}
	return tempI;
}
var COMMENT_HEAD = "<!--";
var COMMENT_TAIL = "-->";

function nextPosition(temp, tempI)
{
	while( tempI < temp.length && temp.charAt(tempI) == '<' ){
		var right;
		if( temp.substr( tempI,COMMENT_HEAD.length) == COMMENT_HEAD ){
			var rSub = temp.substr( tempI + COMMENT_HEAD.length );
			right = rSub.indexOf( COMMENT_TAIL );
			if( right == -1 ){	   
				right = rSub.indexOf('>');
				if(right == -1 ){   
					tempI = temp.length;
					break;
				}
				tempI = right + 1 + tempI + COMMENT_HEAD.length;
			}else{		
				tempI = right + COMMENT_TAIL.length + tempI + COMMENT_HEAD.length;
			}
		}else{
			var rSub = temp.substr(tempI+1);
			right = rSub.indexOf('>');
			if( right == -1 ){	break;	}				
			var left = rSub.indexOf('<');
			if( left == -1 ){	// <>
				tempI = right+ 1 + tempI + 1;
				continue;
			}
			if( left > right ){	//<><
				tempI = right + 1 + tempI + 1;
				continue;
			}
			break;
		}// if ... else 
	}// while
	return tempI;
}


function indexOfWord(temp, kw, p, n)
{
	var tempI, i , j ;

	for( i= nextPosition(temp,p) ; i< temp.length - kw.length + 1; i=nextPosition(temp, i+1 ) ){
		tempI = i;
		for(j=0; j<kw.length; j++){
			tempI = nextPosition(temp, tempI);
			if(  temp.charAt(tempI) != kw.charAt(j) ){
				break ;
			}
			if( j< kw.length -1 )
				tempI = nextPosition(temp, tempI+1 );
		}
		if( j == kw.length){
			var ch = /\w/;
			if( ( i != 0 && ch.test(temp.charAt(i-1)) && temp.charAt(i-1)!='_' 
					&& ch.test(temp.charAt(i)) && temp.charAt(i)!='_') ||
				( tempI < temp.length-1 && ch.test(temp.charAt(tempI+1)) && temp.charAt(tempI+1)!='_' 
					&& ch.test(temp.charAt(tempI)) && temp.charAt(tempI)!='_') )
			{
				return indexOfWord( temp, kw, tempI+1, n  );
			}
			return i;
		}
	}
	return -1;
	
}

function render(str, colors, tp2)
{
	var color;
	var tp = anchororder[tp2-1];
	if (colors instanceof Array)
		color = colors[tp%colors.length];
	else
		color = colors;
	if (anchored != null && !anchored[tp]) {
		anchored[tp] = true;
		str = str.anchor("keyword"+(tp));
	}
	
	if (!setbkcolor)
		return str.fontcolor(color);
	else
		return '<span style="background-color=' + color + '"><b>' + str + '</b></span>'
}

function setColor(obj, keywords, colors)
{
	var text = obj.innerHTML;
	var flag = new Array(text.length);
	var matched = new Array(keywords.length);
	var i,j;
	for(j=0; j<flag.length; j++)
		flag[j] = 0;
	for(i=0; i<keywords.length; i++)
	{
		if( keywords[i].length <=0 ) continue;
		if(keywords[i].length == 1 && keywords[i].charCodeAt(0)>0xff)
		{
			var sub_matched = false;
			for(j=0; j<i; j++)
				if(matched[j] && keywords[j].indexOf(keywords[i], 0) != -1)
				{
					sub_matched = true;
					break;
				}
			if(sub_matched)
				continue;
//			for(j=1; j<text.length-1; j++) {
//				if ((flag[j-1] || flag[j+1]) && text.charAt(j) == keywords[i]) {
//					flag[j] = i+1;
//					sub_matched = true;
//				}
//			}
//			if (text.length>=2 && flag[text.length-2] && text.charAt(text.length-1) == keywords[i]) {
//				flag[text.length-1] = i+1;
//				sub_matched = true;
//			}
//			if (text.length>=2 && flag[1] && text.charAt(0) == keywords[i]) {
//				flag[0] = i+1;
//				sub_matched = true;
//			}
//			if(sub_matched)
//				continue;
		} 
		var p = 0, q = 0;
		matched[i] = false;
		while( (q = indexOfWord(text.toUpperCase(), keywords[i].toUpperCase(), p, maxlevel)) != -1)
		{
			var tq = q +1;
			for(j=1; j < keywords[i].length; j++){
				tq = nextPosition( text, tq );
				tq = tq+1;
			}
			for( j = q;j< tq;j++)
				flag[j] = i+1;
			q = tq;
			p = q + 1;
			matched[i] = true;
		}
	}
	
	var result = "";
	var bg = 0;
	var tp = flag[0];
	var i;
	for(i=0; i<text.length; i++) {
		if(flag[i] == tp)
			continue;
		else {
			if (tp != 0)
				result += render(text.substr(bg, i - bg), colors, tp);
			else
				result += text.substr(bg, i - bg);
			tp = flag[i];
			bg = i;
		}
	}
	if (tp != 0)
		result += render(text.substr(bg, i - bg), colors, tp);
	else
		result += text.substr(bg, i - bg);

	obj.innerHTML = result;
}

function _setAllColor(root, classname, keywords, color) {
	for (var i = 0; i < root.childNodes.length; ++i) {
		var obj = root.childNodes[i];
		if (obj.name=="setcolor_area"||obj.id=="setcolor_area") {
			setColor(obj, keywords, color)
		} else if (obj.hasChildNodes()) {
			_setAllColor(obj, classname, keywords, color);
		}
	}
}

/**
 * 给keyword标记给定的颜色  
 * 同时给每个keyword的第一次出现标记anchor，anchor的名字为#keyword1 #keryword2 ...
 */
function setAllColor(classname, keywords, color) {
	if ( anchororder == null )
	{
		anchororder = new Array(keywords.length);
		for (var i = 0; i < anchororder.length; ++i) 
		{
			anchororder[i] = i;
		}
	}	
		
	for(var i=0; i<keywords.length; i++)
	{
		var k = i;
		for(var j=k+1; j<keywords.length; j++)
			if(keywords[k].length < keywords[j].length)
				k = j;
		var temp = keywords[k];
		keywords[k] = keywords[i];
		keywords[i] = temp;
		var temp2 = anchororder[k];
		anchororder[k] = anchororder[i];
		anchororder[i] = temp2;
	}
	_setAllColor(document.documentElement, classname, keywords, color);
}

function setAllBkColor(classname, keywords, color, makeanchor) {
	setbkcolor = true;
	if (anchored == null && makeanchor == true) {
		anchored = new Array(keywords.length);		
		for (var i = 0; i < anchored.length; ++i) {
			anchored[i] = false;			
		}
	}
	setAllColor(classname, keywords, color);
	setbkcolor = false;
}
