
var map = null, bounds = null, closest_marker = null;

function createMarker(point, html, link, i) {
	var marker = null, label = null;
	marker = new GMarker(point);
	marker.i = i;
	marker.sRow = '#row'+i;
	var attachment;
	var center = bounds.getCenter();	
	if(point.lat() > center.lat()) {
		if(point.lng() > center.lng())
			attachment = 'ne';
		else
			attachment = 'nw';
	}
	else {
		if(point.lng() > center.lng())
			attachment = 'se';		
		else
			attachment = 'sw';		
	}

	var label = new BpLabel(point,html,'myLabelClass',attachment, true);
	marker.tltp = label;
	map.addOverlay(label);
	
	GEvent.addListener(marker, "click", function() {
		//this.openInfoWindowHtml(html);
		window.location = link;
	});
	
	GEvent.addListener(marker, 'mouseover', function(ll){
		jQuery(""+this.sRow).addClass('over');
		this.setImage(mouseover_icon);
		this.tltp.show();
		//this.openInfoWindowHtml(html);
	});

	GEvent.addListener(marker, 'mouseout', function(ll){
		jQuery(""+this.sRow).removeClass('over');
		this.setImage(mouseout_icon);
		this.tltp.hide();
		//map.closeInfoWindow();
	});

	jQuery(''+marker.sRow).data("marker", marker);
	return marker;
}

function addAllMarkers() {
	if(city_data.length != 0) {

		var terr_infos = new Array(city_data.length);
		var points = new Array(city_data.length);

		for (var i=0, j=city_data.length; i<j; i++ ) {
			var terr_info = city_data[i].split("|");
			var point = new GLatLng (terr_info[3], terr_info[4]);
			bounds.extend(point);
			terr_infos[i] = terr_info;
			points[i] = point;
		}
	
		for (var i=0, j=city_data.length; i<j; i++ ) {
			var terr_info = terr_infos[i];
			var terr_name = terr_info[1];
			var terr_link = terr_info[2];
			var info_html = 
				'<div>' + '<strong style="font-size:14px;">'+ terr_info[1] +'<\/strong><br\/>';
			
			if(terr_info[6] != '' && terr_info[6] != '0') info_html += '<p><b>average home price*:<\/b> '+ terr_info[6] + 'k<\/p>';
			if(terr_info[5] != '' && terr_info[5] != '0') info_html += '<p><b>number of listings:<\/b> '+ terr_info[5] + '<\/p>';
			
			if(terr_info[2] != "") info_html += '<p>click to view all listings in the area<\/p>';
				
			info_html += '<\/div>';
			
			var point = points[i];
			var marker = createMarker(point, info_html, terr_link, i);
			if (marker != null) {
				map.addOverlay(marker);
				//bounds.extend(point);
			}
		}
		var center = bounds.getCenter();
		var zoom_level = map.getBoundsZoomLevel(bounds);
		map.setCenter(center, (zoom_level < 10)? zoom_level : 10);
		
	}
	else {
		if((cur_lat!=0) || (cur_long!=0)) {
			var point = new GLatLng (cur_lat, cur_long);
			//var marker = createMarker(point, "", "center", 0);
			//if (marker != null) {
			//	map.addOverlay(marker);
			//}
			map.setCenter(point, 11);
		}
	}
}

function bindBehaviors() {
	jQuery('.terr_row').hover(
		function(){
			var $this = jQuery(this);
			$this.addClass('over');
			var marker = $this.data("marker");
			marker.setImage(mouseover_icon);
			marker.tltp.show();
			//GEvent.trigger(marker, 'click');
		},
		function(){
			var $this = jQuery(this);
			$this.removeClass('over');
			var marker = $this.data("marker");
			marker.setImage(mouseout_icon);
			marker.tltp.hide();
		}
	);
}

function initMap() {
	bindBehaviors();	
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		bounds = new GLatLngBounds;	
		//if((cur_lat==0) && (cur_long==0)) map.setCenter(new GLatLng(38, -97),3);
		//else map.setCenter(new GLatLng(cur_lat,cur_long),8);
		map.setCenter(new GLatLng(0,0),8);
		addAllMarkers();
		//GEvent.trigger(closest_marker, 'click');		
	} 
	else {
		mapObject = document.getElementById("map");
		mapObject.innerHtml = "<p>Sorry, the Google Maps API is not compatible with this browser</p>";
		//alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}

