var map;

function init() {
	navHover();
	setupMap();
}

function setupMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GScaleControl());
		map.setCenter(new GLatLng(38, -98), 3);
		map.enableDoubleClickZoom();
		
		var html = '<h3>BOSS FACTORY SERVICE</h3>';
		html += '<address>4901 Alexander St.<br />';
		html += 'Los Angeles, CA 90040<br />';
		html += '<strong>(323) 890-3700</strong></address>';
				
		var point = new GLatLng(33.988933, -118.157671);
		var marker = createMarker(point, html);
		map.addOverlay(marker);
		marker.openInfoWindowHtml(html);
		marker.winOpen = true;
	}
}

function showAddress(address) {
	if(address == "Enter your address, city or ZIP code") return false;
	var geoCoder = new GClientGeocoder();
	if (geoCoder) {
		geoCoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert("Sorry, the address “" + address + "” could not be found.");
				} else {
					map.clearOverlays();
					map.setCenter(point, 9);
					
					var html = '<h3>YOUR SEARCH LOCATION:</h3>';
					html += '<address>' + address + '</address>';
					map.addOverlay(createMarker(point, html));
					
					var geoRSS = new GGeoXml("http://www.bossus.com/support/service_centers/geo_rss.xml");
					map.addOverlay(geoRSS);
				}
			}
		);
	}
}

function createMarker(point, html) {
	var marker = new GMarker(point);
	GEvent.addListener(
		marker,
		"click",
		function() {
			if(this.winOpen) {
				this.closeInfoWindow();
				this.winOpen = false;
			} else {
				this.openInfoWindowHtml(html);
				this.winOpen = true;
			}
		}
	);
	GEvent.addListener(
		marker,
		"infowindowclose",
		function() {
			this.winOpen = false;
		}
	);
	return marker;
}
