(function($) {
	$(document).ready(function() {
		Gmap.init();
		window.unload = GUnload;
	});

	Gmap = {
		map: '',
		el: '',
		markers: [],
		init: function() {
			var GeoCoder;

			if (!GBrowserIsCompatible()) {
				return;
			}
		
			this.el = $('#gmap');
			if (!$(this.el).length) {
				return false
			}

			this.map = new GMap2(this.el.get(0));

			var top1 = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(2,2));
			this.map.addControl(new GMapTypeControl(), top1);
			var top2 = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(2,25));
			this.map.addControl(new GSmallMapControl(), top2);

			GEvent.addListener(this.map, "click", function(overlay, point) {
				if (overlay) {
					// we now need a check here in case the overlay is the info window
					// only our markers will have a .myhtml property
					if (overlay.myhtml) {
						overlay.openInfoWindowHtml(overlay.myhtml);
					}
				} else if (point) {
					//whatever you want to happen if you don't click on an overlay.
				}
			});
		
			var searchStr = "Intercoiffure Fritz";
			var addresses = searchStr.split('%%');

			var geoCodes = [{x: 7.661364, y: 47.610635}]
			this.addMarker(addresses[0].replace(/\$/g, '<br />'), geoCodes[0]);
			if(this.markers[0]) {
				GEvent.trigger(this.markers[0], "click");
			}
		},
		addMarker: function(address, latLng) {
			var point = new GLatLng(latLng.y, latLng.x);
			var marker = new GMarker(point);
			this.map.setCenter(point, 16);
			marker.myhtml = "Intercoiffure J. Fritz<br/>Adlergässchen 6<br/>79539 Lörrach";
			this.map.addOverlay(marker);
			this.markers.push(marker);
		},
		defaultCenter: function() {
			this.map.setCenter(new GLatLng(47.610635,7.661364), 16);
		}
	}
})(jQuery);
