var html_traversal = function (_html) {
	this.html = _html;
	this.htmlLength = _html.length;
	this.pos = 0;
	this.expected_type = 1;
	this.read_failed_times = 0;
	this.result = '';
	this.lastResultIndex = 0;	
	this.keys = [];
	this.usedKeys = [];	
	this.in_string = false;
	this.string_end = '';
	this.in_link = 0;
	//this.url_prefix = 'http://www.21food.com/api/adinbuilt.jsp?key=';
	this.url_prefix = 'http://products.21food.com/a-';
	this.aClassName = '';
	this.aStyle = '';
	this.aAttrs = '';
};

html_traversal.prototype.trav = function (){
	if(this.aClassName && this.aClassName.length>0 ) {
		this.aAttrs += ' class="'+ this.aClassName +'"';
	}
	if(this.aStyle && this.aStyle.length>0){
		this.aAttrs += ' style="'+ this.aStyle +'"';
	}
	for (this.pos=0; this.pos < this.htmlLength;) {
		if(this.read_failed_times>2){			
			this.read_failed_times = 0;
			this.pos ++;
		}
		if(this.expected_type==1) {
			var tag = this.readTag();
			if(tag){
				if(tag.tagName=='a' || tag.tagName=='A') {
					if(tag.tagType==1){
						this.in_link = 1;
					}else if(tag.tagType==2){
						this.in_link = 0;						
					}
				} 			
				this.read_failed_times = 0;
				this.expected_type = 1;
			} else {				
				this.read_failed_times ++;
				this.expected_type=0;
			}
		} else if(this.expected_type==0) {
			this.saveAs();
			var text = this.readText(this.read_failed_times);
			if(text && text.length>0){
				if(!text.isSpace() && this.in_link==0 && this.usedKeys.length<16) {
					text = this.replaceKeys (text);
				}
				this.result += text;
				this.lastResultIndex = this.pos;				
				this.read_failed_times = 0;
				this.expected_type = 1;				
			} else if(text==null) {
				this.read_failed_times ++;
				this.expected_type=1;
			}
		}
	}
	this.saveAs();
};

html_traversal.prototype.saveAs = function (){
	if(this.lastResultIndex < this.htmlLength && this.pos >= this.lastResultIndex) {
		var s = this.html.substring(this.lastResultIndex, this.pos);
		this.result += s;
		this.lastResultIndex = this.pos - 1;
	}
}

html_traversal.prototype.readText = function (){
	var limit = 0, oldpos = this.pos, cc, e;
	if(arguments.length>0) limit = parseInt(arguments[0]);
	if(isNaN(limit)) limit = 0;	
	if(limit >= this.htmlLength - this.pos) {
		this.pos = this.htmlLength;
	} else {
		if(limit>0){
			this.pos = this.pos + limit;
		}
		cc = this.readTextUntil ('<');
	}
	e = Math.min(this.pos, this.htmlLength);
	if( e == oldpos && e < this.htmlLength){
		return null;	
	} else if ( e > oldpos) {
		return this.html.substring(oldpos,e);
	} else {
		return '';
	}
};

html_traversal.prototype.readTag = function (){
	var oldpos = this.pos;
	if(this.pos >= this.htmlLength) return -1;
	var e,i,c,tagName,tagAttrs,tagType = 1; 

	if('<' == this.html.charAt(this.pos)) {		
		this.pos ++;
	} else {
		return 0;
	}
	if('/' == this.html.charAt(this.pos)) {		
		this.pos ++;
		tagType = 2;
	}
	i = this.pos;	
	while (this.pos<this.htmlLength && this.isTagNameChar(this.html.charAt(this.pos)) ){
		this.pos ++;
	}
	if (this.pos > i) {
		tagName = this.html.substring(this.pos,i);		
	} else {
		this.pos = oldpos;
		return 0; 
	}
	i = this.pos;
	var cc = this.readTagToGT ();	
	if(cc>0){
		if('/' == this.html.charAt(this.pos-2)){
			tagType = 3;
			e = this.pos - 2;
		} else {
			e = this.pos - 1;
		}
	} else {
		e = Math.min(this.pos, this.htmlLength);
		tagType = 4; 
	}
	tagAttrs = this.html.substring(i,e);
	return {'tagType':tagType,'tagName':tagName,'tagAttrs':tagAttrs};
};
html_traversal.prototype.readTagToGT = function () {	
	var toc = '>';	
	var oldpos = this.pos;
	for (;this.pos<this.htmlLength;this.pos++) {
		var c = this.html.charAt(this.pos);
		if(c==toc && !this.in_string){
			this.pos ++;
			return this.pos - oldpos;
		} else if (this.in_string && c=='\''){
			this.pos ++;
		} else if (c=='\'' || c=='"'){
			if(this.in_string && c==this.string_end) {
				this.in_string = false;
			} else if(!this.in_string) {
				this.in_string = true;
				this.string_end = c;
			}
		}
	}
	return -1 - oldpos;
};

html_traversal.prototype.readTextUntil = function (toc) {	
	var oldpos = this.pos;
	for (;this.pos<this.htmlLength;this.pos++) {
		var c = this.html.charAt(this.pos);
		if(c==toc){
			return this.pos - oldpos;
		} 
	}
	if(this.pos>=this.htmlLength) return this.htmlLength - oldpos;
	return -1 - oldpos;
};
html_traversal.prototype.getNext = function (){
	if(this.pos+1<this.htmlLength)return this.html.charAt(this.pos+1);else return null;
};
html_traversal.prototype.isTagNameChar = function (c){
	if ( (c>='a' && c<='z') || (c>='0'&&c<='9') || (c>='A'&&c<='Z') || (c==':') || (c=='!') || (c=='-') || (c=='_') ) return true;
	else return false;
};
html_traversal.prototype.isSpaceChar = function (c) {
	if ( c==' ' || c=='\n' || c=='\r' || c=='\t') {return true;} else {return false;}
};

html_traversal.prototype.replaceKeys = function (text) {
	var li = 0, ntext = '';
	for (i=0;i<this.keys.length;i++) {
		var key = this.keys[i];
		if(key.length<1) continue;
		var k = text.indexOf(key, li);
		if(k>=0){
			var href = '<a href="'+ this.url_prefix + escape(key) +'" '+ this.aAttrs +' target="_blank">'+ key +'</a>';
			ntext = text.substring(0, k);
			ntext = ntext + href;
			ntext = ntext + text.substring(k+key.length);
			text = ntext;
			li = k + href.length;
			this.keys[i] = '';
			this.usedKeys.push(key);
		}
	}
	return text;
}
String.prototype.isSpace = function (){
	return /^[ \n\r\t]*$/img.test(this);
}
function __ad_inbuilt () {
	if(typeof(__keys__array)!='undefined' && typeof(__adinbuilt_elemid)!='undefined') {
		var __div = document.getElementById(__adinbuilt_elemid);
		if(__div && __div.innerHTML){
			var __o  = new html_traversal (__div.innerHTML);
			if(typeof(__adinbuilt_aclass)!='undefined') {
				__o.aClassName = __adinbuilt_aclass;
			}
			if(typeof(__adinbuilt_astyle)!='undefined') {
				__o.aStyle = __adinbuilt_astyle;
			}
			__o.keys = __keys__array;__o.trav();__div.innerHTML = __o.result;
		}
	}
}
document.write('<scr'+'ipt language="javascript" src="http://www.21food.com/api/adinbu'+'iltkeys.js"></scr'+'ipt>');
