function createMarker(point, obj, i, target) {
	$(obj).attr("id", target+"_"+i);
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(obj.html, {maxWidth: "300"});
		highLightItem(obj, i, target);
		
	});
	return marker;
}
function showAddresses(mapobj, target, zoom){
	var minLat = 90; var maxLat = -90; var minLong = 180; var maxLong = -180; 
	var theSelector = target != 'self' ? '#'+target+' .addressItem' : '.addressItem';
	var items = $(theSelector);
	for(var i = 0; i < items.length; i++){
		var drivingDirection	= $(items[i]).find(".drivingDirection").html();
		var html				= $(items[i]).find(".address").html();
		if(drivingDirection != null){
			html += "<br />" + drivingDirection;
		}
		items[i].html			= html;
		items[i].latitude		= $(items[i]).find(".latitude").html() * 1;
		items[i].longitude		= $(items[i]).find(".longitude").html() * 1;
		if(items[i].latitude!=0 && items[i].longitude!=0){

			if(items[i].latitude < minLat) minLat = items[i].latitude;
			if(items[i].latitude > maxLat) maxLat = items[i].latitude;
			if(items[i].longitude < minLong) minLong = items[i].longitude;
			if(items[i].longitude > maxLong) maxLong = items[i].longitude;
			
			point		= new GLatLng(items[i].latitude, items[i].longitude);
			mapobj.setCenter(point, zoom);
			marker		= createMarker(point, items[i], i, target);
			items[i].marker	= marker;
			items[i].index	= i;
			items[i].target	= target;

			mapobj.addOverlay(marker);
			
			$(items[i]).click(function(){
				highLightItem(this, this.index, this.target);
				var p = new GLatLng(this.latitude, this.longitude); 
				mapobj.centerAndZoom(p);
				this.marker.openInfoWindowHtml(this.html, {maxWidth: "300"});
				return false;
			});
		}
	}
	
	var centerLat = (minLat+maxLat)/2 + 0.03;	// add 0.03 so it will move right from the zoom slider
	var centerLong = (minLong+maxLong)/2;

	mapobj.setCenter(new GLatLng(centerLat, centerLong), zoom);
}
function highLightItem(obj, i, target) {
	$(".addressItem").removeClass("selected");
	$(obj).addClass("selected");
	var $paneTarget = $("#"+target); 
	var $itemTarget = $("#"+target+"_"+i);
	$paneTarget.stop().scrollTo($itemTarget, 800);
}
if(target=="self"){
	$(function(){
		bindToggleSidebarLooking();
		if(typeof $("#frmSidebarLooking")[0] != "undefined"){
			bindSidebarLooking();
			bindAgain();
			bindGoTo();
		}
	});
	function bindToggleSidebarLooking(){
		if(typeof $(".cmdAgain")[0] != "undefined"){
			$("#secondary h3").hide();
			$("#frmSidebarLooking").hide();
		}else{
			$("#secondary h3").show();
			$("#frmSidebarLooking").show();
		}
	}
	function bindSidebarLooking(){
		$("#frmSidebarLooking").submit(function(){
			if($("#strZip").val()=="" && $("#theRadius").val()==""){
				alert("Ooops! Enter something first..");
				$("#strZip").focus();
				return false;
			}
			$("#secondary").append('<img class="indicator" src="/jscripts/images/indicator.gif" />');
			$.post(
				locatorURL,
				{
					isAjax:		1,
					strZip:		$("#strZip").val(),
					theRadius:	$("#theRadius").val()
				},
				function(txt){
					if(txt != ""){
						$(".indicator").remove();
						$("#locatorResults").html(txt);
						bindToggleSidebarLooking();
						map.clearOverlays();
						showAddresses(map, 'self'); // hardcoded for movida - since it only uses this
						bindAgain();
						bindGoTo();
						txt = null;
					}
				}
			);
			return false;
		});
	}
	function bindAgain(){
		$(".cmdAgain").click(function(){
			$("#strZip").val("");
			$("#theRadius").val("");
			$("#locatorResults").html("");
			$(this).parent().remove();
			bindToggleSidebarLooking();
			map.clearOverlays();
			map.setCenter(new GLatLng(39.10678, -94.67647), 4);
			return false;
		});
	}
	function bindGoTo(){
		$(".cmdGoTo").click(function(){
			$("#secondary").append('<img class="indicator" src="/jscripts/images/indicator.gif" />');
			$.post(
				locatorURL,
				{
					isAjax:	1,
					page:	$(this).attr("id").split("_")[1]
				},
				function(txt){
					if(txt != ""){
						$(".indicator").remove();
						$("#locatorResults").remove();
						$("#paging").remove();
						$(this).remove();
						$(".cmdAgain").parent().remove();
						$("#secondary").append(txt);
						map.clearOverlays();
						showAddresses(map, 'self'); // hardcoded for movida - since it only uses this
						bindAgain();
						bindGoTo();
						txt = null;
					}
				}
			);
			return false;
		});
	}
}