var cities_focus = 0;

var getcity = new AJAX();

getcity.processResponse = function (responseText){
	if (responseText == ''){
		document.getElementById("city_suggestions").innerHTML = responseText;
		document.getElementById("city_suggestions").style.display = "none";
		return true;
	}
	
	//cities_focus = 1;
	document.getElementById("city_suggestions").innerHTML = responseText;
	document.getElementById("city_suggestions").style.display = "block";
	return true;
}


function autoComplete(){

	if (document.getElementById("loc_box").value.length < 3){
		document.getElementById("city_suggestions").style.display = "none";
		return false;
	}
	getcity.fetch("/widgets/getcities.php?city=" + escape(document.getElementById("loc_box").value));
}

var highlighted = 1;

function downKey(){
	/*if (cities_focus == 0){
		return false;
	}*/
	
	var x = document.getElementById('slot_'+highlighted);
	if (x == null){
		return false;
	}
	
	if (x.currentStyle){
		var y = x.currentStyle['display'];
	}else if (window.getComputedStyle){
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue('display');
	}
	
	if (y == 'block'){
		document.getElementById('slot_'+highlighted).style.backgroundColor = '#dddddd';
		if (highlighted > 1){
			var last = highlighted-1;
			document.getElementById('slot_'+last).style.backgroundColor = '#ffffff';
		}
		highlighted++;
	}
}

function upKey(){
	/*if (cities_focus == 0){
		return false;
	}*/
	
	var previous = highlighted - 2;
	
	var x = document.getElementById('slot_'+previous);
	if (x == null){
		return false;
	}
	
	if (x.currentStyle){
		var y = x.currentStyle['display'];
	}else if (window.getComputedStyle){
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue('display');
	}
	
	if (y == 'block'){
		document.getElementById('slot_'+previous).style.backgroundColor = '#dddddd';
		var last = highlighted-1;
		document.getElementById('slot_'+last).style.backgroundColor = '#ffffff';
		highlighted--;
	}
}


function keyPress(key){
	cities_focus = 0;
	
	switch (key){
		case 13:
			if (document.getElementById('city_suggestions').style.display=="none"){
				return checkSearchForm();
				//closeLocation(1);
			}
			document.getElementById('city_suggestions').style.display="none";
			break;
			
		case 38:
			upKey();
			break;
				
		case 40:
			downKey();
			break;
			
		default:
			highlighted = 1;
			autoComplete();
	}
	return false;
}

var ua = navigator.userAgent.toLowerCase();
var isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1)); 

function testForEnter(e) 
{    
	var current = highlighted-1;
	if (e.keyCode == 13 && document.getElementById('city_suggestions').style.display!='none'){
		document.getElementById('loc_box').value = document.getElementById('city_slot_'+current).innerHTML;
		if (!e) { var e = window.event; }
		if (isIE) {
			e.returnValue = false;
			e.cancelBubble = true;
		} else {
			e.stopPropagation();
			e.preventDefault();
		}
		return false;
	}
} 
