
var cityArr = null;
var cityLinkArr = null;
var columnCount = 6;
var startInputValue = "Type a city name...";

function runCityAutoFind(){
	var cityInput = document.getElementById("city_auto_find");
	var tArr = new Object();
	var cityId = null;
	var filterArr = new Array();
	var cityInputVal = cityInput.value.toUpperCase();
	var inputLen = 0;
	var tCityStr = "";
	var tCount = 0;

//	if(! cityArr){
//		cityArr = JSON.parse(document.getElementById("city_auto_find_json_div_id").innerHTML);
//	
//	}
	
	if(! cityLinkArr){
		cityLinkArr = document.getElementsByName("city_link");
	
	}
	
	//cityLinkArr = document.getElementsByName("city_link");
	
	//console.log(cityArr);

	inputLen = cityInputVal.length;

	if(inputLen == 0){
		var oldList = document.getElementById("city_list_div_id");
		document.getElementById("city_list_div_id").style.display = "block";
		document.getElementById("city_auto_find_list_div_id").innerHTML = "";
		document.getElementById("city_auto_find_list_div_id").style.display = "none";
		return;
	}
	
	var cityName = "";
	var tLink = null;
	var tTitle = "";
	var cityNameObj = new Object();
	for(var i = 0; i < cityLinkArr.length; i++){
		cityName = cityLinkArr[i].title;
			tCityStr = cityName.substring(0,inputLen).toUpperCase();
			if(tCityStr == cityInputVal){
				
				if(cityNameObj[cityName]) continue;
				else cityNameObj[cityName] = true;
				
				tLink = cityLinkArr[i].cloneNode(true);
				tTitle = tLink.title;
				tTitle = tTitle.substring(0, (tTitle.length - 8));
				tLink.innerHTML = tTitle;
				tLink.name = "city_search_link";
				
				filterArr.push(tLink.cloneNode(true));
				tLink = null;
				//alert(cityName);
				//filterArr[cityId] = cityArr[cityId];
				tCount++;
			}
	}
	
//	for(cityId in cityArr){
//
//		tCityStr = cityArr[cityId].name.substring(0,inputLen).toUpperCase();
//		if(tCityStr == cityInputVal){
//			filterArr[cityId] = cityArr[cityId];
//			tCount ++;
//		}
//
//	}

	updateCityList(filterArr, tCount);

}

//function updateCityList(filterArr, resultCount){
//
//	var oldList = document.getElementById("city_list_div_id");
//	oldList.style.display = "none";
//
//	var containerElement = document.getElementById("city_auto_find_list_div_id");
//	containerElement.style.display = "block";
//
//	var divTag = null;
//	var linkTag = null;
//
//
//	var citiesPerColumn = 0;
//	var colNum = 6;
//	
//	if(resultCount <= 6){
//		citiesPerColumn = 1;
//	}
//	else{
//		citiesPerColumn = Math.ceil(resultCount / colNum);
//	}
//	
//	var tCount = 0;
//	var containerDiv = null;
//	var tName = "";
//	var elementsLeftCount = resultCount;
//
//	containerElement.innerHTML = "";
//	containerDiv = document.createElement("div");
//
//	if(resultCount == 0){
//		containerDiv.innerHTML = "Sorry, no cities found.";
//	}
//	
//	for(cityId in filterArr){
//
//		linkTag = document.createElement("a");
//		
//		if(filterArr[cityId].s){
//			linkTag.href = "http://"+filterArr[cityId].s+".cityvibe.com/escorts/";
//		}
//		else linkTag.href = "http://eroticservices.cityvibe.com/index.php?do=list&city="+cityId;
//
//		if(filterArr[cityId].t && filterArr[cityId].t > 0){
//			linkTag.style.fontWeight = "bold";
//		}
//		linkTag.title = filterArr[cityId].name + " Escorts";
//		tName = filterArr[cityId].name;
//
//		if(tName.length > 18){
//			tName = tName.substring(0,17)+"...";
//		}
//
//		linkTag.innerHTML = tName;
//
//		divTag = document.createElement("div");
//		//
//		//http://eroticservices.cityvibe.com/index.php?do=list&city=1002
//		//divTag.id = "div1";
//		//divTag.setAttribute("align","center");
//		//divTag.style.margin = "";
//		divTag.className ="";
//		divTag.appendChild(linkTag);
//		if(! containerDiv) containerDiv = document.createElement("div");
//		containerDiv.appendChild(divTag);
//
//		//
//		//
//		tCount++;
//
//		if(tCount == citiesPerColumn){
//			if(containerDiv){
//				
//				containerDiv.style.width = "153px";
//				//containerDiv.style.height = "13px";
//				containerDiv.style.overflow = "hidden";
//				containerDiv.style.styleFloat = "left";
//				containerDiv.style.cssFloat = "left";
//				containerElement.appendChild(containerDiv);
//
//				elementsLeftCount -= citiesPerColumn;
//				colNum--;
//				citiesPerColumn = Math.ceil(elementsLeftCount / colNum);
//				//
//			} 
//			containerDiv = null;
//			tCount = 0;
//
//		}
//	}
//
//	if(containerDiv) containerElement.appendChild(containerDiv);
//}

