var asTimeout = null;
var closeTimeout = null;
var searchCrit;
var mouseoverflag = false;
var sasw = new google.maps.LatLng(-34.8422, 16.4516);
var sane = new google.maps.LatLng(-22.1111, 32.8966);
var saBounds = new google.maps.LatLngBounds(sasw, sane);
var geocoder = new google.maps.Geocoder();

function clearTimer() {
	document.getElementById("results").style.display = "none";
}

function startTimer(obj, event) {
	clearTimeout(asTimeout);
	if (obj.value.length != 0) {
		if (event.keyCode == 13) {
			searchCrit = obj.value.replace(/\'/g, "\\'");	
			fireTimer();
		} else {
			searchCrit = obj.value.replace(/\'/g, "\\'");
			asTimeout = setTimeout("fireTimer()", 2000);
		}
	}
}

function fireTimer() {
	var bg = "#ffffff url(http://bizzorg.com/img/busy.gif) no-repeat center right";
	document.getElementById('search').style.background = bg;
	if (document.getElementById("r1").checked) {
		codeAddress();
	} else {
		AJAXCall("getLocations", encodeURI("crit=" + searchCrit + "&type=2"), "gotSearchResults(rep)");
	}
}

function codeAddress() {
	geocoder.geocode( {'address': searchCrit + ', South Africa', 'region': 'ZA', 'bounds': saBounds}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var str = "";
			var found = false;
			for (var n = 0; n < results.length; n++) {
				for (var x = 0; x < results[n].address_components.length; x++) {
					if (results[n].address_components[x].short_name == "ZA") {
						found = true;
						str += "<div class='resultline'' onclick='cityChanged(\"" + results[n].geometry.location.lat() + "," + results[n].geometry.location.lng() + ",12\")' onmouseover='overResult(this, " + results[n].geometry.location.lat() + ", " + results[n].geometry.location.lng() + ")' onmouseout='outResult(this)'>" + results[n].formatted_address + "</div>";
					}
				}

				if (!found) {
					document.getElementById("results").innerHTML = "<div class='resultline' onmouseover='mouseoverflag=true' onmouseout='mouseoverflag=false' >" + searchCrit.replace(/\\'/g, "'") + " - Not found</div>";
				} else {
					document.getElementById("results").innerHTML = str;
				}
			}
		} else {
			document.getElementById("results").innerHTML = "<div class='resultline' onmouseover='mouseoverflag=true' onmouseout='mouseoverflag=false' >" + searchCrit.replace(/\\'/g, "'") + " - Not found</div>";
		}
		document.getElementById('search').style.backgroundImage = "none";
		document.getElementById("results").style.display = "block";
	});
}

function gotSearchResults(obj) {

	if (obj[0][0] == "none") {
		document.getElementById("results").innerHTML = "<div class='resultline' onmouseover='mouseoverflag=true' onmouseout='mouseoverflag=false' >" + searchCrit.replace(/\\'/g, "'") + " - Not found</div>";
	} else {
		var str = "";
		for (var n = 0; n < obj.length; n++) {
			str += "<div class='resultline' onclick='cityChanged(\"" + obj[n][1] + "," + obj[n][2] + "," + obj[n][3] + "\")' onmouseover='overResult(this, " + obj[n][1] + ", " + obj[n][2] + ")' onmouseout='outResult(this)'>" + obj[n][0] + "</div>";
		}
		str += "<div class='resultline' onmouseover='mouseoverflag=true' onmouseout='mouseoverflag=false'><a href='http://plak.co.za/search.php?crit=" + searchCrit + "&type=All' class='plaklink'>More search results...</a></div>";
		document.getElementById("results").innerHTML = str;
	}
	document.getElementById('search').style.backgroundImage = "none";
	document.getElementById("results").style.display = "block";
}

function overResult(obj, xLat, xLng) {
	mouseoverflag=true;
	obj.style.color="#0080c0";
	
	var point = new google.maps.LatLng(xLat, xLng);
	xMarker.setPosition(point);

	google.maps.event.clearListeners(xMarker, "mouseover");
	google.maps.event.clearListeners(xMarker, "mouseout");

	google.maps.event.addListener(xMarker, "mouseover", function(event) {
		showName(obj.innerHTML, event.latLng, 0);
	});

	google.maps.event.addListener(xMarker, "mouseout", function(event) {
		hideName();
	});
}

function outResult(obj) {
	mouseoverflag=false;
	obj.style.color="black";
}

function closeResDiv() {
	clearTimeout(closeTimeout);
	closeTimeout = setTimeout("closeResults()", 300);
}

function closeResults() {
	if (!mouseoverflag) {
		document.getElementById("results").style.display = "none";
	}
}

function showResults() {
	if (document.getElementById("results").innerHTML != "") {
		clearTimeout(closeTimeout);
		document.getElementById("results").style.display = "block";
	}
}
