$(document).ready(function(){
	initialize();
});
function showTooltip(tip) {
	GEvent.trigger(places[tip], 'mouseover');
}
function hideTooltip(tip) {
	GEvent.trigger(places[tip], 'mouseout');
}
function initialize() {
	if (GBrowserIsCompatible()) {
		
		var map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(37.732, -121.4875), 11);
		var customUI = map.getDefaultUI();
		customUI.controls.maptypecontrol = true;
		customUI.controls.largemapcontrol3d = true;
		customUI.zoom.scrollwheel = false;
		map.setUI(customUI);

		function createArea(enc, lev, bg, label, url) {
			area = new GPolygon.fromEncoded({
				polylines: [{
					points: enc,
					zoomFactor: 32,
					levels: lev,
					numLevels: 4,
					color: bg,
					opacity: ".6",
					weight: 2
				}],
				color: bg,
				fill: "0",
				opacity: "0",
				outline: true
			});

			var tooltip = new Tooltip(area, label, 0, true);
			GEvent.addListener(area, "mouseover", function() { this.setFillStyle({opacity: ".6"}); this.tooltip.show(); });
			GEvent.addListener(area, "mouseout", function() { this.setFillStyle({opacity: "0"}); this.tooltip.hide(); });
			GEvent.addListener(area, "click", function() { window.location = url });
			area.tooltip = tooltip; 
			map.addOverlay(tooltip);
			map.addOverlay(area);

			return area;
		}
		function createMarker(lat, lng, titletip, content) {
			var latlng = new GLatLng(lat, lng);
			mark = new GMarker(latlng, { title: titletip });
			mark.bindInfoWindowHtml(content);
			map.addOverlay(mark);
		}

		var color1 = '#d6c511'; // yellow
		var color2 = '#1bd611'; // green
		var color3 = '#1173d6'; // blue
		var color4 = '#bf222d'; // red

		// Defined as: areas('id', 'points_encoding', 'B_levels', 'colors', 'tooltip', 'link_url')
		var areas = new Array(
			new Array('tracy', 'u|ieFj}ldVszA??or@srA??se@rI?v@yv@~e@gCaUsnBeh@dAu@{cBki@?fF_]z`@eAbDaqDnn@pVfg@gCbAut@`UhCvnAvwE`wBeALuaBpv@iCLllB|{AgCkBfyB~fB?eaAvpB|lBvXdjAllBu~CdfC_w@sVqD{wFqxAlEf@hhC{zBcALp}BgfAxeAnGy{D', 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB', color3, 'Tracy', '/Tracy-Ca-Real-Estate'),

			new Array('mount', 'kbteFhq|dVmXe_@luA{aC~}ArGaDr{C}uCeA', 'BBBBBB', color2, 'Mountain House', '/Mountain-House-CA-Real-Estate')
		);

		// Defined as: array('lat', 'lng', 'title', 'content')
		var markers = new Array(
			new Array('37.7395769','-121.4452877', 'Frank Silveria\'s office', '<div class="bubble" style="width: 375px"><h1>Keller Williams Realty - Frank Silveria III</h1><p>1858 West 11th Street<br />Tracy, CA 95376<br />(209) 835-9300<br /><a href="http://thesilveriateam.com">thesilveriateam.com</a></p><p><img src="http://thesilveriateam.com/agent_files/map/logo.jpg" width="150" height="19" /></p></div>'),

			new Array('37.7762870','-121.5324700', 'Mountain House', '<div class="bubble"><h1>Mountain House</h1></div>'),

			new Array('37.7390879','-121.4192480', 'Tracy', '<div class="bubble"><h1>City of Tracy</h1><p>333 Civic Center Drive<br />Tracy, CA 95376<br />(209) 831-6000<br /><br /><a href="http://tracy.ca.us">tracy.ca.us</a></p><p><img src="http://base.googlehosted.com/base_media?q=LBC-12679477-83007574&size=2&hl=en" width="90" height="60" /></p></div>'),

			new Array('37.7601790','-121.4616370', 'West Vally Mall', '<div class="bubble"><h1>West Valley Mall</h1><p>3200 Naglee Road<br />Tracy, CA 95304-7316<br />(209) 836-4091<br /><br /><a href="http://t-mobile.com">t-mobile.com</a></p><p><img src="http://base.googlehosted.com/base_media?q=maps:www.igougo.com/photos/journal/thum/westvalley3_prefRes.jpg&size=2&hl=en" width="90" height="72" /></p></div>')
		);
		

		places = new Array();
		for(i=0; i<areas.length; i++) {
			places[areas[i][0]] = createArea(areas[i][1], areas[i][2], areas[i][3], areas[i][4], areas[i][5]);
		}
		marks = new Array();
		for(i=0; i<markers.length; i++) {
			createMarker(markers[i][0], markers[i][1], markers[i][2], markers[i][3]);
		}
	}
}