function updateCityList(filterArr, resultCount){

	var oldList = document.getElementById("city_list_div_id");
	oldList.style.display = "none";

	var containerElement = document.getElementById("city_auto_find_list_div_id");
	containerElement.style.display = "block";

	var divTag = null;
	var linkTag = null;


	var citiesPerColumn = 0;
	var colNum = 6;
	
	if(resultCount <= 6){
		citiesPerColumn = 1;
	}
	else{
		citiesPerColumn = Math.ceil(resultCount / colNum);
	}
	
	var tCount = 0;
	var containerDiv = null;
	var tName = "";
	var elementsLeftCount = resultCount;

	containerElement.innerHTML = "";

	containerDiv = document.createElement("div");

	if(resultCount == 0){
		containerDiv.innerHTML = "Sorry, no cities found.";
	}
	
	for(var i = 0; i < filterArr.length; i++){
	//for(cityId in filterArr){

//		linkTag = document.createElement("a");
//		
//		if(filterArr[cityId].s){
//			linkTag.href = "http://"+filterArr[cityId].s+".cityvibe.com/escorts/";
//		}
//		else linkTag.href = "http://eroticservices.cityvibe.com/index.php?do=list&city="+cityId;
//
//		if(filterArr[cityId].t && filterArr[cityId].t > 0){
//			linkTag.style.fontWeight = "bold";
//		}
//		linkTag.title = filterArr[cityId].name + " Escorts";
//		tName = filterArr[cityId].name;
//
//		if(tName.length > 18){
//			tName = tName.substring(0,17)+"...";
//		}
//
//		linkTag.innerHTML = tName;

		divTag = document.createElement("div");
		//
		//http://eroticservices.cityvibe.com/index.php?do=list&city=1002
		//divTag.id = "div1";
		//divTag.setAttribute("align","center");
		//divTag.style.margin = "";
		divTag.className ="";
		divTag.appendChild(filterArr[i]);
		if(! containerDiv) containerDiv = document.createElement("div");
		containerDiv.appendChild(divTag);

		//
		//
		tCount++;

		if(tCount == citiesPerColumn){
			if(containerDiv){
				
				containerDiv.style.width = "153px";
				//containerDiv.style.height = "13px";
				containerDiv.style.overflow = "hidden";
				containerDiv.style.styleFloat = "left";
				containerDiv.style.cssFloat = "left";
				containerElement.appendChild(containerDiv);

				elementsLeftCount -= citiesPerColumn;
				colNum--;
				citiesPerColumn = Math.ceil(elementsLeftCount / colNum);
				//
			} 
			containerDiv = null;
			tCount = 0;

		}
	}

	if(containerDiv) containerElement.appendChild(containerDiv);
}

function initCityInput(){
	var cityInput = document.getElementById("city_auto_find");

	if(cityInput.value == startInputValue){
		cityInput.value = "";
		cityInput.style.color = "#000";
	}
}

function resetCityInput(){
	var cityInput = document.getElementById("city_auto_find");
	var startValue = "Type a city name...";
	if(cityInput.value == ""){
		cityInput.value = startInputValue;
		cityInput.style.color = "#333";
	}
}
