// City-Data.com City Info JS
// Copyright 2007 City-Data.com
// Version 1.1
//

function prepare_urlstring(text,rep) {
    if (rep==null) rep = '-';
    text = text.replace(/([^a-z0-9_-])/gi,' ');
    text = text.replace(/(\s{2,})/g,' ');
    text = text.replace(/\s/g,rep);
    return text;
}

function highlightCity(node,city,type) {
	var res = false;
	var allowchr = ' ,."()-[]<>`:;/?!\'\n';
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
			if (res = highlightCity(node.childNodes[hi_cn],city,type)) break;
		}
	}
	if (node.nodeType == 3) {
		tmpNode = node.nodeValue;
		tmpCity = city;
		if (tmpNode.indexOf(tmpCity) != -1) {
			pn = node.parentNode;
			if (pn.className != "citylink") {
				nv = node.nodeValue;
				ni = tmpNode.indexOf(tmpCity);
				if ((allowchr.indexOf(nv.charAt(ni-1))==-1)||(allowchr.indexOf(nv.charAt(ni+city.length))==-1)) {
					return res;
				}
				/*ts = nv.substr(ni+city.length,10);
				var tsregexp = /^\s[A-Z]/;
				if ((tsregexp.test(ts))&&(ts.indexOf('City')>-1)) {
					return res;
				}*/
				before = document.createTextNode(nv.substr(0,ni));
				linkText = nv.substr(ni,city.length);
				after = document.createTextNode(nv.substr(ni+city.length));
				linkNode = document.createTextNode(linkText);
				link = document.createElement('a');
				switch (type) {
					case 'counties':
						link.setAttribute('href','http://www.city-data.com/county/'+prepare_urlstring(city,'_')+'-'+prepare_urlstring(state_code)+'.html');
						link.setAttribute('title',city+', '+state_name+' detailed profile');
						break;
					case 'cities':
						link.setAttribute('href','http://www.city-data.com/city/'+prepare_urlstring(city)+'-'+prepare_urlstring(state_name)+'.html');
						link.setAttribute('title',city+', '+state_name+' detailed profile');
						break;
					case 'zips':
						link.setAttribute('href','http://www.city-data.com/zips/'+prepare_urlstring(city)+'.html');
						link.setAttribute('title',city+', Zip Code ('+state_name+') detailed profile');
						break;
				}
				link.className='citylink';
				link.appendChild(linkNode);
				pn.insertBefore(before,node);
				pn.insertBefore(link,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
				return true;
			}
		}
	}
	return res;
}

function insertInfoLinks(type) {
	var posts = document.getElementById("posts");
	var posts_div = posts.getElementsByTagName("div");

	switch (type) {
		case 'counties':
			var data = counties;
			break;
		case 'cities':
			var data = cities;
			break;
		case 'zips':
			var data = zips;
			break;
	}

	for (var c=0; c<data.length; c++) {
		var cf = false;
		for (var i=0; i<posts_div.length; i++) {
	   		name = posts_div[i].id;
	        if (name.substr(0,12) == "post_message") {
	        	if (highlightCity(document.getElementById(name),data[c],type)) {
	        	  cf = true;
	        	  break;
	        	}
	    	}
		}
		if (cf) continue;
	}
}

insertInfoLinks('counties');
insertInfoLinks('cities');
insertInfoLinks('zips');
