/* Copyright (C) 2002-2011 by Home of the Brave
   Web http://home.of.the.brave.de
   E-Mail info@brave.de */
/* $Revision: 1.14 $ $Date: 2011/05/26 15:07:32 $ */

/*
 * Convert blocks with CSS class "google_map_conf" to Google maps.
 * dependencies: BodyEvents, Google Maps API
 * owner: Simon.Leidig@brave.de
 *
 */

var GoogleMaps = {
	Zoom:	10,
	Type:	'roadmap',
	Marker:	true,
	Width:	'auto',
	Height:	'auto',
	Controls: {
		Type:	true,
		Zoom:	true,
		Move:	true,
		Scale:	true,
		View:	true
	},
	create: function (n) {
		var c; var m;
		if (!(
			n.className &&
			n.className.match(/\bgoogle_map_conf\b/) &&
			n.firstChild &&
			(c = n.firstChild.innerHTML) && (
				(m = c.match(/\b((?:center_)?)address\s*[:=]\s*([^<\s][^<]*[^<\s])\b/i)) ||
				(m = c.match(/((?:\bcenter\s*[:=]\s*)?)(-?\b\d+(?:\.\d+)?)\b.*?(-?\b\d+(?:\.\d+)?)\b/)) ||
				(m = c.match(/\b(center)\s*[:=]\s*auto\b/))
			)
		)) return;

		var multi = m[1]; var lat = m[2]; var lng = m[3];
		var country = !lng && (m = c.match(/\bcountry\s*[:=]\s*([a-z]{2})\b/i)) ?
			m[1] : null;
		var zoom = (m = c.match(/\bzoom\s*[:=]\s*(\d+)\b/i)) ?
			m[1] : GoogleMaps.Zoom;
		var type = ((m = c.match(/\btype\s*[:=]\s*(normal|roadmap|satellite|hybrid|terrain)\b/i)) ?
			m[1] : GoogleMaps.Type).toUpperCase();
		if (type == 'NORMAL') type = 'ROADMAP';
		var marker = (m = c.match(/\bmarker\s*[:=]\s*(yes|no)\b/i)) ?
			(m[1] == 'yes' ? true : false) : GoogleMaps.Marker;
		var width = (m = c.match(/\bwidth\s*[:=]\s*(\d+)\b/i)) ?
			m[1] : GoogleMaps.Width;
		var height = (m = c.match(/\bheight\s*[:=]\s*(\d+)\b/i)) ?
			m[1] : GoogleMaps.Height;
		var cType = (m = c.match(/\bcontrol_type\s*[:=]\s*(yes|no)\b/i)) ?
			(m[1] == 'yes' ? true : false) : GoogleMaps.Controls.Type;
		var cZoom = (m = c.match(/\bcontrol_zoom\s*[:=]\s*(yes|no)\b/i)) ?
			(m[1] == 'yes' ? true : false) : GoogleMaps.Controls.Zoom;
		var cMove = (m = c.match(/\bcontrol_move\s*[:=]\s*(yes|no)\b/i)) ?
			(m[1] == 'yes' ? true : false) : GoogleMaps.Controls.Move;
		var cScale = (m = c.match(/\bcontrol_scale\s*[:=]\s*(yes|no)\b/i)) ?
			(m[1] == 'yes' ? true : false) : GoogleMaps.Controls.Scale;
		var cView = (m = c.match(/\bcontrol_view\s*[:=]\s*(yes|no)\b/i)) ?
			(m[1] == 'yes' ? true : false) : GoogleMaps.Controls.View;

		var g		= google.maps;
		var points	= [];
		var infos	= [];
		var info	= document.createElement('div');
		var sep		= false;
		var first	= true;
		for (var j = 1; j < n.childNodes.length; j++) {
			var d = n.childNodes[j];
			if (!(
				d.nodeName.toLowerCase() == 'div'	&&
				d.className							&&
				d.className.match(/\bblock\b/)		&&
				d.innerHTML.length
			)) {
				if (
					multi								&&
					d.nodeName.toLowerCase() == 'h1'	&&
					d.innerHTML.length					&&
					(m = d.innerHTML.match(/(-?\b\d+(?:\.\d+)?)\b.*?(-?\b\d+(?:\.\d+)?)\b/))
				) {
					points.push(new g.LatLng(m[1],m[2]));
					if (!first) infos.push(info);
					info	= document.createElement('div');
					sep		= false;
					first	= false;
				}
				continue;
			}
			var p = document.createElement('div');
			p.innerHTML = d.innerHTML;
			if (sep) info.appendChild(document.createElement('p'));
			info.appendChild(p);
			sep = true;
		}
		infos.push(info);

		n.innerHTML = '';
		n.className = n.className.replace(/\bgoogle_map_conf\b/,'google_map');
		var t = document.createElement('div');
		t.className = 'block';
		if (width  != 'auto') t.style.width  = width +'px';
		if (height != 'auto') t.style.height = height+'px';

		var show = function (point,bounds) {
			if (!point) return;
			n.appendChild(t);
			var map = new g.Map(t,{
				center:						point,
				zoom:						1 * zoom,
				mapTypeId:					g.MapTypeId[type],
				mapTypeControl:				cType,
				scaleControl:				cScale,
				streetViewControl:			cView,
				navigationControl:			cMove || cZoom,
				navigationControlOptions:	{style:g.NavigationControlStyle[cMove ? 'ZOOM_PAN' : 'SMALL']}
			});
			var mark = marker && !multi ? new g.Marker({position:point,map:map}) : null;
			if (multi) {
				for (var i = points.length; i-- > 0;) {
					mark = new g.Marker({position:points[i],map:map});
					if (infos[i].firstChild) new g.InfoWindow({content:infos[i]}).open(map,mark);
				}
				if (bounds) map.fitBounds(bounds);
			} else if (info.firstChild) new g.InfoWindow({content:info,position:map.getCenter()}).open(map,mark);
		}
		if (lng) show(new g.LatLng(lat,lng));
		else if (lat) new g.Geocoder().geocode({address:lat,region:country},function (results,state) {
			if (state == g.GeocoderStatus.OK) show(results[0].geometry.location);
		});
		else if (points.length > 0) {
			if (points.length == 1) show(points[0]);
			else {
				var bounds = new g.LatLngBounds();
				for (var i = points.length; i-- > 0;) bounds.extend(points[i]);
				show(bounds.getCenter(),bounds);
			}
		}
	}
};
BodyEvents.addListener('load', function () {
	try { if (!window.google.maps.Map) return; }
	catch (e) { return; }
	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++) GoogleMaps.create(divs[i]);
} );

