loadingmsg = '<h3><img src=\"images/throbber.gif\" width=\"16\" height=\"16\" alt=\"Loading...\" class=\"icon\" /> Loading...</h3>';

var map, loadstart;

$(function() {
	$("#map").html(loadingmsg);
	$("#resultspanel:hidden").html(loadingmsg);
	$("#searchquery").val("Search Directory...").focus(function() {
		$(this).val($(this).val().replace("Search Directory...",""));
	});
	
	if (google.maps.BrowserIsCompatible()) {
		map = new google.maps.Map2($("#map").get(0));
		map.setUIToDefault();
		map.setCenter(new google.maps.LatLng(-34.066, 137.585), 13);
		google.maps.Event.addListener(map, "load", resizemap());
	} else {
		$("#map").html("<p>Your browser is not compatible with the map interface. Please update your browser, or feel free to browse the rest of the directory without the map.</p>");
	}
	
	$(window).unload(google.maps.Unload).resize(resizemap);
	if ($.isFunction(loadstart)) {
		loadstart();
	}
})

mapmarkers = new Array();
markerbase = $("base").attr('href') + "images/mapicons/";
basetitle = document.title.split(' - ', 2).join(' - ');

function resizemap() {
	var position = map.getCenter()
	$("#map").height(Math.max($(window).height() - $("#map").offset().top - 20, 300));
	map.checkResize();
	map.setCenter(position);
	
	$("#categorypanel:visible").height(Math.max($(window).height() - $("#categorypanel").offset().top - 20, 200));
	$("#resultspanel:visible").height(Math.max($(window).height() - $("#resultspanel").offset().top - 20, 300));
}

function createmarker(number, point, name, detail, link) {
	if (number >= 1 && number <= 99) {
		var micon = new google.maps.Icon(G_DEFAULT_ICON, markerbase + "blue/" + number + ".png");
	} else {
		var micon = new google.maps.Icon(G_DEFAULT_ICON, markerbase + "blue/dot.png");
	}
	var marker = new google.maps.Marker(point, {icon: micon, title: name});
	google.maps.Event.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml('<h3>' + name + '</h3>' + detail + '<p class="rightlink"><a href="directory/' + link + '">more info...</a></p>');
	});
	return marker;
}

function searchdirectory(searchquery, searchcategory) {
	if (!searchquery) {
		var searchquery = $("#searchquery").val();
		var searchcategory = $("#searchcategory").val();
	}
	if (searchquery == "" || searchquery == " " || searchquery == "_") {
		alert("No search query entered. Please enter a query and try again.");
	} else {
		resetmap(true, "Search Results");
		$.get("directorylookup.php", {query: searchquery, category: searchcategory}, displayresults);
	}
}

function showcategory(category, cattitle) {
	resetmap(true, cattitle);
	$.get("directorylookup.php", {category: category}, displayresults);
}

function displayresults(xml, textStatus) {	
	$("#resultspanel").html('<p class="centerlink"><a href="directory" onclick="resetmap(false); return false;">&lt; Back</a> <span class="faded">|</span> ' + $("marker", xml).length + ' results found</p><hr />');
	var number = 1;
	if ($("marker", xml).length > 0) {
		map.clearOverlays();
		var bounds = new google.maps.LatLngBounds();
		$("#resultspanel").append('<ol></ol>');
		$("marker", xml).each(function() {
			var item = $('<li><h3><a href="directory/' + $(this).attr('link') + '">' + $(this).attr('name') + '</a></h3><p>' + $(this).attr('detail') + '</p></li>');
			if ($(this).attr('lat') != 0 && $(this).attr('lng') != 0) {
				var point = new google.maps.LatLng(parseFloat($(this).attr('lat')), parseFloat($(this).attr('lng')));
				mapmarkers[number] = createmarker(number, point, $(this).attr('name'), $(this).attr('detail'), $(this).attr('link'));
				map.addOverlay(mapmarkers[number]);
				bounds.extend(point);
				item.hover(
					function() {
						number = $("#resultspanel ol li").index(this)+1;
						mapmarkers[number].setImage(markerbase + "yellow/" + number + ".png");
						map.panTo(mapmarkers[number].getLatLng());
					},
					function() {
						number = $("#resultspanel ol li").index(this)+1;
						mapmarkers[number].setImage(markerbase + "blue/" + number + ".png");
					}
				);
			}
			item.appendTo("#resultspanel ol");
			number++;
		});
		if (!bounds.isEmpty()) {
			map.setZoom(Math.min(map.getBoundsZoomLevel(bounds), 16));
			map.panTo(bounds.getCenter());
		}
	}
}

function resetmap(resultson, title) {
	map.clearOverlays();
	document.title = basetitle; 
	if (title) {
		document.title += " - " + title;
	}
	if (resultson) {
		$("#directorybrowse").slideUp("slow");
		$("#directoryresults").slideDown("slow", resizemap);
	} else {
		$("#directoryresults").slideUp("slow");
		$("#directorybrowse").slideDown("slow", resizemap);
		$("#resultspanel").html(loadingmsg);
	}
}