$(document).ready(function(){ 
var comm_json = ({"Communities":[{"name":"14515 Dickens","id":"1","address":"14515 Dickens St.","city":"Sherman Oaks","state":"CA","zip":"91403","phone":"(818) 981-3464","image":"\/properties\/14515-Dickens\/images\/14515_dickens_main_sm.jpg","domain":"www.gfproperties.net\/go\/?prop=8","lat":"34.15065","lng":"-118.45925","type":"community"}
,{"name":"14625 Dickens","id":"2","address":"14625 Dickens St.","city":"Sherman Oaks","state":"CA","zip":"91403","phone":"(818) 981-3464","image":"http:\/\/www.gfproperties.net\/properties\/14625-Dickens\/14625-Dickens.jpg","domain":"www.gfproperties.net\/go\/?prop=9","lat":" 34.15124","lng":" -118.48249","type":"community"}
,{"name":"14430 Benefit","id":"3","address":"14430 Benefit St.","city":"Sherman Oaks","state":"CA","zip":"91403","phone":"(818) 906-9591","image":"http:\/\/www.gfproperties.net\/properties\/14430-Benefit\/14430-Benefit.jpg","domain":"www.gfproperties.net\/go\/?prop=7","lat":" 34.128962","lng":" -118.44757","type":"community"}
,{"name":"4433 Murietta","id":"5","address":"4433 Murietta Ave.","city":"Sherman Oaks","state":"CA","zip":"91403","phone":"(818) 728-9856","image":"\/properties\/4433-Murietta\/4433-Murietta.jpg","domain":"www.gfproperties.net\/go\/?prop=10","lat":"34.15022","lng":"-118.43970","type":"community"}
,{"name":"1420 Barrington Way","id":"17","address":"1420 Barrington Way","city":"Glendale","state":"CA","zip":"91202","phone":"(323) 252-5211","image":"\/properties\/1420-Barrington\/1420-Barrington.jpg","domain":"www.gfproperties.net\/go\/?prop=1","lat":"34.14733","lng":"-118.23409","type":"community"}
,{"name":"519 Palm","id":"18","address":"519 Palm Drive","city":"Glendale","state":"CA","zip":"91202","phone":"(818) 242-1186","image":"\/properties\/519-Palm\/images\/519_palm_main.jpg","domain":"www.gfproperties.net\/go\/?prop=2","lat":"34.16223","lng":"-118.26535","type":"community"}
,{"name":"1031 N. Crescent Heights","id":"19","address":"1031 N. Crescent Heights Blvd.","city":"West Hollywood","state":"CA","zip":"90046","phone":"(818) 235-9903","image":"\/properties\/La-Ventana\/La-Ventana.jpg","domain":"www.gfproperties.net\/go\/?prop=15","lat":"34.09020","lng":"-118.34395","type":"community"}
,{"name":"2050 Bentley","id":"14","address":"2050 S. Bentley Ave.","city":"Los Angeles","state":"CA","zip":"90025","phone":"(310) 235-9903","image":"\/properties\/2050-Bentley\/2050-Bentley.jpg","domain":"www.gfproperties.net\/go\/?prop=3","lat":"34.04279","lng":"-118.43832","type":"community"}
,{"name":"2130 Fair Park","id":"15","address":"2130 Fair Park Ave.","city":"Los Angeles","state":"CA","zip":"90041","phone":"(323) 255-8123","image":"http:\/\/www.gfproperties.net\/properties\/2130-Fair-Park\/2130-Fair-Park.jpg","domain":"www.gfproperties.net\/go\/?prop=4","lat":"34.135758","lng":"-118.21439","type":"community"}
,{"name":"350 S. Reno","id":"16","address":"350 S. Reno St.","city":"Los Angeles","state":"CA","zip":"90057","phone":"(213) 381-9243","image":"\/properties\/350-Reno\/images\/350s_reno_main.jpg","domain":"www.gfproperties.net\/go\/?prop=5","lat":"34.067041","lng":"-118.28407","type":"community"}
,{"name":"649 S. Ridgeley","id":"13","address":"649 S. Ridgeley Dr.","city":"Los Angeles","state":"CA","zip":"90036","phone":"(323) 855-1931","image":"\/properties\/649-Ridgeley\/649-Ridgeley.jpg","domain":"www.gfproperties.net\/go\/?prop=6","lat":"34.06377","lng":"-118.35073","type":"community"}
,{"name":"12712 Moorpark","id":"6","address":"12712 Moorpark St.","city":"Studio City","state":"CA","zip":"91604","phone":"(818) 915-2854","image":"\/properties\/12712-Moorpark\/images\/12712_moorpark_main.jpg","domain":"www.gfproperties.net\/go\/?prop=12","lat":"34.15083","lng":"-118.41002","type":"community"}
,{"name":"10650 Moorpark","id":"8","address":"10650 Moorpark St.","city":"Toluca Lake","state":"CA","zip":"91602","phone":"(818) 980-3322","image":"\/properties\/10650-Moorpark\/images\/10650_moorpark_main.jpg","domain":"www.gfproperties.net\/go\/?prop=14","lat":"34.15076","lng":"-118.36303","type":"community"}
,{"name":"8819 Harratt","id":"11","address":"8819 Harratt St.","city":"West Hollywood","state":"CA","zip":"90069","phone":"(323) 309-5377","image":"\/properties\/8819-Harratt\/8819-Harratt.jpg","domain":"www.gfproperties.net\/go\/?prop=15","lat":"34.09020","lng":"-118.38376","type":"community"}
,{"name":"718 N. Kings","id":"12","address":"718 N. Kings Rd.","city":"West Hollywood","state":"CA","zip":"90069","phone":"(818) 788-8887","image":"\/properties\/718-Kings\/718-Kings.jpg","domain":"www.gfproperties.net\/go\/?prop=16","lat":"34.08483","lng":"-118.36127","type":"community"}
,{"name":"175 S. Beverly","id":"9","address":"175 S. Beverly Dr.","city":"Beverly Hills","state":"CA","zip":"90212","phone":"(818) 788-8887","image":"\/properties\/175-Beverly\/175-Beverly.jpg","domain":"www.gfproperties.net\/go\/?prop=17","lat":"34.06601","lng":"-118.39927","type":"community"}
,{"name":"275 S. Beverly","id":"10","address":"275 S. Beverly Dr.","city":"Beverly Hills","state":"CA","zip":"90212","phone":"(818) 788-8887","image":"\/properties\/275-Beverly\/275-Beverly.jpg","domain":"www.gfproperties.net\/go\/?prop=18","lat":"34.04290","lng":"-118.39925","type":"community"}]});	
	var map = new GMap2(document.getElementById('map-canvas'));
	var losangelesCA = new GLatLng(34.052187,-118.243425);
	map.setCenter(losangelesCA, 8);
	map.addControl(new GLargeMapControl3D());
	
	var siteIcon = new GIcon(G_DEFAULT_ICON);
	siteIcon.image = "/map/disabledcommunitymarker.png";
	siteIcon.shadow = "/map/marker_shadow.png";
	siteIcon.iconSize = new GSize(27, 28);
	siteIcon.shadowSize = new GSize(37, 36);
	markerOptions = { icon:siteIcon };
	
	var bounds = new GLatLngBounds();
	var geo = new GClientGeocoder();
	var markers = [];
	
	if (comm_json.Communities.length > 0) {
		for (i=0; i<comm_json.Communities.length; i++) {
			var location = comm_json.Communities[i];
			addLocation(location);
		} 
		//zoomToBounds();
		map.showBounds(bounds, {top:30,right:20,left:50,bottom:30});
	}
	
	$("#pop_up").appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE));
	
	function addLocation(location) {
		var point = new GLatLng(location.lat, location.lng);		
		var marker = new GMarker(point,markerOptions);
		map.addOverlay(marker);
		markers[i] = marker;
		bounds.extend(marker.getPoint());
		
		location.html = buildPopUp(location);
		
		GEvent.addListener(marker,'mouseover',function(){ 
			marker.setImage("/map/marker_h.png");
		}); 
		
		GEvent.addListener(marker,'mouseout',function(){ 
			marker.setImage("/map/disabledcommunitymarker.png");
		});
		
		GEvent.addListener(marker, "click", function(){
			displayPoint(marker,location);
		});
		
		$("<li />").html(location.name).click(function(){
				displayPoint(marker,location); 
   		}).hover(function(){
				marker.setImage("/map/marker_h.png");
			},function() {
				marker.setImage("/map/disabledcommunitymarker.png");
			}).appendTo("#map-list ul");
	}
	
	function zoomToBounds() {
		map.setCenter(bounds.getCenter());
		map.setZoom(map.getBoundsZoomLevel(bounds));
	}
	
	function displayPoint(marker,location){
		map.panTo(marker.getPoint());
		var markerOffset = map.fromLatLngToDivPixel(marker.getPoint());
		var width = $("#pop_up").outerWidth();
		$("#pop_up .pop_up_content").html(location.html);
		$("#pop_up").show().css({ top:markerOffset.y, left:markerOffset.x-width/2 });
	}
	
	function buildPopUp(location) {

		var template = "<div class='details-box'><div class='pop-up-name'><a href='http://[*domain*]' target='_blank'>[*name*]</a></div><div class='details-box-image'><img src='[*image*]' width='92' height='61' border='0'/></div><div class='btm-address'>[*address*]<br/>[*city*], [*state*]<br/>[*phone*]</div><div class='view-now'><a href='http://[*domain*]' target='_blank'>View Property!</a></div></div>";

		template = template
			.replace(/\[\*name\*\]/g,location.name)
			.replace(/\[\*address\*\]/g,location.address)
			.replace(/\[\*state\*\]/g,location.state)
			.replace(/\[\*domain\*\]/g,location.domain)
			.replace(/\[\*image\*\]/g,location.image)
			.replace(/\[\*city\*\]/g,location.city)
			.replace(/\[\*phone\*\]/g,location.phone);

		return template;
	}
	
	$("#pop_up a.markerDetailClose").click(function() {
		$("#pop_up").hide();
		return false;
	});
	
});

