function healthcoach_googlemap_init(dom_obj, coords) {
	"use strict";
	if (typeof google=="undefined") {
		return;
	}
	if (typeof HEALTHCOACH_STORAGE['googlemap_init_obj'] == 'undefined') healthcoach_googlemap_init_styles();
	HEALTHCOACH_STORAGE['googlemap_init_obj'].geocoder = '';
	try {
		var id = dom_obj.id;
		HEALTHCOACH_STORAGE['googlemap_init_obj'][id] = {
			dom: dom_obj,
			markers: coords.markers,
			geocoder_request: false,
			opt: {
				zoom: coords.zoom,
				center: null,
				scrollwheel: false,
				scaleControl: false,
				disableDefaultUI: false,
				panControl: true,
				zoomControl: true, //zoom
				mapTypeControl: false,
				streetViewControl: false,
				overviewMapControl: false,
				styles: HEALTHCOACH_STORAGE['googlemap_styles'][coords.style ? coords.style : 'default'],
				mapTypeId: google.maps.MapTypeId.ROADMAP
			}
		};
		
		healthcoach_googlemap_create(id);

	} catch (e) {
		
		dcl(HEALTHCOACH_STORAGE['strings']['googlemap_not_avail']);

	};
}

function healthcoach_googlemap_create(id) {
	"use strict";

	// Create map
	HEALTHCOACH_STORAGE['googlemap_init_obj'][id].map = new google.maps.Map(HEALTHCOACH_STORAGE['googlemap_init_obj'][id].dom, HEALTHCOACH_STORAGE['googlemap_init_obj'][id].opt);

	// Add markers
	for (var i in HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers)
		HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].inited = false;
	healthcoach_googlemap_add_markers(id);
	
	// Add resize listener
	jQuery(window).resize(function() {
		if (HEALTHCOACH_STORAGE['googlemap_init_obj'][id].map)
			HEALTHCOACH_STORAGE['googlemap_init_obj'][id].map.setCenter(HEALTHCOACH_STORAGE['googlemap_init_obj'][id].opt.center);
	});
}

function healthcoach_googlemap_add_markers(id) {
	"use strict";
	for (var i in HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers) {
		
		if (HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].inited) continue;
		
		if (HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].latlng == '') {
			
			if (HEALTHCOACH_STORAGE['googlemap_init_obj'][id].geocoder_request!==false) continue;
			
			if (HEALTHCOACH_STORAGE['googlemap_init_obj'].geocoder == '') HEALTHCOACH_STORAGE['googlemap_init_obj'].geocoder = new google.maps.Geocoder();
			HEALTHCOACH_STORAGE['googlemap_init_obj'][id].geocoder_request = i;
			HEALTHCOACH_STORAGE['googlemap_init_obj'].geocoder.geocode({address: HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].address}, function(results, status) {
				"use strict";
				if (status == google.maps.GeocoderStatus.OK) {
					var idx = HEALTHCOACH_STORAGE['googlemap_init_obj'][id].geocoder_request;
					if (results[0].geometry.location.lat && results[0].geometry.location.lng) {
						HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[idx].latlng = '' + results[0].geometry.location.lat() + ',' + results[0].geometry.location.lng();
					} else {
						HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[idx].latlng = results[0].geometry.location.toString().replace(/\(\)/g, '');
					}
					HEALTHCOACH_STORAGE['googlemap_init_obj'][id].geocoder_request = false;
					setTimeout(function() { 
						healthcoach_googlemap_add_markers(id); 
						}, 200);
				} else
					dcl(HEALTHCOACH_STORAGE['strings']['geocode_error'] + ' ' + status);
			});
		
		} else {
			
			// Prepare marker object
			var latlngStr = HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].latlng.split(',');
			var markerInit = {
				map: HEALTHCOACH_STORAGE['googlemap_init_obj'][id].map,
				position: new google.maps.LatLng(latlngStr[0], latlngStr[1]),
				clickable: HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].description!=''
			};
			if (HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].point) markerInit.icon = HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].point;
			if (HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].title) markerInit.title = HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].title;
			HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].marker = new google.maps.Marker(markerInit);
			
			// Set Map center
			if (HEALTHCOACH_STORAGE['googlemap_init_obj'][id].opt.center == null) {
				HEALTHCOACH_STORAGE['googlemap_init_obj'][id].opt.center = markerInit.position;
				HEALTHCOACH_STORAGE['googlemap_init_obj'][id].map.setCenter(HEALTHCOACH_STORAGE['googlemap_init_obj'][id].opt.center);				
			}
			
			// Add description window
			if (HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].description!='') {
				HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].infowindow = new google.maps.InfoWindow({
					content: HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].description
				});
				google.maps.event.addListener(HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].marker, "click", function(e) {
					var latlng = e.latLng.toString().replace("(", '').replace(")", "").replace(" ", "");
					for (var i in HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers) {
						if (latlng == HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].latlng) {
							HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].infowindow.open(
								HEALTHCOACH_STORAGE['googlemap_init_obj'][id].map,
								HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].marker
							);
							break;
						}
					}
				});
			}
			
			HEALTHCOACH_STORAGE['googlemap_init_obj'][id].markers[i].inited = true;
		}
	}
}

function healthcoach_googlemap_refresh() {
	"use strict";
	for (id in HEALTHCOACH_STORAGE['googlemap_init_obj']) {
		healthcoach_googlemap_create(id);
	}
}

function healthcoach_googlemap_init_styles() {
	// Init Google map
	HEALTHCOACH_STORAGE['googlemap_init_obj'] = {};
	HEALTHCOACH_STORAGE['googlemap_styles'] = {
		'default': []
	};
	if (window.healthcoach_theme_googlemap_styles!==undefined)
		HEALTHCOACH_STORAGE['googlemap_styles'] = healthcoach_theme_googlemap_styles(HEALTHCOACH_STORAGE['googlemap_styles']);
}