
var map = null, bounds = null, closest_marker = null;
			
var shadow_icon = '/img4/temp/shadow.png';
var mouseover_icon = '/img4/h_circle_box.png';
var mouseout_icon = '/img4/temp/blue_marker.png';
var mouseout_nearest_icon = '/img4/temp/blue_marker.png';

var price_range = [ [0,100000],
					[100001,200000],
					[200001,300000],
					[300001,400000],
					[400001,500000],
					[500001,600000],
					[600001,700000],
					[700001,800000],
					[800001,1000000],
					[1000001,100000000]];
					
					
function getPriceRangeIcon(price)
{
	if(price) {
		var priceInt = parseInt(price.replace(/[^0-9]/g,""));
		for (var i=0; i<price_range.length; ++i ){
			if(priceInt > price_range[i][0] && priceInt < price_range[i][1]) {
				return price_range_icon[i];
			}
		}
	}
	return price_range_icon[4];
}
				

function createMarker(point, html, link, i, price) {
	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	var baseIcon = new GIcon(G_DEFAULT_ICON);
	baseIcon.shadow = shadow_icon;
	baseIcon.iconSize = new GSize(20, 27);
	baseIcon.shadowSize = new GSize(37, 27);
	//baseIcon.iconAnchor = new GPoint(9, 34);
	//baseIcon.infoWindowAnchor = new GPoint(9, 2);
	var custIcon = new GIcon(baseIcon);
	var icon = getPriceRangeIcon(price);
	custIcon.image = icon;
	markerOptions = { icon:custIcon };
	var marker = null, label = null;
	marker = new GMarker(point, markerOptions);
	marker.i = i;
	marker.sRow = '#row'+i;
	marker.icon = icon;
	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(this.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>Avg Home Price*:<\/b> $'+ terr_info[6] + '<\/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 += '<br/><p>Click Map Icon to View All Area Listings<\/p>';
				
			info_html += '<\/div>';
			
			var point = points[i];
			var marker = createMarker(point, info_html, terr_link, i, terr_info[6]);
			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);
		//}
		var point = new GLatLng(38, -97);
		map.setCenter(point, 3);
	}
}

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();
			marker.setZIndex(999);
			//GEvent.trigger(marker, 'click');
		},
		function(){
			var $this = jQuery(this);
			$this.removeClass('over');
			var marker = $this.data("marker");
			marker.setImage(marker.icon);
			marker.tltp.hide();
			marker.setZIndex(1);
		}
	);
}

function initMap() {
	bindBehaviors();	
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		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");
	}
}

